本文介绍了如何在android.mk中的AOSP中包含.aar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to build an application with android.mk in aosp build tree.I have a custom .arr lib with me, Which resides in the following folder apps/libs/mylib.aar

任何人都可以告诉我如何将aar包含在android aosp版本中.我已经尝试过此处描述的以下方法

Anyone can tell me how to include the aar in the android aosp build.I already tried the following methods described here Stackoverflow link for aosp build with .aar lib

Android.mk看起来像

Android.mk is looked like

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))

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

I checked different approaches to build the same.

推荐答案

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

I know I am too late, but still worth sharing this information.LOCAL_STATIC_JAVA_AAR_LIBRARIES support of AAPT2 was broken at some point.So even if you add your library as descibed above, resources from aar would not be linked.

来自AOSP git历史记录:

from AOSP git history:

为2014年10月30日添加支持将Aar解压缩到out/.../intermediates *目录中,并链接到aosp模块.

Oct 30, 2014 Add support for prebuilt AARs.Aars were unpacked into out/.../intermediates* dirs and linked to the aosp modules.

2015年12月5日支持使用APT2a构建如您在 core/android_manifesta> mk中看到的: ,因此不再需要链接解压缩的aar,因为AAPT2支持直接链接到数组.

Dec 5, 2015 Support to build with AAPT2As you can see in core/android_manifest.mk:26, linking unpacked aars was not necessary anymore, because AAPT2 supports linking directly into arrays.

但不幸的是,它们没有正确地添加为--extra-packages.

But unfortunately they were not added as --extra-packages correctly.

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

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

If you are still developing for android 8 or 8.1 please add these changes manually or cherry-pick them in your tree. Worked perfectly for me.

UPD 2018-11-28

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

Exact steps to fix it for android 8.1 and earlier:

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

1) cherry-pick fix Dec 5, 2015 Support to build with AAPT2 from aosp

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

2) in build/core/prebuilt_internal.mk:593 add parameter --auto-add-overlay

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

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

3) As RenderScript was refactored much later, you have to define your aar module explicitly:

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解决方法.由于时间不足,并且迫切需要支持较旧的客户端版本,因此我可能错过了一些用例.

Disclaimer: this is rather a hacky workaround that worked for me. Due to lack of time and urgent need to support older client versions, I may have missed some use cases.

这篇关于如何在android.mk中的AOSP中包含.aar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:44
查看更多