javaandroidandroid-sourceandroid-soong

AOSP Add Jar to Boot Class Path


I've set up the AOSP project, it builds fine (Android 12).

I've created a new module under frameworks/base/mylib which is just a Java library. It builds fine and gets added to /system/framework/mylib.jar. The jar contains a classes.dex which contains my classes.

I now want to add this jar to the boot class path so any app can access the classes inside of it.

I've tried adding PRODUCT_BOOT_JARS += com.example.mylib to my target product, build/target/product/sdk_phone_arm64.mk.

This causes the build to generate the following files in the framework folder:

arm64/boot-com.example.mylib.art
arm64/boot-com.example.mylib.oat
arm64/boot-com.example.mylib.vdex

boot-com.example.mylib.vdex

The jar now contains an empty classes.dex. When I try accessing a class in my app, I am getting the following errors:

Accessing hidden field Lcom/example/mylib/MyClass;->
MY_STRING:Ljava/lang/String; (blocked, linking, denied)

java.lang.NoSuchFieldError: No static field MY_STRING of type Ljava/lang/String; 
in class Lcom/example/mylib/MyClass; or its superclasses 
(declaration of 'com.example.mylib.MyClass' appears in 
/system/framework/com.example.mylib.jar)

This seems to be due to the non SDK interface restrictions of Android.

How do I get around this, if I need to add it to the SDK where can I do that?

I am aware that using the <uses-library> will add the library to the app's class path, but I'd like to avoid app's needing to add this to their Manifest files and just make it globally available.


Solution

  • Any non SDK interface is automatically "hidden", and can only be accessed in some specific scenarios AndroidPackageUtils

    1. Platform Signed Apps. If an app is signed with the same signing key as the system image, it will be allowed to access hidden APIs.
    2. Whitelisted System Apps: If a system app (that is installed as part of the system image build) is added to the hiddenapi-package-whitelist.xml file, then that system app will be allowed to access hidden APIs.
    3. With AndroidManifest tag: If a system app includes the tag android:usesNonSdkApi="true" in it’s Manifest, then that system app will be allowed to access hidden APIs.

    Any other way would involve extensive modification of AOSP SDK interfaces, and also requires modifying build/soong/scripts/check_boot_jars/package_allowed_list.txt