Wednesday, January 11, 2012

Linux and ADB

Hello all!
I thought I would post a little guide on what you have to do to get adb to work with eclipse and your device out on ubuntu.
This assumes:

  • You're running Linux as a normal user
  • You've installed the Android SDK
  • You've installed Eclipse
  • You've installed the Android SDK plugin for Eclipse
I did all of that and it worked wonderfully for the emulator, but when I tried to run it on my Nexus One, it wouldn't recognize it. Since I do linux for a living, I though I would do a short writeup on what was broken and how to fix it.
Lets begin with reproduction of the problem. I'll plug in my Android device with USB Debugging turned on. Then I'll go to a terminal and type:


user@server:~/Projects/android-sdk-linux_86/platform-tools$ ./adb devices
List of devices attached 
???????????? no permissions

Well that's no fun. Note: if I had the emulator running, it would find it just fine. Well lets take a look at what went wrong. To understand the problem we are facing, you must understand that in linux, everything is accessed like a file, and files have permissions. The adb-server was having issues reading the usb device and to fix that, we must find the USB device "file" and make sure the permissions are correct on it. Devices in linux are kept under /dev/, so after looking around there a bit I find it at /dev/bus/usb/001/106 
I can do the following


user@server:~/Projects/android-sdk-linux_86/platform-tools$ sudo chown user /dev/bus/usb/001/106
<enter password>
user@server:~/Projects/android-sdk-linux_86/platform-tools$ ./adb kill-server
user@server:~/Projects/android-sdk-linux_86/platform-tools$ ./adb start-server


And now when I list my devices


user@server:~/Projects/android-sdk-linux_86/platform-tools$ ./adb devices
List of devices attached 
HT9CRP801xxx device

Huzzah! But do I have to go through that song and dance each time I want to develop? NO!
You don't even have to resort to writing your own script! You can use a function included in most linux distributions called udev. udev is a program that is ran every time hardware changes on your machine. You have the ability to write "rules" to compare the new hardware against some parameters and if  they match, you can have it do some action, like call a script or set permissions on the newly created device files. All rules go into /etc/udev/rules.d/ . Lets take a look at the rule I created.


user@server:~/Projects/android-sdk-linux_86/platform-tools$ cat /etc/udev/rules.d/99-android.rules 
SUBSYSTEM=="usb", SYSFS{idVendor}=="18d1", MODE="0666"

Basically, make a file just like that one, but change the 18d1 to match your device. It's pretty straight-forward (uh...sure), but where did that 18d1 come from? It came from the output of lsusb


user@server:~/Projects/android-sdk-linux_86/platform-tools$ lsusb
... 
Bus 001 Device 107: ID 18d1:4e12  
...

Mine had a blank description  but maybe other device would say something about adb in there to give you a hint. I know this isn't quite a step-by-step guide, but hopefully it helps someone out. Let me know in the comments if you get stuck.

4 comments:

  1. very interesting! :)
    On the firstlook like wtf but think if I read it 2-3 times I will understand it :)
    +1 Follower because android user ! :)

    ReplyDelete
  2. Thats whati was looking forward to fix a prob.. nice... Android power ! +1

    ReplyDelete
  3. Took me a few reads to realise what was being said, but thanks!:D
    +1

    ReplyDelete
  4. hard to undderstand at first. solved after a while.

    ReplyDelete