如何使用 android.mk 在 AOSP 中包含 .aar [英] How to include .aar in AOSP with android.mk

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

问题描述

我需要在 aosp 构建树中使用 android.mk 构建一个应用程序.我有一个自定义的 .arr lib,它位于以下文件夹 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

任何人都可以告诉我如何在 android aosp 构建中包含 aar.我已经尝试了这里描述的以下方法 使用 .aar 库构建 aosp 的 Stackoverflow 链接

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.

推荐答案

我知道我为时已晚,但仍然值得分享这些信息.LOCAL_STATIC_JAVA_AAR_LIBRARIES 对 AAPT2 的支持在某些时候被破坏了.因此,即使您按上述方式添加库,也不会链接来自 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:

添加A、nosp-mirror/platform_build/commit/2902d4585f2a60​​a91084e96D>Aars 被解压到 out/.../intermediates* 目录并链接到 aosp 模块.

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

norelifer>noreelk85a452af4269c947797b68027c2d663d4ef#diff-0a65aec914853a06,不再需要链接解压的 aars,因为 AAPT2 支持直接链接到数组.

Dec 5, 2015 Support to build with AAPT2 As 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 中修复.

The bug was fixed in 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) 挑选修复 2015 年 12 月 5 日支持使用 AAPT2 从 aosp 构建

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆