Why do I get access denied to data folder when using adb?
AndroidAdbAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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).
Solution 2 - Android
There are two things to remember if you want to browse everything on your device.
- 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.
- You need to be running ADB in root mode, do this by executing:
adb root
Solution 3 - Android
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).
Solution 4 - Android
$ 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!
Solution 5 - Android
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.
Solution 6 - Android
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. :)
Solution 7 - Android
Following are the two steps to get root access:
- Your android devices must be rooted.
- In ADB shell, type su and the android device will prompt y/n? You choose allow.
Solution 8 - Android
This works if your Android device is rooted by any means (not sure if it works for non-rooted).
adb shell
- access the shellsu
- become the superuser.
You can now read all files in all directories.
Solution 9 - Android
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.
Solution 10 - Android
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$ 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
> abc@abc-L655:
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..
Solution 11 - Android
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.
Solution 12 - Android
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
- Add below dependeccy in your application`s gradle file (Module: app)
> 'compile 'com.facebook.stetho:stetho:1.4.2'
- 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
> Your DB
Hope this will help to all! :)
Solution 13 - Android
- You phone should be rooted. This is an important step!
- Go to the adb file location and start command prompt as an admin on that location
- run below commands: adb shell run-as com.domain.yourapp this will ask root access on your phone, allow it.
- You will be able to browse files using root access. Thanks!
Solution 14 - Android
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.
Solution 15 - Android
When you are in the shell directory for the device. Just run
su - root
Then you should be able to access the data/ folder.
Solution 16 - Android
without rooting the device you can access the files by the following The cmd screenshot:
-
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.
-
display the working devices.
adb devices
for say the respond os the command is
List of devices attached 0123456789ABCDEF device
-
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