My app is running as a system app on a custom AOSP image. I am able to uninstall/install other APKs that I'm downloading from my backend. However, I cannot uninstall other system apps (which is a hard requirement for my purpose). My current mechanism is to use the deletePackage mechanism from the Android package manager via reflection:
val cPackageManager = Class.forName("android.content.pm.PackageManager")
cPackageDeleteObserver = Class.forName("android.content.pm.IPackageDeleteObserver")
deletePackage = cPackageManager.getMethod("deletePackage", String::class.java, cPackageDeleteObserver, Integer.TYPE)
deletePackage!!.invoke(context.packageManager, packageName, deleteObserver, DELETE_ALL_USERS)
Does anybody know how to achieve what I'm trying to do?
Don't you need to be a rooted device to uninstall System apps, or have you found a way to do this without any rooting?
You must remount the system, since you run SELinux, to be able to rearrange the locked files in the system.
3.You can run this code in your app instead
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("pm uninstall com.package.name");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
if pm uninstall doesn't work, use the rm -rf path/deletefolder then reboot the system
Related
I have some test built, and my testing department can't figure out how to use the terminal. Uiautomator test are .jar files so must be ran via terminal. So for convenience, I want to make an app for them with the tests in a list to choose from to execute. Is this possible? My research leads me to believe that the devices will need to be rooted. If that is the case I will not be able to do it. So is there a workaround to this? This is what I have tried:
Runtime rt = Runtime.getRuntime();
try {
Process process = rt.exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("uiautomator runtest test.jar -c ui.test.getData\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
This code will produce this :
java.io.IOException: Error running exec(). Command: [su] Working Directory: null Environment: null
Please help me with a way to get this to work. Thanks!
Do you have suggestions of something I could do to make it easy for my testing team to run a test of mine?
Create a desktop app that runs the test.
Or, just put a nice batch file/shell script in front of it. Perhaps one that launches the test and displays a photo of a cute kitten in the tester's Web browser.
(testers love cute kittens)
Or, set up a continuous integration server, so testers are not manually running the tests at all -- the tests are run automatically, and the testers are simply examining the results. I presume that somebody has a recipe for Hudson/Jenkins/whatever that can run an uiautomator test. And there may be a separate recipe for integrating photos of cute kittens into the test result reports, though it is possible that you'll be stuck writing your own for that.
I need to execute a C program in my App by simply adding the executable to the android project and building the .apk. Then I try to execute the program in my application like this:
Process result = Runtime.getRuntime().exec("su");
String cmd = "PROGRAM_NAME";
DataOutputStream dos = new DataOutputStream(result.getOutputStream());
DataInputStreamdis = new DataInputStream(result.getInputStream());
dos.writeBytes(cmd + "\n");
dos.writeBytes("exit\n");
dos.flush();
I know I need root access to do this so I installed Superuser.apk but that didn't work. Is there another possible way to do this? Btw the code is not fully extended it should just give a look at the way the program should be executed
I'm running the emulator with Android 4.2.1
Edit:
Checking root permission first with
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes) {
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid) {
Log.d("ROOT", "Can't get root access or denied by user");
}
I have got the same problem as you. you can find many answers like here , but they are too old and they are not working anymore (in the new sdk).
I found the best answer here which says that the Security Tips of Android are not allowing to any developer to have root access :
A central design point of the Android security architecture is that no application, by default,
has permission to perform any operations that would adversely impact other applications, the
operating system, or the user. This includes reading or writing the user's private data (such as
contacts or e-mails), reading or writing another application's files, performing network access,
keeping the device awake, etc.
So the only access that you have under the Application Layer is by permissions.
I am able to create the multiple user profile on Jellybean using "pm create-user Name" terminal command on Emulator, but I want to know is there any way so I can run the same command programmatically. I don’t want to open the terminal.
When the Multiple User feature was first found in 4.1, I wrote an app to do just this. I've open sourced it here. You can find the code for running that command programmatically in TerminalUtils, but I'll put it in the answer as well.
public static void createUser(String name)
{
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("pm create-user \"" + name + "\"\n");
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
TerminalUtils also contains methods to remove and rename users.
Yes you can by :
Using Stericson RootTools :
RootTools provides rooted developers a standardized set of tools for
use in the development of rooted applications. In the end, we will
accomplish this by providing developers with robust, easy-to-use
libraries that will drastically improve development times as well as
promote code reuse. This project is open to any proven developer that
feels they have something to contribute. By pitching in together we
can streamline our own processes, improve the effectiveness of our
apps, learn new techniques, and provide a better experience for our
users.
Using Android Runtime :
Allows Java applications to interface with the environment in which
they are running. Applications can not create an instance of this
class, but they can get a singleton instance by invoking getRuntime().
I need to remount android file system as read write instead of read only. I know how to do it through command prompt and also how to go about it for application development.
But I am into tweaking the android source code and there if I use
Process proc=Runtime.getRuntime.exec("su");
it doesnot work. Therefore I need to use android.os.process object for the same and I am stuck here can anybody help me with it.
Try this code to get superuser access (you'll need a rooted phone with superuser installed):
try {
// Perform su to get root privledges
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("<..... here you may write commands ....>\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
toastMessage("not root");
}
Once you have superuser access, you may start adbd listening on some TCP port using:
setprop service.adb.tcp.port 5555
adbd &
setprop service.adb.tcp.port -1
Open a TCP connection to the selected port and issue remount command.
Actually, it's difficult to understand what are you going to do. But if you're tweaking android sources (aosp) then it's will be easier for you to modify init.rc (which is under system/core/rootdir) file to get system image rw. If you want to change the state during the runtime you need to create your own native application (for instance, look into the run-as command), change inside it the uid of the process, make it as suid, embed it into the sources, and call it from your application.
I am trying to send terminal commands programmaticly from an android activity. At the moment I'm using something like the following:
Process process = null;
DataOutputStream os = null;
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("./data/program1\n");
os.writeBytes("./data/program2\n");
os.writeBytes("exit\n");
os.flush();
However, my program1 is failing to run successfully and I believe it is due to inadequate user permissions. Now for my question:
Does anyone know how I can dump the terminal to a file and save it on the phone or sdcard? The program is tying into the terminal to feed it commands, I want to know a way to open a connection the otherway and access the (what is normally visual on a terminal screen) output.
See the sources for the Terminal application, as that is bidirectional.
Sadly, running shell commands or launching native processes is not an officially supported part of android, so no future stability is guaranteed.