run shell command programmatically (not rooted) [duplicate] - android

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.

The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

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.

Is there an ADB command to change the advanced settings of an installed application?

I have been trying to figure out a command using ADB command line to change the advanced settings of an application I am installing through ADB.
Once installed I wish to change the following settings to 'allow' by using an ADB command:
Is this possible?
I thought it might be: found here
adb shell pm grant com.mycompany.mypackage INSTALL_NON_MARKET_APPS 1
The command for the Install unknown apps would be:
adb shell pm grant com.company.testapp android.permission.REQUEST_INSTALL_PACKAGES
Which will fail, because android.permission.REQUEST_INSTALL_PACKAGES is not a changeable permission type:
Operation not allowed: java.lang.SecurityException: Permission android.permission.REQUEST_INSTALL_PACKAGES is not a changeable permission type
Maybe there is a way around this, that I don't know of.
You can try:
appops set com.mycompany.mypackage REQUEST_INSTALL_PACKAGES allow
It works for my device with Android 10.

Understanding command through adb shell and through code - Android

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.
The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

Is it possible to run a shell command as root with only the su binary and no SuperSU or Superuser apk installed?

I have an Android phone that has only su binary installed and it works, meaning I can adb shell into the phone and run an 'su' command and I will be root.
When I try to run a command via code, it doesn't seem to work no matter which way I try to run it. I've tried many different variants of the following command.
Runtime.getRuntime().exec("su -c ps");
When I run this command on another rooted phone with a Superuser.apk or SuperSU.apk app installed, I get a dialog asking if I want to allow it to run with root permissions. When the apks are not there, it never asks and the command never works.
I've tried installing the apks on the first phone but they don't seem to do anything. So, as the original question asks --> Is there any way to run the elevated command from within the app without the SU apps installed?
It might be because you need to pass the commands to su as parameters like this:
su -c 'ls -l'
Or you might need to specify the full path to su, but I don't see why it wouldn't work the way you have it:
Runtime.exec("/system/bin/su -c ps")Or maybeRuntime.exec("/system/bin/su -c \'ps\'")
Try checking the output of this command too: System.getenv("PATH")
Another variant could be Runtime.exec("su -c \'ls -s \'")
Make sure you don't forget to escape the single quotes as they are part of the actual String.
Thats the way that I've found works most consistently, and it has also worked on devices that don't have Superuser or SuperSU installed, as those apps only listen for the Broadcast that is sent out when an application tries to run a command as root. #Boardy SuperSU and Superuser intercept the broadcast and so act as a middle man between the app and root privileges, but its not necessary for a rooted device. It IS necessary if you want to have more control over the applications that are running commands as root, but even then it still only limits you to deciding which applications, not which commands, are given root privileges.
Also, you might wanna take a look at RootTools and more specifically, the RootTools.isAccessGiven() command, which requests root privileges for your app.
Source: Launch a script as root through ADB
Not all versions of an 'su' for Android will accept a command to execute from the command line parameters.
Where they do not, you will need to let 'su' launch a privileged shell, obtain its input file descriptor, and pipe command(s) into that. This has been covered numerous times here on Stackoverflow.
I believe you would need to have the SU apps installed as they are what provide the user the question as to whether the app should be allowed to run as root or not.
You should be able to do this.
try :
adb root shell ls -l

Executing a Root access based commands on Android application

I basically want to start a process which resides in /system/bin/... from the Application java command.
I tried all types of Runtime.process.exec() options
Tried the su and ouputStream combination as well, but nothing is able to start the application.
I am using the code in device and emulators, no luck in both.
I am able to run the same commands from ADB Shell successfully (as it has root permissions).
The device is rooted, I am able to see the # when I use adb to debug my device and it also can go into su via adb.
based on my searching I found that (after doing ps from adb shell)
I am able to run any command with lrwz--x--x permission such as (ls->toolbox, ps->toolbox) from the application layer but I am not able to execute any other commands which are not linked to the toolbox.
This doesn't execute ls:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os=new DataOutputStream(p.getOutputStream());
os.writeBytes("ls \n");
os.flush();
But this does execute ls:
Process p = Runtime.getRuntime().exec("ls");
I would really appreciate if I can get any help on this here! I am posting this after doing lots of research.
Rather than sending ls \n to the su command's standard input, try running:
su -c ls
The -c option to su(1) asks it to run a command with elevated privileges rather than starting an interactive shell.
Try creating a shell script file(eg: init.sh) and write following commands in it:
su
reboot
Now try executing this shell script by
Runtime.getRuntime().exec("sh init.sh");
If this restarted your droid then you can write your required code in init.sh file & run it.

Categories

Resources