51-android.rules to autorun Android’s adb server
This webpage helps you set up your 51-android.rules file so that you can automatically start Android’s adb server program.
This file is of interest to you if you use the Android SDK toolkit or Eclipse on Linux while trying to develop or test a program on your Android Cell Phone or Tablet. The contents on this webpage are based on information found on Android’s SDK Dev Guide, the Vendor and Product list found on the USB ID Repository and information gathered from various other websites and pages (as of October 11, 2011).
This webpage was written due to what seemed to be some confusion and a lack of information needed to get other Linux Distros such as Fedora, Mageia, Mandriva, RedHat, and other distros with subtle differences to work.
WHY ARE YOU INTERESTED IN THIS FILE?
Three reasons come to mind, but you may think of other reasons why too.
- If you have used the Android Debug Bridge (adb server) or the Eclipse Debugger on Linux and you cannot find or connect to your Cell Phone or Tablet.
- If you are interested in how this rules file works for the purposes of tethering or connecting other hardware you may have in mind.
- If you are interested in how this works for the purposes of connecting hardware-software automatically to your computer.
The problem that is happening is that the SDK tries to connect to the Android cell Phone or Tablet as a regular user program, but Linux is blocking and firewalling as a default condition. Linux needs to know that you are allowed access, and therefore needs to setup for those conditions.
These conditions can be solved manually, or automatically. To do this automatically, the solution is to describe what needs to be done when the hardware is plugged into the USB port on your computer, and the way to do that «here» is using a udev rules file. For the Android SDK, this file is referred to, as 51-android.rules.
First, you can download 51-android.rules, or you can copy and paste the file shown below into a text editor.
You need to modify this file before you can use it.
GET YOUR PHONE OR TABLET ID DETAILS FIRST
Before you can make use of the file above, you need to see what are the
When you plug your Android phone or tablet into the USB on your computer, it identifies itself differently based on which mode your cell phone is set to, for example, the HTC Magic cell phone identifies itself as «0bb4:0c01» in normal mode, but as «0bb4:0c02» when it is in debug mode.
If you have a development cell phone or tablet, go to it’s settings and find the setting you need to turn it into «debug» mode. After you switch your Android phone or tablet into «debug» mode, then plug it into the USB slot on your computer. Next go to the command line, and use the lsusb command to find the cell phone or tablet identity. You can follow the steps shown below.
- su — The lsusb command needs to be done in Root Super User mode, so, first you will need to do su.
lsusb — This command shows all the devices currently plugged into your computer’s USB ports. You should see your cell phone device appear amongst the list.
This example above shows the results of «0bb4:0c02«.
ADD OR EDIT THE FILE WITH YOUR PHONE OR TABLET ID DETAILS
The udev program that uses the ID information is case sensitive and expects the information as lower-case hexidecimal values. You need to edit, or add a line to your 51-android.rules file with the information you found if it isn’t already included in the list. It should look something like this:
The first line beginning with «#» is an optional comment. The second line is the important line which checks for the ID and then sets the value «yes» which is needed further down in the file.
If your Android cell phone or table does not have a separate code for «debug» mode, or you want to make the udev rules a bit more general, then you can substitute ? (question marks), such as ATTR
. however, you need to be careful that you do not use other items from the same manufacturer at the same time you use this rule file, otherwise they may also be incorrectly seen as another similar device (for example, a Motorola Modem would mistakenly also be seen as a Motorola Cell Phone).
If your device does not have a separate ID for debug mode, or if you decide to do the more general method, you should comment-out the instructions at the bottom because you may not want to run the Android adb server every time you plug-in your Development Phone or Tablet.
If you want to simplify this file, you can remove all the other lines referring to other Cell Phones that you do not have or use.
USB OR PLUGDEV GROUP?
Good question. This is one of those subtle differences between various Linux distributions. For example: RedHat, Fedora, Mageia, Mandriva, etc. have a user group named «usb» while other distributions such as Debian, Ubuntu, etc. have a user group named «plugdev«.
If you want to follow the example shown on Android’s Using Hardware Devices and you are using a Linux distribution which makes use of «plugdev«, then you will want to edit this line near the bottom of the file:
MODE=»0664″ OR MODE=»0666″?
If you want only users belonging to group «usb» to be able to write to your Cell Phone or Tablet, then keep MODE=»0664″, but you need to add yourself to the «usb» or «plugdev» group. If you want to keep it simple and give eveyone access, then you will need to change the command to MODE=»0666″.
YOU NEED TO EDIT THE RUN+ INSTRUCTION
When you downloaded Android’s SDK toolkit, you saved it somewhere, like your home directory, or another directory. Inside the toolkit is a subdirectory «android-sdk-linux_x86/platform-tools/» which has the adb server which is needed when you connect to your hardware to debug a program. You will need to edit the «RUN+» instruction to point to the location where your adb is located.
If you run the adb server as a normal user, you can run into the problem that Linux blocks access to your hardware.
- adb devices — You probably have seen this before. You need your 51-android.rules file set up first.
If you get these udev 51-android.rules set up, and you run the adb server as a root user, then you can access your hardware device. Once the adb server is running as a root user, you can then access your hardware as a normal user too.
- adb devices — You should get a list of the devices connected to your computer after you get these udev 51-android.rules set up.
If you want to run Android’s adb server under another user other than «root«, then modify the «RUN+» statement like this (substitute a user for «you«):
Note: Make sure the user you choose has user or group access to the USB port.
Here is an example output if you did it this way:
- adb devices — You should get a list of the devices connected to your computer after you get these udev 51-android.rules set up.
ps -A -f | grep «adb» — Check to see who is adb running under.
WHAT DOES SYMLINK+ DO?
This is not important for Android’s adb server since it does not use this, but it might be useful to you if you need a consistent location to find your hardware for other purposes.
Some computers have USB devices appear in directory «/dev/usb/«, other computers may have devices appear in directory «/dev/bus/usb/«. You may have noticed that when you plug in a device into a USB port, it appears on one port when you look inside directory «/dev«, then if you unplug the device and plug it back in, it appears on another location. Do it again, it appears somewhere else in directory «/dev«. Not very consistent. What the «SYMLINK+» instruction does, is create a link to your device in a consistent location, so regardless of where it appears in directory «/dev/«, you can always find a link at «/dev/android_adb/» when you plug your device into a USB port on your computer.
A few webpages referred to «android_adb«, so it seemed a good choice just to be consistent.
This shows you what it could look like for you after you setup your udev rules file.
- ls -l /dev/an* — This would display the symlink that is created when you plug-in your Android Cell Phone or Tablet (after you set up your 51-android.rules).
ls -l /dev/bsg/ — For this example, the symlink pointed to a directory named «bsg«, so if you look in directory «bsg«, you would then see the device is already connected.
INSERT YOUR FILE IN UDEV RULES.D DIRECTORY
Once you have added or edited your particular Cell Phone or Tablet to the rules file, and added the location of where you keep your adb server, it is now time to save the 51-android.rules file in directory «/etc/udev/rules.d/«. We assume you saved your edited file in your «
su — The next few commands need to be done in Root Super User mode.
/51-android.rules /etc/udev/rules.d/ — Copy the file to the udev rules directory.
chown root:root /etc/udev/rules.d/51-android.rules — Use the chown command to make it a file that belongs to Root.
chmod 644 /etc/udev/rules.d/51-android.rules — Use the chmod command to make it known as a read-only type file for all other users.
exit — You no longer need to be in Root Super User mode.
exit — Exit the command-line terminal. You are done.
NOTES: According to some websites, some people indicate they have had trouble with this file if it is named 51-android.rules. This can happen if another rule after this changes some settings, such as user or group ownership of the USB ports get reverted back to «root:root«. To solve this problem, you will need to save your file with a number that makes this rule happen later in the udev rules process, for example 61-android.rules or 71-android.rules (the 51, 61, 71 indicates when the rule applies).
TEST TO SEE IF THE ADB SERVER WORKS
After saving the rules file above, check if it all works. Reboot your computer. Change your Cell Phone or Tablet to «Debug» mode, and also enable «unknown sources«, then plug it into your computer’s USB port. Then go to the command line and check:
- adb devices — The adb server should already be running by now, so you should see a list of the devices already connected when you try this command.
NOTE: Some devices may still have problems, so you might need to run android update adb for some devices, or you may need to append a vendor ID value at the end of file
/.android/adb_usb.ini (for example 0x2080 for the Nook, or 0x0e79 for the Archos). Please read more about your specific device as there may be some extra steps required.
ls -l /dev/an* — This should display a symlink to your Android Cell Phone or Tablet (and it will disappear if you unplug your Phone).
ls -l /dev/bsg/ — For this example, the symlink points to a directory named «bsg«, so if you look in directory «bsg«, you should see your device with the correct group. and mode of 0664. If the group and permissions are wrong, you likely need to make the rule file run later, like maybe 6N or 7N or maybe 8N. Take a look inside directory «/etc/udev/rules.d» and look at the files after 51 towards 99 for possible conflicts.
adb shell — If the adb server is running, you can now create a shell into your cell phone.
ls -l — List the directories inside your cell phone.
exit — Exit the shell out from your phone.
exit — Exit the command-line terminal. You are done.
COMMENTS, ERRORS OR FEEDBACK
It is great if you find this 51-android.rules file useful. If you have any comments, suggestions, or fixes, please send them using the comment section here, thanks!
If you want to know more about udev, you can read this very detailed guide writing udev rules.
Источник
mpaepper / android-ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
When programming apps for Android, you usually want to test them on real Android devices. |
This little gist describes how to do so using Ubuntu 14. |
First, you need to download and install the Android development IDE (http://developer.android.com/sdk/index.html) and create an Android project which you want to debug. |
Next, you need to setup the debugging mode on your Android device. Starting in Android 4.2 you need to enable the developer options first: 1) Go to settings 2) Go to About Phone 3) Tap the build number 10 times (or more, not sure ;)) and you will get the notification that you enabled it. |
Then go to the developer settings and enable the debug mode for your phone. |
Now you think you can just plug it into your USB mode? — Nope. |
First, we will have to setup a udev rule for your device: |
1) Create this file as root: /etc/udev/rules.d/51-android.rules |
2) Make the file readable: sudo chmod o+r 51-android.rules |
3) Edit it with this content: SUBSYSTEM==»usb», SYSFS |
(Note: Sometimes you need net instead of usb, that was the case for me, check other rules in /etc/udev/rules.d for comparison) |
4) sudo service udev restart |
The vendor ID (0bb4) depends on your specific phone, find a list here: http://developer.android.com/tools/device.html#VendorIds |
Then you want to install adb if you have not yet: |
1) sudo apt-get install android-tools-adb |
2) sudo adb kill-server |
3) sudo adb start-server |
Now you can plugin your device. There should be message on your phone whether you want to allow to debug it -> obviously say yes. |
Then running sudo adb devices should properly list your device. |
Using the Android IDE you can now debug your app on your device 🙂 |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник