Search code examples
androidjar

Resources from jar available from source code but not in xml


I've compiled SettingsLib and copied jar to my project: frameworks/base/packages/SettingsLib/SettingsLib/android_common/javac/SettingsLib.jar

Jar contains this drawable that I would like to use. It's accessible in Java code:

int id = com.android.settingslib.R.drawable.avatar_selector;

but when I try to use it in xml files:

android:drawable="@drawable/avatar_selector"

it fails due to: AAPT: error: resource drawable/avatar_selector not found.


Solution

  • Ok, I've figured it out myself. The procedure is ugly so no wonder there is no info about it from Google developers, maybe they were ashamed of the process and preferred to keep this for themselves :D If there is better way to do that then please let me know.

    First of all if you just build Android with "make" you will not have aar files in out directory, just the jars (maybe with some small exceptions). What you need to do is to "make " e.g. "make SettingsLib" (weird but this is how it currently is), this will additionally generate aar here:

    out/soong/.intermediates/frameworks/base/packages/SettingsLib/SettingsLib/android_common/SettingsLib.aar
    

    but it's not the end of the story ... If you unzip and compare SettingsLib.jar with SettingsLib.aar you will notice that jar contains:

    META-INF, androidx, com
    

    while aar contains:

    META-INF, AndroidManifest.xml, classes.jar, R.txt
    

    but ... if you open "magic" aar that Google put into source code e.g. prebuilts/sdk/current/aaos-libs/car-apps-common.aar you will notice that it additionally contains res directory:

    META-INF, res, AndroidManifest.xml, classes.jar, R.txt
    

    so what you need to do is to unzip generated aar (in this case SettingsLib.aar), then copy paste resources (in this case frameworks/base/packages/SettingsLib/res), zip the files once again, change extension to .aar and finally resources will be available also from the xml files.

    You might also encounter some problems with duplicated strings or binary resources conflicting with the normal ones and you might need to clean project when rebuilding but other than that it should work.