Is it possible to add a prebuilt APK to an AOSP bu

2020-07-17 06:43发布

问题:

I am looking to add a prebuilt APK to my AOSP build. I can already do this by doing the following:

  1. Add the APK to the $root_of_my_source_tree/packages/apps folder.
  2. Create an Android.mk file. The file has the following contents:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE_TAGS := optional
    
    LOCAL_MODULE := <name of APK>
    
    LOCAL_SRC_FILES := <Name of APK>.apk
    
    LOCAL_MODULE_CLASS := APPS
    
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    
    LOCAL_CERTIFICATE := platform
    
    include $(BUILD_PREBUILT)
    
  3. Add:

    PRODUCT_PACKAGES += \ <name of APK>
    

    To a make file that gets picked up by the AOSP build system.

This adds the APK to the /system/app folder which is read-only so the user will not be able to update the APK. I would like the APK to be placed in the /data/app folder so the user can actually update the prebuilt APK.

I have tried the suggestions in this question and they did not work. I tried the suggestion in this question and while it did output the APK to the /data/app folder of the finished build, once I flashed the image onto my device, the app was not there. The second answer in that question does provide a possible explanation for this but no other suggestions.

Ultimately, I need an APK preloaded in my build that can be updated so if this doesn't work or is a bad approach, I am open to suggestions.

回答1:

I found the standard behavior of the PackageManager class good enough for my use case. So here are a couple tidbits worth explaining:

  • Lavakush's answer does work and it does output the APK to the out/target/product/vendor/data/app folder in the source tree but the APK does not show up under /data/app in the system image once it's flashed onto a device. According to this answer, the Android build system does not build anything into /data/ so that explains that. I still find it confusing that it does show up as output in the out/target/product/vendor/data/app folder however.
  • In my use case, I need to preload APKs onto a build and then update them as needed. Due to the read-only nature of the /system/app folder I assumed that I would not be able to update my app which is why I wanted to preload it to /data/app. As in turns out, according to this answer & to my testing, the PackageInstaller class' standard behavior is to install the updated APK to the data/app folder and to start running this APK instead of the preloaded /system/app APK. The answer also says that the settings->apps screen will give you the option to uninstall the updated APK and then you'll be left with the original system/app APK. I did observe this behavior and it is fine for my use case.


回答2:

In your Android.mk file Use LOCAL_MODULE_PATH to TARGET_OUT_DATA or TARGET_OUT_DATA_APPS

LOCAL_MODULE_PATH := $(TARGET_OUT_DATA)
------
include $(BUILD_PREBUILT)