如何使用 android.mk 在 AOSP 中包含 .aar
How to include .aar in AOSP with android.mk
我需要在 aosp 构建树中使用 android.mk 构建一个应用程序。
我有一个自定义的 .arr 库,它位于以下文件夹 apps/libs/mylib.aar
任何人都可以告诉我如何在 android aosp 构建中包含 aar。
我已经尝试过此处描述的以下方法 Whosebug 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))
我检查了不同的构建方法。
我知道我来晚了,但仍然值得分享这些信息。
LOCAL_STATIC_JAVA_AAR_LIBRARIES
对 AAPT2 的支持在某些时候被破坏了。
因此,即使您按上述方式添加库,也不会链接来自 aar 的资源。
来自 AOSP git 历史:
Oct 30, 2014 Add support for prebuilt AARs.
Aars 被解压到 out/.../intermediates* 目录并链接到 aosp 模块。
Dec 5, 2015 Support to build with AAPT2
正如您在 core/android_manifest.mk:26 中所见,不再需要链接解压的 aars,因为 AAPT2 支持直接链接到数组。
但不幸的是,它们没有被正确添加为 --extra-packages
。
错误已在 android-p-preview-5 中修复。
如果您仍在为 android 8 或 8.1 开发,请手动添加这些更改或在您的树中挑选它们。非常适合我。
UPD 2018-11-28
针对 android 8.1 及更早版本修复它的具体步骤:
1) cherry-pick 修复 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)
免责声明: 这是一个对我有用的变通方法。由于时间紧迫,急需支持旧客户端版本,我可能遗漏了一些用例。
我需要在 aosp 构建树中使用 android.mk 构建一个应用程序。 我有一个自定义的 .arr 库,它位于以下文件夹 apps/libs/mylib.aar
任何人都可以告诉我如何在 android aosp 构建中包含 aar。 我已经尝试过此处描述的以下方法 Whosebug 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))
我检查了不同的构建方法。
我知道我来晚了,但仍然值得分享这些信息。
LOCAL_STATIC_JAVA_AAR_LIBRARIES
对 AAPT2 的支持在某些时候被破坏了。
因此,即使您按上述方式添加库,也不会链接来自 aar 的资源。
来自 AOSP git 历史:
Oct 30, 2014 Add support for prebuilt AARs. Aars 被解压到 out/.../intermediates* 目录并链接到 aosp 模块。
Dec 5, 2015 Support to build with AAPT2 正如您在 core/android_manifest.mk:26 中所见,不再需要链接解压的 aars,因为 AAPT2 支持直接链接到数组。
但不幸的是,它们没有被正确添加为 --extra-packages
。
错误已在 android-p-preview-5 中修复。
如果您仍在为 android 8 或 8.1 开发,请手动添加这些更改或在您的树中挑选它们。非常适合我。
UPD 2018-11-28
针对 android 8.1 及更早版本修复它的具体步骤:
1) cherry-pick 修复 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)
免责声明: 这是一个对我有用的变通方法。由于时间紧迫,急需支持旧客户端版本,我可能遗漏了一些用例。