android : unable to run shell script(.sh) from my application - android

i am not able to run shell scripts from my application.only "ls -l" and echo commands respond.rest dont seem to do anything.not even "touch" command works.i tried creating a file in sdcard using touch command in my script..only the echo command worked.my shell script is in sdcard too

The sdcard is mounted with noexec. You will have to copy the scripts to the app's local data drive to execute.
You should be able to see the mounted permissions of all file-systems by logging into the phone (adb shell or whatever else) and typing mount

The rest of the commands like "cd" etc doesnot work in Android as it encounters lot of permission issues.
So the best solution is to trigger "script manager" or "terminal emulator" apps already available through your app.

Related

Which Android Emulator image do contain 'su'?

How to know which Emulator image will contain su?
I can see that often it only contains su or google play
Run adb root and you get a root shell ... otherwise su needs to be manually installed.
However, when already having a root shell available, installing it isn't much of a problem.
All images are rooted, but SDK apps requesting escalation of privileges do rely upon su.
This question here generally duplicates: How to get root access on Android emulator?
adb shell scripting must:
start the emulator
run adb root
run adb shell
remount system partition
adb push the su binary
chmod to set permissions
exit
Or to answer the question:
start the emulator
and check if the file exists, eg. with adb shell stat /usr/bin/su
Where stat gives this response, when it's not installed:
stat: '/usr/bin/su': No such file or directory
One could even loop all Android images installed in $ANDROID_SDK_HOME...
there's no "one click" solution, but adb can be fully automated with Bash or Batch. And one wouldn't even have to run the emulator, but can mount QCOW2 as a nbd network block device.

Why does data folder in android simulator deny permission?

I tried to create database file into
C:\Users\cammm\AppData\Local\Android\Sdk/platform-tools/data/data/com.cookandroid.cammm
Here are steps that I tried to get into the folder
click on "cmd" and run as administrator
cd C:\Users\cammm\AppData\Local\Android\Sdk/platform-tools
adb root
adb shell cd /data/data/com.cookandroid.cammm
Now this pops up: "/system/bin/sh: cd: /data/data/com.cookandroid.adb: Permission denied"
I used "ls" in "platform-tools" folder and saw that it was
"drwxrwx--x 37 system system 4096 2018-07-18 15:46 data"
which means file can only execute but not write or read in others.
using chmod did not work. (chmod: chmod '/data/data/com.cookandroid.adb' to 40702: Operation not permitted)
run as administor does not work
What should I do?
You will only be able to access the data/data folder if the device which you are accessing is rooted. when you have the device rooted then it will show # as prompt on the command line. so Make sure you have the rooted device to execute the above steps.
Please note: It's not advisable to use the rooted devices to carry out any function of the application as it's simply a hack of the device so you have to understand the risk associated with it.

Copy folder from Android app to local Windows directory

I'm trying to use the Android Adb Command Prompt to copy a folder inside the app container to a local Windows folder. The device is running Android 5.1.1 and is not rooted.
adb pull or cp aren't working. How can I copy a folder?
The following approaches aren't working:
Approach 1
adb shell
adb pull /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs C:/temp/test
error: device not found
Inside the shell you can't see to do adb pull. See here.
Approach 2
DDMS can't access the data folder.
Approach 3
adb shell
run-as DroidSample.DroidSample
cp /files/MetroLog/MetroLogs/ C:/temp/test
cp: /files/MetroLog/MetroLogs/: No such file or directory
Approach 4
adb shell
run-as DroidSample.DroidSample
cp /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs/ C:/temp/test
cp: /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs is a directory (not copied).
This is also not working.
Approach 5
adb shell
run-as DroidSample.DroidSample
chmod 777 /files/MetroLog/MetroLogs
exit
exit
adb pull /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs C:/temp/test
adb shell run-as DroidSample.DroidSample
chmod 700 /files/MetroLog/Metrologs
remote object '/data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs' does not exist
So also this isn't working.
Approach 6
adb shell
mkdir /sdcard/tmp
cp /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs /sdcard/tmp
cp: /data/data/DroidSample.DroidSample/files/MetroLog/MetroLogs: Permission denied
This is also not working.
Approach 7
The only thing which half work is this
adb exec-out run-as DroidSample.DroidSample cat "files/MetroLog/MetroLogs/Log - 20160509.log" > C:/temp/test/test.log
But here I don't get the original file and I also have to know the exact file name. Additionally, that I loose line breaks and I have to do this for each file. Not that what I want.
So I'm running out of ideas. How can I access the internal stored files and copy them over?
You have almost solved the problem. As the storage of this kind is secured, you need to do one additional step. You need to copy the file from secured location to sdcard of the device. And then you can copy it anywhere via usb or android pull. Here are the command sequence I executed successfully.
adb shell
run-as DroidSample.DroidSample
cd shared_prefs
cp DroidSample.DroidSample_preferences.xml /sdcard/DroidSample.DroidSample_preferences.xml
exit
exit
adb pull /sdcard/DroidSample.DroidSample_preferences.xml C:/test/
That's it.
And I really appreciate the way you posted your question. Best of luck.
You're trying to gain read access to /data partition on actual android device. Such thing is not possible without root access, even if the app folder is yours. For the reason that permissions to read /data partition are not granted and cannot be granted, unless you're using an emulator. On emulator, which by default is with admin privileges for developer, you can access the data partition to read and write. On actual device you cannot. Not with adb, not with DDMS.
So basically speaking, anything that requires access to those files under /data is not going to work. Whether you sue cp command or pull command. The moment your kernel reads the beginning of your path which starts with /data/... it says: Oops, no can do.
You are trying to access /data folder of android device which is not accessible in unrooted device.

How to use adb command to push a file on device without sd card

How to push a file from computer to an Android device having no SD Card in it. I tried:
C:\anand>adb push anand.jpg /data/local
3399 KB/s (111387 bytes in 0.032s)
C:\anand>adb push anand.jpg /data/opt
3199 KB/s (111387 bytes in 0.034s)
C:\anand>adb push anand.jpg /data/tmp
3884 KB/s (111387 bytes in 0.028s)
Above commands to move a file anand.jpg to a device but I didn't get this jpg file in the device.
I didn't get any success result on cmd prompt, I only got:
3399 KB/s (111387 bytes in 0.032s).
From Ubuntu/Mac Terminal, the below command should work.
./adb push '/home/hardik.trivedi/Downloads/one.jpg' '/data/local/'
For adb v33 and above if you are getting a permission denied error, try what I tried. The following command and it works fine.
The only caveat is you might need to use tmp directory on such an emulator.
adb shell //Entering into shell
su //Super user mode
chmod 777 /data/local/tmp/ //Grantint RWX access
exit
chmod 777 /data/local/tmp/ //Grantint RWX access
exit
And then try
./adb push '/home/hardik.trivedi/Downloads/one.jpg' '/data/local/tmp/'
I did it using the push command, which has syntax:
adb push filename.extension /sdcard/0/
Example of copying directory, and sub-directory content:
adb push C:\my-location\data\. /storage/emulated/0/Android/data
Note that push did just hang in latest platform-tools (33.0.1, at time of writing) for a certain amount of files, beside the adb.exe suddenly taking 5MB+ instead of 1.5MB, hence I just did replace the adb.exe with one I had from 28.0.0 version of platform-tools (I did not downgrade the entire platform-tools, because adb.exe is kind of stand-alone).
I don't say there is any conspiracy around data folder,
But my Samsung device puts limits on my USB file transfer, beside Android v11+ not allowing access to data folder anymore, hence I needed above command to work with 100% speed (without hanging one hour for little more files).
Follow these steps :
go to Android Sdk then 'platform-tools' path on your Terminal or Console
(on mac, default path is : /Users/USERNAME/Library/Android/sdk/platform-tools)
To check the SDCards(External and Internal) installed on your device fire these commands :
1) ./adb shell (hit return/enter)
2) cd -(hit return/enter)
now you will see the list of Directories and files from your android device
there you may find /sdcard as well as /storage
3) cd /storage (hit return/enter)
4) ls (hit return/enter)
you may see sdcard0 (generally sdcard0 is internal storage) and sdcard1 (if External SDCard is present)
5) exit (hit return/enter)
to come out of adb shell
6) ./adb push '/Users/SML/Documents/filename.zip'
/storage/sdcard0/path_to_store/ (hit return/enter)
to copy file
Sometimes you need the extension,
adb push file.zip /sdcard/file.zip
run below command firstly
adb root
adb remount
Then execute what you input previously
C:\anand>adb push anand.jpg /data/local
C:\anand>adb push anand.jpg /data/opt
C:\anand>adb push anand.jpg /data/tmp
After Trying all the answers this worked for me
Where I am Pushing a file on Desktop to Android Device (Redmi K20 pro) connected Over the air using adb.
This command pushes the file to the downloads folder on my phone
adb push ~/Desktop/notifications.drawio ./storage/emulated/0/Download
after running this if you get a permission denied error
try running these commands in order (which basically changes the directory permission)
adb shell
chmod 777 /data/local/tmp
exit
and then run try the adb push command
I have documented this here feel free to share your views and help improve it.
Try this to push in Internal storage.
adb push my-file.apk ./storage/emulated/0/
Works in One plus device, without SD card.
My solution (example with a random mp4 video file):
Set a file to device:
adb push /home/myuser/myVideoFile.mp4 /storage/emulated/legacy/
Get a file from device:
adb pull /storage/emulated/legacy/myVideoFile.mp4
For retrieve the path in the code:
String myFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myVideoFile.mp4";
This is all. This solution doesn't give permission problems and it works fine.
Last point: I wanted to change the video metadata information. If you want to write into your device you should change the permission in the AndroidManifest.xml. Add this line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I've got a Nexus 4, that is without external storage. However Android thinks to have one because it mount a separated partition called "storage", mounted in "/storage/emulated/legacy", so try pushing there: adb push anand.jpg /storage/emulated/legacy
As there are different paths for different versions. Here is a generic solution:
Find the path...
Enter adb shell in command line.
Then ls and Enter.
Now you'll see the files and directories of Android device. Now with combination of ls and cd dirName find the path to the Internal or External storage.
In the root directory, the directories names will be like mnt, sdcard, emulator0, etc
Example: adb push file.txt mnt/sdcard/myDir/Projects/
This might be the best answer you'll may read.
Setup Android Studio
Then just go to view & Open Device Explorer.
Right-click on the folder & just upload a file.
In Mac: To push files via adb
adb push /Users/Downloads⁩/amazon.sdktester.json '/mnt/sdcard/amazon.sdktester.json'
You are trying to write to system folders. With ADB you have root (admin) access so you see the system folders of which sdcard is one of them so to send a picture you could use
D:\Program Files\Android\sdk\platform-tools\adb push am files\android sdk\adb.exe push C:\Downloads\anand.jpg /sdcard/pictures/
NB: C:\Downloads\anand.jpg replace with path and name to picture..
Certain versions of android do not fire proper tasks for updating the state of file system.
You could trigger an explicit intent for updating the status of the file system.
(I just tested after being in the same OP's situation)
adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///
(You could pass a specific filepath instead of file:/// like file:///sdcard )
In my case, I had an already removed SDCard still registered in Android.
So I longpressed the entry for my old SDCard under:
Settings | Storage & USB
and selected "Forget".
Afterwards a normal
adb push myfile.zip /sdcard/
worked fine.
To push all the files at your directory to the Android device use:
PS D:\myFiles> adb push . '/data/local/tmp/'

redirect adb logcat to android avd

i want to redirect the Android developer tools logcat output to a file into the virtual device from a shell command, before than running the android app test.
The command i usually use to redirect the output to a file is:
adb shell logcat -v time - f log.txt packageName:F *:E > /folder/log.txt
but it puts the log file into a computer directory (/folder/ in this case).
I want to change it with a directory in the virtual device but like above, it says the folder does not exist.
There is way to do it via shell command?
You can simply do
adb shell "logcat -v time -f /mnt/sdcard/log.txt packageName:F *:E"
to accomplish it all in one command from the host shell. You do not need the redirect when you use the -f flag, in fact the redirect would not capture anything if you have directed the output of logcat to a file rather than to stdout.
If that is not working, it is either because you are using a version of Android which mounts the external storage at some other path, or you do not have an emulated sdcard attached to your virtual device.
You can investigate either of these problems by examining the output of
adb shell mount
If you do not have an sdcard at all on your AVD, follow the emulator documentation instructions for creating and attaching one.
For testing purposes only there may be other paths than the sdcard at which you can write, particularly on an emulator where the adb shell runs as root, for example on some versions /data/local or similar.
You can try this one adb shell then #logcat>/sdcard/log.txt now i am sure about the results.you just need a command prompt window to be opened for adb shell,that's not so bad i guess.

Categories

Resources