Developing System Apps using an emulator
Deploying systems apps on your AOSP emulator
Hope you are able to build custom AOSP emulator as described in the below article :
Now is the time to deploy applications as system apps to this customized or any existing emulator.
Android Emulator Setup to develop System applications
The Develop → Test → Debug cycle for an android system application is quite longer than the normal app development cycle. This article describes the emulator setup and a few handy scripts to facilitate a faster development cycle.
Step 1 — Setting up the emulator
If you are using Android Studio like me, the AVD manager of the android studio comes with plenty of preconfigured AVD options to create a virtual device. But most of them are targeted for general app development. we really need to pay attention while creating a virtual device for system apps development. You are free to select any device configuration that matches your requirement. But while selecting a system image for the virtual device, we need to select an image which DOES NOT include “Google APIs”. This is because, if you use images with “Google APIs” we will not be able to sign our system applications with the same key as Google API since it is not available to us. Whereas the plain AOSP images without Google APIs, we can obtain the platform keys from AOSP repository and we can use it to sign our system applications. The image highlighted in green is the right one for AOSP Oreo 8.1.
In case if you have an emulator with “Google API” and try to sign your system app with AOSP platform keys, be assured that it will NOT work.
Step 2 — The Applications Manifest
If you are intent to run your app as a system process, then it is important to include android:sharedUserId attribute in the AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
package="<YourPackageNameHere>">
Once you add this attribute, please note that this application can only be installed as a system app. If you try to install this as a normal app, it will fail.
Step 3 — Writing to System partition of the emulator
In order to make an app as a system app, it needs to be installed in “/system/app/” folder. The system partition of the emulator is read-only and you may not be able to do an adb push directly. To make the system partition writable, we need to restart the emulator with “-writable-system” flag
emulator -avd <YourAVDNameHere> -writable-system
Once the emulator is restarted in writable system mode, use the following snippet to sign your app with AOSP keys and deploy it to system
adb root
adb remount
apksigner sign --key platform.pk8 --cert platform.x509.pem <YourAPK>
adb shell mkdir /system/app/<YourAppFolder>
adb push <YourAPK> /system/app/<YourAppFolder>/
adb reboot
The above snippet will install your app as a system app and once the emulator reboots you can see your app installed as a system app.
To uninstall the app, we simply need to remove the Applications folder from “/system/app/”. A sample snippet is shown below
adb root
adb remount
adb shell rm -rf /system/app/<YourAppFolder>
adb reboot
Step 4 — Confirming System app status
How do we verify whether our application is indeed running as a system application?
There are a couple of ways, one is that if you open the “App Info” of the application, there will not be any provision to “Uninstall” it since it is a system application. But the foolproof way is to add a debug log to print the Process’s uid once the application is running. Add this code snippet in your Activity’s code
ApplicationInfo ai = getApplicationInfo();
Log.e(TAG,"Application UID: " + ai.uid + ", SYSTEM_UID: " + Process.SYSTEM_UID);
If your application is running as a system process, then it would print
Application UID: 1000, SYSTEM_UID: 1000
which means your process’s uid matches to that of Process.SYSTEM_UID. If it matches then congratulations your application is a system application now.