I am doing android custom ROM development now. the build system of aosp is based on Android.mk, But I want to include some aar libraries, is it possible to include aar libaries in Android.mk ?
3 Answers
You should add following blocks into your Android.mk
LOCAL_STATIC_JAVA_AAR_LIBRARIES:= <aar alias>
.
.
.
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := <aar alias>:libs/<lib file>.aar
include $(BUILD_MULTI_PREBUILT)
Please also be aware of satisfy minSdkVersion required by library into your manifest file.

- 166
- 2
- 4
-
1I doubt this can use resources in aar. – Juude Jan 05 '16 at 08:24
-
Is there any solution about this? – bitristan Jan 05 '16 at 08:27
-
According to Google AOSP build system implementation - when you use LOCAL_STATIC_JAVA_AAR_LIBRARIES instead of LOCAL_STATIC_JAVA_LIBRARIES - the only difference is resources. It has put resources aside and combine them with your package resources on package linking stage. – Kostiantyn Luzan Jan 12 '16 at 17:50
-
3For others coming here - Please check @qhdwangnan answer below, this answer alone will not suffice to solve the problem. – bluefalcon Feb 02 '17 at 04:43
-
Following your answer, I get this error : ninja: error: 'out/target/common/obj/JAVA_LIBRARIES/
_intermediates/aar/classes.jar', needed by 'out/target/common/obj/APPS/AppName_intermediates/AndroidManifest.xml', missing and no known rule to make it – SoulaimenK Feb 13 '19 at 16:29 -
@SoulaimenK Could you provide your Android.mk file, maybe you miss something? – Kostiantyn Luzan Feb 14 '19 at 17:18
-
@KostiantynLuzan : Could you check my comment below – SoulaimenK Feb 15 '19 at 09:26
Kostiantyn Luzan's answer has a problem. After compile, the resources in the aar will be added to my main package's R file, but not in the aar package's R file. For example, the aar's package name is my.aar, the main project's package name is my.main. The aar has a string named "string_in_aar". After compile, the strings id is my.main.R.string_in_aar rather than my.aar.R.string_in_aar. This makes the apk crash, because the code in the aar uses my.aar.R.string_in_aar.
The solution is use: LOCAL_AAPT_FLAGS += --extra-packages {aar package name}. You will get two R file. They has the some content. One's package is the main package, the other is the aar package.

- 175
- 1
- 5
-
Does anyone have a solution for how to include the AAR library and resoures when you have LOCAL_USE_AAPT := true set as well? – Keith Oct 05 '18 at 18:54
-
-
@qhdwangnan can you please provide sample Androd.mk file for more understanding... – Gowthaman M Oct 11 '19 at 04:22
My Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-appcompat
LOCAL_STATIC_JAVA_LIBRARIES += android-common
LOCAL_SRC_FILES := $(call all-java-files-under, app/src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/app/src/main/res
LOCAL_RESOURCE_DIR += prebuilts/sdk/current/support/v7/appcompat/res
LOCAL_MANIFEST_FILE := app/src/main/AndroidManifest.xml
LOCAL_PACKAGE_NAME := PackageName
LOCAL_SDK_VERSION := current
LOCAL_PROGUARD_FLAG_FILES := app/proguard-rules.pro
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.appcompat
LOCAL_AAPT_FLAGS += --extra-packages com.exp.librairy
LOCAL_STATIC_JAVA_AAR_LIBRARIES := explibrairy
include $(BUILD_PACKAGE)
#Build aar libs
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_AAR_LIBRARIES := explibrairy:project/build/outputs/aar/explibrairy-debug.aar
include $(BUILD_MULTI_PREBUILT)
include $(call all-makefiles-under, $(LOCAL_PATH))

- 584
- 2
- 6
- 20
-
You use LOCAL_PREBUILT_STATIC_AAR_LIBRARIES - there is no such variable, in my example used LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES – Kostiantyn Luzan Feb 17 '19 at 18:24