1. Android.mk中增加两处
LOCAL_STATIC_JAVA_AAR_LIBRARIES:= <aar alias> //这里是一处
.
.
.
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
//这里是第二处
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := <aar alias>:libs/<lib file>.aar
include $(BUILD_MULTI_PREBUILT)
2. 清单文件中设置 minSdkVersion required by library into your manifest file.,否则编译不过
3. 避免编译时将R文件合并,可以使用 LOCAL_AAPT_FLAGS
after compile, the resources in the aar will be added to my main package's R file, but not in the aar package's R file. For example, the aar's package name is my.aar, the main project's package name is my.main. The aar has a string named "string_in_aar". After compile, the strings id is my.main.R.string_in_aar rather than my.aar.R.string_in_aar. This makes the apk crash, because the code in the aar uses my.aar.R.string_in_aar.
The solution is use:
LOCAL_AAPT_FLAGS += --extra-packages {aar package name}.
You will get two R file. They has the some content. One's package is the main package, the other is the aar package.
4. 此后还要添加obj下aar展开的res文件目录,比如
# 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
如果报错:
android.mk error: commands commence before first target
, 则很可能是上一句的换行连接符 \ 之后多了个空格;