How do I execute an adb command in Android Studio? [duplicate] - android

This question already has answers here:
'adb' is not recognized as an internal or external command, operable program or batch file
(27 answers)
Closed 6 years ago.
For example, I want to execute this command from this tutorial:
$ adb shell monkey -p your.package.name -v 500
How do I do it? Where do I enter the command? I've tried entering it into the terminal but it says that '$' is not recognized.
I also tried removing '$' but it then says that "'adb' is not recognized as an internal or external command,
operable program or batch file."

$ isn't part of the command.
Use adb shell monkey -p your.package.name -v 500
adb is the command you ran on your terminal.
For example:
adb devices
shows you the connected devices.
adb shell
Starts a remote shell in the target emulator/device instance.
See https://developer.android.com/studio/command-line/adb.html for more info about adb.

Related

Where to see the xml file after "adb shell uiautomator dump view.xml" [duplicate]

This question already has answers here:
Is there a way to get current activity's layout and views via adb?
(8 answers)
Closed 5 years ago.
I use command
adb shell uiautomator dump view.xml
to dump the UI hierarchy of current Android screen. Then, I try to see the view.xml, I tried command:
adb shell cat view.xml
but get error:
/system/bin/sh: cat: view.xml: No such file or directory
How/Where can I see the dumped view.xml file?
Why don't you do the following:
adb shell uiautomator dump
adb pull /sdcard/window_dump.xml
If you directly want to read out from shell you can execute following command from adb shell after dumping the XML
adb shell cat /sdcard/window_dump.xml
Being on your developer machine you can dump the "UI hierarchy of current Android screen" to /sdcard/window_dump.xml on the smartphone with command:
adb shell uiautomator dump
the response is:
UI hierchary dumped to: /sdcard/window_dump.xml
then you can cat the xml to your developer machine's filesystem with command:
adb shell cat /sdcard/window_dump.xml > ~/duuuuuuuuuuuump.xml
the you can view it with favorite xml viewer, for example nano:
nano ~/duuuuuuuuuuuump1.xml
Either use absolute path or just use adb shell uiautomator dump without filename parameter at all - it will use the default location. In your case you are trying to create the /view.xml file and you have no writing permission for the / folder.

adb shell command from batch file [duplicate]

This question already has an answer here:
windows batch script not execute next line after "adb shell"
(1 answer)
Closed 5 years ago.
I have a .bat file that runs script for testing an app and printing the log to file i have all the commands i tested them manually.
Problem:
after entering the command adb shell, the shell opens in the command prompt. I wrote the next commands that are entered in the shell with root#generic
the commands aren't going through and it just waits at that spot.
what do i have to type in front of the commands to make them appear
example of what i have
cd directory of sdk
adb
adb shell
am instrumentation ... (this is the command that won't go through once the shell is open.
any help appreciated I've tried a few things with no success
Just use the following line in your batch file:
adb shell am instrumentation
that will connect to the shell on the Android device and run the am command.

Unable to run 'adb root' on a rooted Android phone [duplicate]

This question already has answers here:
adb shell su works but adb root does not
(11 answers)
Closed 5 years ago.
After rooting my device, I need to run adb root and then adb shell so I could then access my applications database. When trying to run adb root I keep getting "adbd cannot run as root in production builds". Why is this? The only other option is to use the Android emulator for testing, but we all know how terrible the emulator is (not really a viable development solution).
I finally found out how to do this! Basically you need to run adb shell first and then while you're in the shell run su, which will switch the shell to run as root!
$: adb shell
$: su
The one problem I still have is that sqlite3 is not installed so the command is not recognized.

Run sqlite3 command from batch file?

I want to make it easier to access the sqlite databases on the emulator, so I've created a batch file that successfully runs the first line, but is there a way to run the second line from the batch file? (At this point adb is already running, so it would be running a command in the shell.)
adb -s emulator-5554 shell
# sqlite3 /data/data/com.myProject/databases/myDatabase
UPDATE:
For some reason
adb -s emulator-5554 shell sqlite3 /data/data/com.myProject/databases/myDatabase
prevents you from using the command line. Typing and pasting both seemed to be blocked. I ran this line in a command line outside of the batch and it does the same thing.
Separate lines work fine:
adb -s emulator-5554 shell
sqlite3 /data/data/com.myProject/databases/myDatabase
but this doesn't work in the batch file (the sqlite3 command gets executed too early).
You can run specific commands other than just an interactive shell on the target device. eg:
adb -s emulator-5554 shell ls /sdcard
or
adb -s emulator-5554 shell sqlite3 /data/data/com..../database.db
Once it gets complicated, it might be better to use a script that you copy over onto the device and run on demand.
You can use input redirection to send a stream of commands to ADB:
adb -s emulator-5554 shell <a.txt
Where a.txt contains
sqlite3 /data/data/com.myProject/databases/myDatabase
logout
This scales better. a.txt can grow as big as you want.

Android ADB access to application databases without root

Can anyone tell me, is it possible to use the ADB to pull and push a database from an app, without root privileges on the phone?
For example, I know the location on my rooted magic and dream is:
/data/data/com.xxxx.xxxx/databases/xxxx
I know that you can use ADB without root, but when trying to use the shell - you can't view that location without root privaliges. But I have been told you can use push and pull if you know the file you want?
Basically I want to pull a database from MY app on a non rooted phone modify it and push it back on.
Only trouble I have is, the two phones I have are both root and I don't have access to a non root one to try it out.
While Nilhcem's answer didn't work for me, it lead me in the right direction (for that I upvoted) and I now have a working solution.
Old answer that may not work with newer versions of Android:
#Transfer file from app databases directory to PC
adb shell
$ run-as package.name
$ cd ./databases/
$ ls -l #Find the current permissions - r=4, w=2, x=1
$ chmod 666 ./dbname.db
$ exit
$ exit
adb pull /data/data/package.name/databases/dbname.db ~/Desktop/
#Transfer file from PC to app databases directory (requires the above permission change)
adb push ~/Desktop/dbname.db /data/data/package.name/databases/dbname.db
adb shell
$ run-as package.name
$ chmod 660 ./databases/dbname.db #Restore original permissions
$ exit
$ exit
Alternate method using external storage (confirmed to work with 6.0.1):
#Transfer file from app databases directory to external storage
adb shell
$ run-as package.name
$ cp ./databases/dbname.db /sdcard/
$ exit
$ exit
#Transfer file from external storage to app databases directory
adb shell
$ run-as package.name
$ cp /sdcard/dbname.db ./databases/
$ exit
$ exit
A quick workaround is to use the run-as command to copy the database in a folder where you can have access, such as /sdcard and then, do a normal adb pull
adb shell
$ run-as package.name cp /data/data/package.name/dbname.db /sdcard/
$ exit
adb pull /sdcard/dbname.db
More information on the run-as command here
Note that the run-as command is available since API level 8 (Android 2.2) and can only be used if the application is debbugable.
On OxygenOS (based on Android 5.2) I've combined the two solutions provided by Pilot_51.
First, I used run-as to gain access to /data/data/package.name/databases, but from here I wasn't able to copy directly to /sdcard/ so I changed the permissions of the file. After that, I exited from run-as mode and used cp to copy the file in /sdcard/ storage. Finally, I was able to use adb pull
$ adb -s <DEVICE_ID> shell
$ run-as package.name
$ chmod 666 databases/dbname.db
$ exit
$ cp /data/data/package.name/databases/dbname.db /sdcard/dbname.db
$ exit
$ adb pull /sdcard/dbname.db ./dbname.db
We set the file permissions to readable for all users from within the app.
if (BuildConfig.DEBUG)
{
new File(mDB.getPath()).setReadable(true, false);
}
Then just pull the .db off with adb normally.
adb -d pull //data/data/xxxxx/databases/xxxxx.db .
NOTE: I've discovered that this needs to be done each time the database file is opened, for example in onCreate as well as the constructor of your SQLiteOpenHelper wrapper (when your database is not null) or perhaps onOpen. If only in onCreate, then the next time you run your app and the .db already exists, for some reason the permissions have been changed back. This might have something to do with how Android manages its data.
if you want to push db file into the application
first of all, place "file.db" under "/storage/emulated/0/" because of permission issue. then you should pretend as application to access data folder.
adb shell
$ run-as com.package.name
:/data/data/com.package.name $ cp /storage/emulated/0/file.db /data/data/com.package.name/databases/
it copies the file.db that in main folder to databases.

Categories

Resources