Something related to the System App

You might wonder how to deploy your App as a system application. There are two cases we can talk about. First one, on a rooted device, you can simply put your App into either /system/app or /system/priv-app by using this way:

1
2
3
4
5
6
7
$adb root 
$adb remount
$adb push your_app.apk /sdcard/

$adb shell
$su
$cd /sdcard/ & mv your_app.apk /system/priv-app

This is an easier way to make your application access @systemApi without pain. What about the second case? We can download the AOSP source code, put our application into the build process, and make the App a system application when building the AOSP source.

Where to put your App

You can put your App in these two folders:

  • your_aosp_project_folder/packages/apps/your_app_folder
  • your_aosp_project_folder/vendor/your_project/packages/apps/your_app_folder

For the last folder, you have to create that yourself by using:

1
mkdir -p vendor/your_project/packages/apps/your_app_folder. 

You can simply put a prebuilt Apk or the entire project source code into the AOSP build. I will simply go over the prebuilt Apk way, but the concepts are the same. Here is the example of preparing the Android.mk or Android.bp for the AOSP build.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_UNINSTALLABLE_MODULE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_APPS)

LOCAL_MODULE := your_app_folder_name
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk

LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

# If you simply wanted to include your App into the AOSP, use PRESIGNED.
# If you wanted to make your App as a system App (Android.uid.system), use platform
LOCAL_CERTIFICATE := PRESIGNED

include $(BUILD_PREBUILT)
The key and Keystore

Please note that if you want to make your application a system App, you will need to add android.uid.system to your AndroidManifest.xml. This setting will make your application share the same system uid with other system applications. And by using this way, you will need to provide the platform key to sign your application:

Manually sign

1
java -Djava.library.path=out/host/linux-x86/lib64 -jar out/host/linux-x86/framework/signapk.jar build/target/product/security/platform.x509.pem build/target/product/security/platform.pk8 your_debug_app.apk your_debug_app_signed.apk

Sign your app by using Android Studio

1
keytool-importkeypair -k ~/.android/platform.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform

After creating the Keystore file, you can use this Keystore file in your Android Studio and set up the SignConfig to sign your application.

signingConfigs {   
    release {   
        storeFile file("your.keystore")   
        storePassword 'your_store_pwd'  
        keyAlias 'your_key_alias'  
        keyPassword 'your_key_pwd'  
    }   
}