安卓源码环境:Android.mk如何编译aar包?

在安卓源码top目录执行编译命令:(注意:下面的Android.mk要被include进去)

make test_aar out/target/common/obj/JAVA_LIBRARIES/test_aar_intermediates/javalib.aar

通用格式:make <module-name> out/target/common/obj/JAVA_LIBRARIES/<module-name>_intermediates/javalib.aar

替换成LOCAL_MODULE的名字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := test_aar

LOCAL_STATIC_JAVA_LIBRARIES :=android-common \
android-support-v4 \

# LOCAL_STATIC_JAVA_AAR_LIBRARIES:= abc

LOCAL_SRC_FILES := $(call all-java-files-under, java)

res_dirs := res \

LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dirs)) \
prebuilts/sdk/current/support/v7/cardview/res \
prebuilts/sdk/current/support/v7/appcompat/res \
prebuilts/sdk/current/support/design/res \

LOCAL_AAPT_FLAGS := \
--auto-add-overlay \
--extra-packages android.support.v7.cardview \
--extra-packages android.support.v7.appcompat \
--extra-packages android.support.design \
--extra-packages com.abc.abc


LOCAL_JACK_ENABLED := disabled
LOCAL_PROGUARD_ENABLED := disabled

include $(BUILD_STATIC_JAVA_LIBRARY)

https://android.googlesource.com/platform/build/+/master/core/static_java_library.mk#133

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Rule to build AAR, archive including classes.jar, resource, etc.
built_aar := $(intermediates.COMMON)/javalib.aar
$(built_aar): PRIVATE_MODULE := $(LOCAL_MODULE)
$(built_aar): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
$(built_aar): PRIVATE_CLASSES_JAR := $(aar_classes_jar)
$(built_aar): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
$(built_aar): PRIVATE_R_TXT := $(intermediates.COMMON)/R.txt
$(built_aar): $(JAR_ARGS)
$(built_aar) : $(aar_classes_jar) $(full_android_manifest) $(intermediates.COMMON)/R.txt
@echo "target AAR: $(PRIVATE_MODULE) ($@)"
$(hide) rm -rf $(dir $@)aar && mkdir -p $(dir $@)aar/res
$(hide) cp $(PRIVATE_ANDROID_MANIFEST) $(dir $@)aar/AndroidManifest.xml
$(hide) cp $(PRIVATE_CLASSES_JAR) $(dir $@)aar/classes.jar
# Note: Use "cp -n" to honor the resource overlay rules, if multiple res dirs exist.
$(hide) $(foreach res,$(PRIVATE_RESOURCE_DIR),cp -Rfn $(res)/* $(dir $@)aar/res;)
$(hide) cp $(PRIVATE_R_TXT) $(dir $@)aar/R.txt
$(hide) $(JAR) -cMf $@ \
$(call jar-args-sorted-files-in-directory,$(dir $@)aar)

参考链接

how to create AAR archive file from Android.mk - Stack Overflow: https://stackoverflow.com/questions/42070123/how-to-create-aar-archive-file-from-android-mk

Build AAR package as part of AOSP build: https://groups.google.com/g/android-building/c/htNXz9Lkq7E?pli=1


安卓源码环境:Android.mk如何编译aar包?
https://iwesley.top/article/797e1cc1/
作者
Wesley
发布于
2024年11月10日
许可协议