How to include .aar in AOSP with android.mk

别说谁变了你拦得住时间么 提交于 2019-12-02 06:14:22

问题


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

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


回答1:


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.

from AOSP git history:

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

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.

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

The bug was fixed in android-p-preview-5.

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

Exact steps to fix it for android 8.1 and earlier:

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

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

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.



来源:https://stackoverflow.com/questions/51058787/how-to-include-aar-in-aosp-with-android-mk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!