如何在android.mk中的AOSP中包含.aar

时间:2018-06-27 09:10:39

标签: android android-source android-make

我需要在aosp构建树中使用android.mk构建应用程序。 我有一个自定义的.arr库,它位于以下文件夹apps / libs / mylib.aar

任何人都可以告诉我如何将aar包含在android aosp版本中。 我已经尝试过Stackoverflow link for aosp build with .aar lib

中介绍的以下方法

Android.mk看起来像

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS  := optional
LOCAL_PACKAGE_NAME := sample
LOCAL_CERTIFICATE  := platform

# SRC files
#=====================================================================
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
                   $(call all-Iaidl-files-under, aidl)

# RES files
#=====================================================================
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_RESOURCE_DIR +=prebuilts/sdk/current/extras/constraint-layout/res
LOCAL_RESOURCE_DIR +=frameworks/support/v7/appcompat/res
LOCAL_RESOURCE_DIR +=frameworks/support/design/res
LOCAL_RESOURCE_DIR +=frameworks/support-v4/res

LOCAL_MANIFEST_FILE :=AndroidManifest.xml
LOCAL_USE_AAPT2 := true
LOCAL_PROGUARD_ENABLED := disabled

# static .aar files
#=====================================================================
LOCAL_STATIC_JAVA_AAR_LIBRARIES:= mylib.aar

#Adding aapt packages
#=====================================================================
LOCAL_AAPT_FLAGS := --auto-add-overlay

LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.appcompat 
LOCAL_AAPT_FLAGS += --extra-packages android.support.v7.recyclerview
LOCAL_AAPT_FLAGS += --extra-packages android.support.annotations
LOCAL_AAPT_FLAGS += --extra-packages android.support.v4
LOCAL_AAPT_FLAGS += --extra-packages android.support.design
LOCAL_AAPT_FLAGS += --extra-packages com.sample.mylib

#Include Static libraries
#=====================================================================
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v7-appcompat
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v7-recyclerview
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v4
LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7-gridlayout
LOCAL_STATIC_JAVA_LIBRARIES += android-support-annotations
LOCAL_STATIC_JAVA_LIBRARIES += android-support-design
LOCAL_STATIC_JAVA_LIBRARIES += gson
LOCAL_STATIC_JAVA_LIBRARIES += zxing
LOCAL_STATIC_JAVA_LIBRARIES += picasso

#Set out path
#=====================================================================
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_APPS)

#For build the application package
#=====================================================================
include $(BUILD_PACKAGE)

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := mylib:libs/mylib.aar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += gson:libs/gson-2.8.1.jar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += zxing:libs/core-3.3.3.jar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += picasso:libs/picasso.jar

include $(BUILD_MULTI_PREBUILT)

include $(call all-makefiles-under,$(LOCAL_PATH))

我检查了不同的方法来构建相同的东西。

1 个答案:

答案 0 :(得分:4)

我知道我为时已晚,但仍然值得分享此信息。 LOCAL_STATIC_JAVA_AAR_LIBRARIES对AAPT2的支持在某个时候被破坏了。 因此,即使您如上所述添加库,也不会链接来自aar的资源。

来自AOSP git历史记录:

Oct 30, 2014 Add support for prebuilt AARs. 将Aar解压缩到out /.../ intermediates *目录中,并链接到aosp模块。

Dec 5, 2015 Support to build with AAPT2 如您在core/android_manifest.mk:26中所见,不再需要链接解压缩的aar,因为AAPT2支持直接链接到数组。

但是很遗憾,它们没有正确地添加为--extra-packages

该错误已在android-p-preview-5中修复。

如果您仍在为Android 8或8.1进行开发,请手动添加这些更改或在树中挑选它们。对我来说很完美。

UPD 2018-11-28

修复针对Android 8.1及更低版本的确切步骤:

1)cherry-pick fix 2015年12月5日,支持从aosp使用AAPT2 进行构建

2)在build / core / prebuilt_internal.mk:593中添加参数--auto-add-overlay

$(my_res_package): PRIVATE_AAPT_FLAGS := --static-lib --no-static-lib-packages --auto-add-overlay

3)由于RenderScript在以后进行了重构,因此您必须明确定义aar模块:

include $(CLEAR_VARS)
LOCAL_MODULE := my-library-module
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := my-library-module.aar
# Provide resources directory in order to compile them, enable AAPT2 for this module
LOCAL_RESOURCE_DIR := $(call intermediates-dir-for,JAVA_LIBRARIES,$(LOCAL_MODULE),,COMMON)/aar/res)
LOCAL_USE_AAPT2 := true
# if LOCAL_RENDERSCRIPT_TARGET_API >= 21, resources won't get compiled. Shouldn't affect anything else
LOCAL_RENDERSCRIPT_TARGET_API := 20
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
LOCAL_BUILT_MODULE_STEM := javalib.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)

免责声明:这是一种对我有用的hacky解决方法。由于时间不足,并且迫切需要支持较旧的客户端版本,因此我可能错过了一些用例。