How to install an app to /system partition ?
Note : This article is for you if you’re making an app for rooted android devices.
The normal behaviour being that the android’s package manager installs the apk file to /data partition. But for accessing hidden APIs and having extra privileges one may want the app to be installed in /system partition. For pre-kitkat devices the extra privileges folder is /system/app whereas for kitkat and post-kitkat devices the extra privileges folder is /system/priv-app.
The trick to install your app to /system partition is to first install it the normal way, then on the first run of the app, move the apk to /system/priv-app folder (/system/app for pre-kitkat devices). The following snippet of code makes your life easy to achieve this.
1 2 3 4 5 6 7 8 9 |
private static final String INSTALL_SCRIPT = "mount -o rw,remount /system\n" + "cat %s > /system/priv-app/RemoteDroid.apk.tmp\n" + "chmod 644 /system/priv-app/RemoteDroid.apk.tmp\n" + "pm uninstall %s\n" + "mv /system/priv-app/RemoteDroid.apk.tmp /system/priv-app/RemoteDroid.apk\n" + "pm install -r /system/priv-app/RemoteDroid.apk\n" + "sleep 3\n" + "am start -n in.tosc.remotedroid.app/.MainActivity\n"; |
Use String.format() in your code to format the above commands as shown :
1 2 3 4 |
String.format(INSTALL_SCRIPT, new String[] { MainActivity.this.getPackageCodePath(), MainActivity.this.getPackageName() }); |
Execute the above formatted String after replacing the apk name, package name and the activity name to yours as shown :
1 2 3 4 |
Shell.SU.run(String.format(INSTALL_SCRIPT, new String[] { MainActivity.this.getPackageCodePath(), MainActivity.this.getPackageName() })); |
I use libsuperuser to execute the SU commands in android. The way how to execute these commands is trivial and totally up to you.
Working example : https://github.com/omerjerk/RemoteDroid/blob/master/app/src/main/java/in/omerjerk/remotedroid/app/MainActivity.java#L31