11

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 ?

Juude
  • 1,207
  • 2
  • 13
  • 22

3 Answers3

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.

  • 1
    I 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
  • 3
    For 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
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.

qhdwangnan
  • 175
  • 1
  • 5
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))
SoulaimenK
  • 584
  • 2
  • 6
  • 20