217

I connected to my live device using the adb and the following commands:

C:\>adb -s HT829GZ52000 shell
$ ls
ls
sqlite_stmt_journals
cache
sdcard
etc
system
sys
sbin
proc
logo.rle
init.trout.rc
init.rc
init.goldfish.rc
init
default.prop
data
root
dev
$ cd data
cd data
$ ls
ls
opendir failed, Permission denied

I was surprised to see that I have access denied. How come I can't browse around the directories using the commandline like this?

How do I get root access on my phone?

Top-Master
  • 7,611
  • 5
  • 39
  • 71
gregm
  • 12,019
  • 7
  • 56
  • 78
  • decided to create a simple avd to have access to the database. Does anyone have any links to guides, without "custom" flare and graphics for developers? Just how to get plain vanilla android on a purchased device. – bgs Jan 06 '11 at 19:20
  • Simple... make sure your phone screen is on. Click "Allow" when it asks if you want Unknown to have access to root. Done. –  Sep 25 '10 at 02:21
  • No such prompt occurs on my Android 7.1.1 device. But adb goes ahead and lists contents of the directory. Trouble is, the contents don't actually exist. I'm using adb-sync to add contents. It's pretty weird. – Tom Russell Jul 29 '18 at 06:04

16 Answers16

248

Starting from API level 8 (Android 2.2), for the debuggable application (the one built by Android Studio all the times unless the release build was requested), you can use the shell run-as command to run a command or executable as a specific user/application or just switch to the UID of your application so you can access its data directory.

List directory content of yourapp:

run-as com.yourapp ls -l /data/data/com.yourapp

Switch to UID of com.yourapp and run all further commands using that uid (until you call exit):

run-as com.yourapp
cd /data/data/com.yourapp
ls -l
exit

 
Note 1: there is a known issue with some HTC Desire phones. Because of a non-standard owner/permissions of the /data/data directory, run-as command fails to run on those phones.

Note 2: As pointed in the comments by @Avio: run-as has issues also with Samsung Galaxy S phones running Cyanogenmod at any version (from 7 to 10.1) because on this platform /data/data is a symlink to /datadata. One way to solve the issue is to replace the symlink with the actual directory (unfortunately this usually requires root access).

Volo
  • 28,673
  • 12
  • 97
  • 125
  • 2
    This works as you explained where you `run-as com.mypackage`. The problem I ran into is `sqlite3 /data/data/com.mypackage/databases/mydb` says "sqlite3: permission denied". Can sqlite3 only be run by root? – styfle Nov 27 '11 at 06:28
  • 1
    @styfle That's mean that either you don't have the permission to run `sqlite3` executable (or access folder where it is located), or there is no `sqlite3` at all on your phone (see for example: http://stackoverflow.com/a/3645800/648313) – Volo Dec 01 '11 at 15:18
  • 1
    @styfle Check http://stackoverflow.com/a/8433520/648313 for the possible solution of `sqlite3` issue you have. – Volo Dec 08 '11 at 16:29
  • 5
    Works like charm when it comes to listing files stored in the internal storage. – zaplec Jun 21 '12 at 13:10
  • 5
    I had to change permissions on the file to be able to move it to my sdcard, because I couldn't cp it running as my application. So, 1) run-as com.package.example, chmod 777 databasefile.db. 2) exit, go to sdcard, cp /data/data/com.package.example/databases/databasefile.db . And step 3 should be reverting the 777 permissions – Maragues Apr 30 '13 at 07:45
  • 1
    `run-as` has issues also with `Samsung Galaxy S` phones running `Cyanogenmod` at any version (from 7 to 10.1) because on this platform `/data/data` is a symlink to `/datadata`. One way to solve the issue is to replace the symlink with the actual directory, however, I've found more convenient to recompile `run-as` without the symlink check. (http://gitorious.org/android-enablement/system-core/trees/1f4d95296acf34a93128332441782a80c10845b4/run-as) – Avio May 16 '13 at 16:13
  • 3
    My package is unknown no matter how correctly I spell it into my terminal : ( – Costa Michailidis Jun 24 '14 at 01:10
  • 3
    This should be the answer marked correct as this is the right method to use for non-rooted phones. – LNI Apr 27 '16 at 16:43
153

If you want to browse everything on your device, you need to do two things:

  1. You need to have a phone with root access in order to browse the data folder on an Android phone. That means either you have a developer device (ADP1 or an ION from Google I/O) or you've found a way to 'root' your phone some other way.
  2. You need to be running ADB in root mode, do this by executing: adb root

Note that if step-1 is done correctly, step-2 just works (no need to re-build anything from source-codes).

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Reto Meier
  • 96,655
  • 18
  • 100
  • 72
  • 88
    And "adb root" requires a custom build of adb, otherwise "adbd cannot run as root in production builds" – bortzmeyer Jan 31 '11 at 13:55
  • 17
    custom build of adb? where can I get it from? – Gopinath May 02 '11 at 11:09
  • 2
    @Gopinath: I believe you have to build it from source for the target device, configured to allow root using the ndk then flash it onto the device along with an image of the binary files. – Ben Jaguar Marshall Nov 02 '12 at 02:00
  • 5
    AFAIK, it is not necessary to have a custom build of adb. It IS necessary to have *adbd* support running as root, but this adbd is on the device - so whatever modification was made to make the device have root access is enough. – nitzanms Oct 01 '14 at 11:14
  • 1
    @bortzmeyer Did you try giving yourself root privileges? After executing `adb root`, execute `su`. – arkon Jan 27 '16 at 21:46
  • @b1nary.atr0phy adb root does not result in shell. It just says: "adbd cannot run as root in production builds" – Andrew Smart Feb 01 '16 at 02:36
  • @Gopinath Here is a custom build of adb: http://stackoverflow.com/a/28070414/1185900 Here is a claim that by setting 'ro.debuggable' to 1 in ./default.prop in initramfs.cpio will allow stock adb to 'adb root': http://forum.xda-developers.com/showthread.php?t=833756 As nitzanms has said, a custom build of adb isn't strictly necessary, but will probably be quicker/easier than editing that initial ramdisk file. – Andrew Smart Feb 01 '16 at 02:49
  • 2
    This should not be the accepted answer. See the answer from Idolon below. – Brill Pappin Nov 15 '17 at 15:20
  • It works with run-as for me, see below answer: https://stackoverflow.com/a/7712173/7483211 – Cornelius Roemer Dec 23 '19 at 22:17
  • If you're like me and are instead running a device through Android's Emulator another important point to keep in mind is that Images that include the "Play Store" can't be rooted, or at least not easily. Make sure if you're running an emulated phone that your image doesn't include the play store and you should be able to "adb root" just fine. It's probably why you're getting that error @bortzmeyer . Android also makes mention of using images without the play store here https://developer.android.com/about/versions/10/get#on_emulator . – Adrian Stratienco May 05 '20 at 18:00
  • I have tried by using "adb root" but still not able to access ls /sdcard/Android/data//files in Android 11 – sweet_vish Nov 24 '21 at 16:51
58

before we start, do you have a rooted phone? if not, I strongly suggest that it's time you make the jump. 99% of the tutorials that help you to do this require that you have a rooted phone (I know b/c I spent about an hour searching for a way to do it without having a rooted phone.. couldn't find any..) also if you think about it, your iPhone also has to be rooted to do this same task. So it's totally reasonable. More about rooting at end of answer.

from your command line type:

adb shell

this takes you to your android shell comand line (you should see something like this: shell@android:/ $ now type:

shell@android:/ $run-as com.domain.yourapp

this should take you directly to the data directory of com.domain.yourapp:

shell@android:/data/data/com.domain.yourapp $ 

if it doesn't (ie if you get an error) then you probably don't have a rooted phone, or you haven't used your root user privileges. To use your root user privileges, type su on the adb command line and see what happens, if you get an error, then you're phone is not rooted. If it's not, root it first then continue these instructions.

from there you can type ls and you'll see all the directories including the dbs:

shell@android:/data/data/com.domain.yourapp $ ls

cache
databases
lib
shared_prefs   

after that you can use sqlite3 to browse the dbase.. if you don't have it installed (you can find it out by typing sqlite3, if you get command not found then you'll have to install it. To install sqlite, follow instructions here.

about rooting: if you've never rooted your phone before, and you're worried about it screwing your phone, I can tell you with full confidence that there is nothing to worry about. there are tonnes of quick and easy phone rooting tutorials for pretty much all the new and old models out there, and you can root your phone even if you have a mac (I rooted my s3 with my mac).

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
  • 9
    You don't need a rooted phone for `run-as` to work. I just tried it on a production-build Galaxy Nexus S. – Ted Hopp Oct 10 '13 at 02:44
  • @TedHopp and so b/c it works just fine on your production-build Galaxy Nexus S.. it will work just fine on all android phones right? – abbood Apr 02 '14 at 05:19
  • @TedHopp personally i'm not much of an android developer.. and so instructions like `build the application as debuggable (either by building it with the Eclipse, or ant debug command)` may sound trivial to you, but they kinda intimidate me. I guess my answer is more for people who are comfortable with rooting their android phones, but aren't well versed in the android dev world. But i'm glad we're having this discussion.. I want the readers of my answer to be aware of the other non-rooting alternatives – abbood Apr 02 '14 at 11:51
  • also keep in mind i never claimed that *all* phones must be rooted.. in the original answer I said %99 of tutorials require a rooted phone.. ;) – abbood Apr 02 '14 at 11:53
  • 1
    Great answer man. I am using a few different phones here, (MotoX, nexus4, LG G3, SGIII) and this is pretty good for all. – Chad Bingham Oct 03 '14 at 22:58
  • I tried run-as : on my Xiaomi phone which is too new for a root exploit yet, against a security app from Verisign. adb said "Package xx.xxx.xxx.xxx is not debuggable". This is good news, since i want my security app to be secure. – andrew lorien May 23 '16 at 04:57
  • I do not recommend rooting your phone if you actually use it as a phone. If its sitting on your desk for development only, i suppose the risk is much less, but you do not need to root it to get access to your development application. – Brill Pappin Nov 15 '17 at 15:55
55

$ adb shell

$ cd /data

$ ls

opendir failed, Permission denied


You should do this:

$ adb shell

$ cd /data

shell@android:/data $ run-as com.your.package 

shell@android:/data/data/com.your.package $ ls

OK!

HelloWorld
  • 7,156
  • 6
  • 39
  • 36
  • This works! A couple of things to be done before executing any commands. Once the developer options are enabled on the phone, we would also need to enable **USB debugging**. I also additionally enabled something on my phone (HTC M8) called **Select debug app** and chose my app. Could be standard on all phones...should be. – srinij May 19 '16 at 06:42
  • giving error `run-as: Could not set capabilities: Operation not permitted` – mufazmi Feb 25 '22 at 07:12
  • This just shows the app's internal storage dir, right? – IgorGanapolsky Dec 01 '22 at 17:17
32

I had a similar problem when trying to operate on a rooted Samsung Galaxy S. Issuing a command from the computer shell

> adb root

fails with a message "cannot run as root in production builds". Here is a simple method that allows to become root.

Instead of the previous, issue the following two commands one after the other

> adb shell
$ su

After the first command, if the prompt has changed from '>' to '$' as shown above, it means that you have entered the adb shell environment. If subsequently the prompt has changed to '#' after issuing the second command, that means that you are now root. Now, as root, you can do anything you want with your device.

To switch back to 'safe' shell, issue

# exit

You will see that the prompt '$' reappears which means you are in the adb shell as a user and not as root.

rytis
  • 648
  • 5
  • 10
19

I had a lot of trouble with this also. I still don't fully understand the permission and root run, but this worked for me (one of the previous answers partly) to copy database file from /data/data/[package name]/databases/my_db.db . Running shell root, or su in shell for some reason didn't work, nor did copying the db file (I could navigate to the directory though), nor did sqlite3.

So, this worked! In DOS command prompt:

C:\Program Files\Android\android-sdk\platform-tools>adb shell
1|shell@android:/ $ run-as de.vogella.android.locationapi.maps
run-as de.vogella.android.locationapi.maps
1|shell@android:/data/data/de.vogella.android.locationapi.maps $ cd /data
cd /data
shell@android:/data $ cd data
cd data
shell@android:/data/data $ cd de.vogella.android.locationapi.maps
cd de.vogella.android.locationapi.maps
shell@android:/data/data/de.vogella.android.locationapi.maps $ cd databases
cd databases
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $ ls
ls
bus_timetable_lines.db
bus_timetable_lines.db-journal
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $ cat bus
_timetable_lines.db > /sdcard/db_copy.db
 bus_timetable_lines.db > /sdcard/db_copy.db                                   <
shell@android:/data/data/de.vogella.android.locationapi.maps/databases $exit   ^
exit
shell@android:/ $ exit
exit

C:\Program Files\Android\android-sdk\platform-tools>

Now go to SDCARD directory and get your file db_copy.db . Even that was hidden, but I managed to email it. Back in Windows, I was able to open db file with SQLite Database Browser. :)

MSquare
  • 6,311
  • 4
  • 31
  • 37
  • I tried -> run-as But the adb didnt not recognized my app_package_name. I used the package_name that I specified in my AndroidManifest.xml. Is there something I am missing? – ARK Jul 22 '15 at 15:18
5

This works if your Android device is rooted by any means (not sure if it works for non-rooted).

  1. adb shell - access the shell
  2. su - become the superuser.

You can now read all files in all directories.

roshnet
  • 1,695
  • 18
  • 22
4

Following are the two steps to get root access:

  1. Your android devices must be rooted.
  2. In ADB shell, type su and the android device will prompt y/n? You choose allow.
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
VietHuong
  • 189
  • 1
  • 5
3

production builds can't enter /data/app

drwxrwx--- system   cache             1970-01-01 08:00 cache
drwxrwxr-x root     system            1970-01-01 08:00 mnt 
drwxrwx--x system   system            1970-01-01 08:15 data

might need change to right owner to operate it.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
keith
  • 47
  • 1
2

The problem could be that we need to specifically give adb root access in the developnent options in the latest CMs.. Here is what i did.

abc@abc-L655:~$ sudo adb kill-server
abc@abc-L655:~$ sudo adb root start-server * daemon not running. starting it now on port 5037 * * daemon started successfully * root access is disabled by system setting - enable in settings -> development options

after altering the development options...

abc@abc-L655:~$ sudo adb kill-server
abc@abc-L655:~$ sudo adb root start-server
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
restarting adbd as root
abc@abc-L655:~$ adb shell
root@android:/ # ls /data/ .... good to go.. 
ashishsony
  • 2,537
  • 3
  • 26
  • 38
1

if you know the application package you can cd directly to that folder..

eg cd data/data/com.yourapp

this will drop you into a directory that is read/writable so you can change files as needed. Since the folder is the same on the emulator, you can use that to get the folder path.

Matthias
  • 3,582
  • 2
  • 30
  • 41
Mark
  • 43
  • 1
1

If you just want to see your DB & Tables then the esiest way is to use Stetho. Pretty cool tool for every Android developer who uses SQLite buit by Facobook developed.

Steps to use the tool

  1. Add below dependeccy in your application`s gradle file (Module: app)

'compile 'com.facebook.stetho:stetho:1.4.2'

  1. Add below lines of code in your Activity onCreate() method
@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Stetho.initializeWithDefaults(this);
 setContentView(R.layout.activity_main);
 }

Now, build your application & When the app is running, you can browse your app database, by opening chrome in the url:

chrome://inspect/#devices

Screenshots of the same are as below_

ChromeInspact

ChromeInspact

Your DB

Your DB

Hope this will help to all! :)

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
0
  1. You phone should be rooted. This is an important step!
  2. Go to the adb file location and start command prompt as an admin on that location
  3. run below commands:
adb shell
run-as com.domain.yourapp 

this will ask root access on your phone, allow it.

  1. You will be able to browse files using root access. Thanks!
Nakilon
  • 34,866
  • 14
  • 107
  • 142
0

I know this thread is pretty old but I encountered this problem today while trying to copy my database files from within android studio to my windows to read it. I didn't need to run any commands, in fact nothing at all in that manner. Only thing I did was reconnect the USB plugged to my device and when the prompt that says "use USB for" came up, I selected "file transfer" and it worked! No need for you to have a rooted device to copy its inner files.

Daniel Iroka
  • 103
  • 8
-5

When you are in the shell directory for the device. Just run

su - root

Then you should be able to access the data/ folder.

Petsome
  • 49
  • 8
-6

without rooting the device you can access the files by the following The cmd screenshot:

  1. start the adb using cmd from the platform-tools folder according to your user

    cd C:\Users\YourUserName\AppData\Local\Android\sdk\platform-tools
    

    it can be in another folder depends on where you installed it.

  2. display the working devices.

    adb devices
    

    for say the respond os the command is

    List of devices attached
    0123456789ABCDEF     device
    
  3. write the command

        adb -s 0123456789ABCDEF shell "su"
    

    it will open in

        root@theDeviceName:/ #
    

4.Accessing files, list all the files

ls

and so-on chang directory to any where else like the data folder

cd data/data
Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26