Target

Pre install android app into android ROM

Prepare AOSP build environment

Follow steps of AOSP setup.

Our sample environment:

branch: master (repo init -u https://android.googlesource.com/platform/manifest)

target: aosp_x86-eng

device: emulator

Init env every time open a new terminal:

source ./build/envsetup.sh

lunch aosp_x86-eng

Build an APK for pre-install

Use Android Studio to build an apk. I built a TestMan.apk.

Place apk to aosp pre-install apps

  • Make a dir TestMan in AOSP_WORKSPACE/packages/apps.

  • Put TestMan.apk here.

  • Make an Android.mk in AOSP_WORKSPACE/packages/apps/TestMan.

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PRIVILEGED_MODULE := true
LOCAL_MODULE := TestMan
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
LOCAL_MODULE_PATH := $(TARGET_OUT_APPS)
include $(BUILD_PREBUILT)

Each line should not have trailing spaces, or you would get error Invalid characters in module stem \(LOCAL_INSTALLED_MODULE_STEM\) when you build.

Modify makefile

Open AOSP_WORKSPACE/build/target/product/aosp_x86.mk.

aosp_x86.mk:

# GSI for system/product
$(call inherit-product, $(SRC_TARGET_DIR)/product/gsi_common.mk)

# Emulator for vendor
$(call inherit-product-if-exists, device/generic/goldfish/x86-vendor.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/product/emulator_vendor.mk)
$(call inherit-product, $(SRC_TARGET_DIR)/board/generic_x86/device.mk)

# Enable mainline checking for excat this product name
ifeq (aosp_x86,$(TARGET_PRODUCT))
PRODUCT_ENFORCE_ARTIFACT_PATH_REQUIREMENTS := relaxed
endif

PRODUCT_NAME := aosp_x86
PRODUCT_DEVICE := generic_x86
PRODUCT_BRAND := Android
PRODUCT_MODEL := AOSP on x86

We can see there is $(SRC_TARGET_DIR)/product/gsi_common.mk which every {TARGET} .mk files call.

Then we need to add TestMan to Default AOSP packages in makefile.

Open AOSP_WORKSPACE/build/target/product/gsi_common.mk

We can see

# Default AOSP packages
PRODUCT_PACKAGES += \
    PhotoTable \
    WAPPushManager \

Add TestMan here

# Default AOSP packages
PRODUCT_PACKAGES += \
    PhotoTable \
    WAPPushManager \
    TestMan \

Add TestMan to whitelist in gsi_common.mk.

# The mainline checking whitelist, should be clean up
PRODUCT_ARTIFACT_PATH_REQUIREMENT_WHITELIST := \
    system/app/messaging/messaging.apk \
    system/app/PhotoTable/PhotoTable.apk \
    system/app/WAPPushManager/WAPPushManager.apk \
    system/app/TestMan/TestMan.apk \
    system/bin/healthd \
    system/etc/init/healthd.rc \
    system/etc/seccomp_policy/crash_dump.%.policy \
    system/etc/seccomp_policy/mediacodec.policy \
    system/etc/vintf/manifest/manifest_healthd.xml \
    system/lib/libframesequence.so \
    system/lib/libgiftranscode.so \
    system/lib64/libframesequence.so \
    system/lib64/libgiftranscode.so \
    system/priv-app/Dialer/Dialer.apk \

Build

  • Go to AOSP_WORKSPACE, run make -j8 or make -j16 … (depend on your CPU cores)

  • If you got #### build completed successfully (02:17 (mm:ss)) ####, run emulator, you can see TestMan is already an in pre-install app which cannot be uninstall.

How to replace default launcher with customized launcher

Find default launcher of AOSP

Launcher is also a pre-install app, so we can find it under AOSP_WORKSPACE/packages/apps/.

We can see there are Luncher2 and Luncher3, why there are two launchers?

We can check their Android.mk to see what’s going on. Launcher2 does not have Android.mk, but Launcher3 has.

We can see there is LOCAL_OVERRIDES_PACKAGES := Home Launcher2 Launcher3 Launcher3QuickStep. Check what is LOCAL_OVERRIDES_PACKAGES.

From How do I add APKs in an AOSP build? we know that Launcher3 module hide Home Launcher2 Launcher3 Launcher3QuickStep.

So why does Launcher3 hide itself? Check Android.mk again we can also see LOCAL_PACKAGE_NAME := Launcher3QuickStepGo.

From Android.mk we see:

LOCAL_PACKAGE_NAME is the name of an app. For example, Dialer, Contacts, etc.

LOCAL_MODULE is the name of what’s supposed to be generated from your Android.mk. For exmample, for libkjs, the LOCAL_MODULE is “libkjs” (the build system adds the appropriate suffix – .so .dylib .dll). For app modules, use LOCAL_PACKAGE_NAME instead of LOCAL_MODULE.

So we should always use LOCAL_PACKAGE_NAME when pre-install module is an App and build with source code. But if we use apk instead of source code, we should always use LOCAL_MODULE, otherwise you’ll get error when build.

Finally, we can see Launcher3’s real name is Launcher3QuickStepGo, and there are other LOCAL_PACKAGE_NAME in Android.mk of Launcher3: Launcher3, Launcher3Go, Launcher3QuickStep. We should use LOCAL_OVERRIDES_PACKAGES := Home Launcher2 Launcher3 Launcher3QuickStep Launcher3QuickStepGo in our own launcher app’s Android.mk to hide them all.

Build new launcher apk to replace default launcher

Then we need to build a launcher apk and place it to AOSP_WORKSPACE/packages/apps/ with the same way of pre-install common apk metioned above. Modify gsi_common.mk, add it to product_packages and whitelist.

Android.mk of NewLauncher:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PRIVILEGED_MODULE := true
LOCAL_MODULE := NewLauncher
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
LOCAL_MODULE_PATH := $(TARGET_OUT_APPS)
LOCAL_OVERRIDES_PACKAGES := Home Launcher2 Launcher3 Launcher3Go Launcher3QuickStep Launcher3QuickStepGo
include $(BUILD_PREBUILT)

Build

  • Run make installclean to remove default launcher apk which generated under AOSP_WORKSPACE/out directory.

  • Run make -j8

  • Run emulator to see new launcher after boot.

(If you do not hide default launcher with LOCAL_OVERRIDES_PACKAGES, you will need to select a launcher after boot.)

Clean

If you remove pre install app, you need to run make installclean before make.

If you want to clear app data, you need to run make dataclean before make.

References

AOSP setup

How do I add APKs in an AOSP build?

How do I set the default launcher in an AOSP build?

Android.mk