aar support in Android.mk
Asked Answered
K

3

12

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 ?

Knotty answered 3/7, 2015 at 11:33 Comment(0)
C
15

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.

Cahill answered 30/12, 2015 at 18:49 Comment(7)
I doubt this can use resources in aar.Knotty
Is there any solution about this?Pepys
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.Cahill
For others coming here - Please check @qhdwangnan answer below, this answer alone will not suffice to solve the problem.Yoicks
Following your answer, I get this error : ninja: error: 'out/target/common/obj/JAVA_LIBRARIES/<librairy>_intermediates/aar/classes.jar', needed by 'out/target/common/obj/APPS/AppName_intermediates/AndroidManifest.xml', missing and no known rule to make itSynclastic
@Synclastic Could you provide your Android.mk file, maybe you miss something?Cahill
@KostiantynLuzan : Could you check my comment belowSynclastic
S
8

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.

Southernly answered 18/4, 2016 at 8:17 Comment(3)
Does anyone have a solution for how to include the AAR library and resoures when you have LOCAL_USE_AAPT := true set as well?Turpitude
Any idea about how to use resource in aar file?Tetryl
@Southernly can you please provide sample Androd.mk file for more understanding...Geostrophic
S
3

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))
Synclastic answered 15/2, 2019 at 9:26 Comment(1)
You use LOCAL_PREBUILT_STATIC_AAR_LIBRARIES - there is no such variable, in my example used LOCAL_PREBUILT_STATIC_JAVA_LIBRARIESCahill

© 2022 - 2024 — McMap. All rights reserved.