4

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.

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • "once I flashed the image onto my device, the app was not there" -- what does this mean? Do you mean that the file was missing from `/data/app`? Do you mean that the app was in `/data/app` but did not show up in Settings? Do you mean that the app was in `/data/app`, showed up in Settings, but did not show up in the home screen launcher? Do you mean something else? – CommonsWare Jul 05 '17 at 16:39
  • 1
    1. When I run my build script for my AOSP image, I get two types of output: a *system.img* file and a bunch of files in the *out/target/product//* folder. The files in the folder are usually representative of what will be loaded to the device once the system image has finished flashing. For example, if my app is in the *out/target/product//system/app* folder, I am 99.99% sure that the app will be loaded to the device once the system image is flashed and it will be located under the */system/app* folder. – Daniel Jul 05 '17 at 16:56
  • 2. My issue is that I need to load my app under */data/app* so it can be deleted or updated as needed. The answer below does generate the app in *out/target/product//data/app/* instead of *out/target/product//system/app/* but once I am done flashing the system image the app is not there. – Daniel Jul 05 '17 at 16:58
  • "but once I am done flashing the system image the app is not there" -- no offense, but your two comments, in the end, repeat the same passage that I inquired about in my original comment. Please explain, **in precise technical terms**, what "the app is not there" means. – CommonsWare Jul 05 '17 at 17:24
  • The APK is missing from *data/app*. The app is not under settings, and it is not in the launcher. With that said, I have found out enough information to answer my own question. I am posting an answer below. – Daniel Jul 05 '17 at 19:28
  • The AOSP's default behavior is to not pack userdata.img into final build – Saleh Dec 13 '18 at 20:07
  • Related: https://stackoverflow.com/questions/6249458/pre-installing-android-application – Ciro Santilli OurBigBook.com Jan 31 '19 at 09:39

2 Answers2

3

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.
Daniel
  • 2,355
  • 9
  • 23
  • 30
  • Daniel, I also want to install an apk file as a user app so that the user can uninstall. Can you share more detail? – Gary Chen Feb 25 '20 at 15:00
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)
Lavakush
  • 1,910
  • 2
  • 11
  • 14
  • 1
    Both did produce a system image with the APK in the *data/app* folder but once I flashed the image, the app was not loaded. Do you know why this happened? Or Do you know any other approach to preload an APK as an user app? – Daniel Jul 05 '17 at 16:36