先看看引入jar (因为和引用aar很像)
LOCAL_STATIC_JAVA_LIBRARIES := dictenginelib \
org.apache.http.legacy \
constraint-layout-solver-1.1.3_ \
yxp_support-new-v4-annotations-25.3.1 \
yxp_support-new-v4-compat-25.3.1 \
yxp_support-new-v4-core-ui-25.3.1 \
yxp_support-new-v4-fragment-25.3.1 \
yxp_support-new-v4-media-compat-25.3.1 \
yxp_support-new-v4-core-utils-25.3.1 \
yxp_support-new-vector-drawable-25.3.1 \
yxp_support-new-animated-vector-drawable-25.3.1 \
IntelligentLearningScheme_cupboard-2.2.0 \
IntelligentLearningScheme_glide-4.3.1 \
IntelligentLearningScheme_glide_annotations-4.3.1 \
IntelligentLearningScheme_glide_disklrucache-4.3.1 \
IntelligentLearningScheme_glide_gifdecoder-4.3.1\
IntelligentLearningScheme_gson-2.8.5 \
IntelligentLearningScheme_okgo-3.0.4 \
IntelligentLearningScheme_okhttp-3.8.1 \
IntelligentLearningScheme_okio-1.13.0
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := IntelligentLearningScheme_cupboard-2.2.0:libs/cupboard-2.2.0.jar \
IntelligentLearningScheme_glide-4.3.1:libs/glide-4.3.1.jar \
IntelligentLearningScheme_glide_annotations-4.3.1:libs/glide_annotations-4.3.1.jar \
IntelligentLearningScheme_glide_disklrucache-4.3.1:libs/glide_disklrucache-4.3.1.jar \
IntelligentLearningScheme_glide_gifdecoder-4.3.1:libs/glide_gifdecoder-4.3.1.jar \
IntelligentLearningScheme_gson-2.8.5:libs/gson-2.8.5.jar \
IntelligentLearningScheme_okgo-3.0.4:libs/okgo-3.0.4.jar \
IntelligentLearningScheme_okhttp-3.8.1:libs/okhttp-3.8.1.jar \
IntelligentLearningScheme_okio-1.13.0:libs/okio-1.13.0.jar \
constraint-layout-solver-1.1.3_:/../../library/third_jars/constraint-layout-solver-1.1.3.jar \
yxp_support-new-v4-annotations-25.3.1:/../../library/third_jars/support-annotations-25.3.1_.jar \
yxp_support-new-v4-compat-25.3.1:/../../library/third_jars/support-compat-25.3.1_.jar \
yxp_support-new-v4-core-ui-25.3.1:/../../library/third_jars/support-core-ui-25.3.1_.jar \
yxp_support-new-v4-fragment-25.3.1:/../../library/third_jars/support-fragment-25.3.1_.jar \
yxp_support-new-v4-media-compat-25.3.1:/../../library/third_jars/support-media-compat-25.3.1_.jar \
yxp_support-new-v4-core-utils-25.3.1:/../../library/third_jars/support-core-utils-25.3.1_.jar \
yxp_support-new-vector-drawable-25.3.1:/../../library/third_jars/support-vector-drawable-25.3.1_.jar \
yxp_support-new-animated-vector-drawable-25.3.1:/../../library/third_jars/support-animated-vector-drawable-25.3.1_.jar
- 使用LOCAL_STATIC_JAVA_LIBRARIES 要引入的jar定义一个变量名。
- 使用LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES 给定义的变量 设置一个引用路径。
再看引用aar
LOCAL_STATIC_JAVA_AAR_LIBRARIES := IntelligentLearningScheme_appcompat-v7-25.3.1 \
IntelligentLearningScheme_recyclerview-v7-25.3.1 \
IntelligentLearningScheme_constraint-layout-1.1.3 \
IntelligentLearningScheme_utilcode-1.9.9
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := IntelligentLearningScheme_appcompat-v7-25.3.1:/../../library/aars/appcompat-v7-25.3.1.aar \
IntelligentLearningScheme_recyclerview-v7-25.3.1:/../../library/aars/recyclerview-v7-25.3.1.aar \
IntelligentLearningScheme_constraint-layout-1.1.3:/../../library/aars/constraint-layout-1.1.3.aar \
IntelligentLearningScheme_utilcode-1.9.9:libs/utilcode-1.9.9.aar
#需要引入library/aar的包名
LOCAL_AAPT_FLAGS := --auto-add-overlay \
--extra-packages android.support.v7.appcompat \
--extra-packages android.support.constraint \
--extra-packages android.support.v7.recyclerview \
--extra-packages com.blankj.utilcode
可以发现不同的是:
- 需要使用
LOCAL_STATIC_JAVA_AAR_LIBRARIES
来给要引入的aar 定义一个变量(区别于LOCAL_STATIC_JAVA_LIBRARIES
)。 - 需要使用
LOCAL_AAPT_FLAGS
为aar添加一个包名。
需要注意的是:
android.support.constraint 的 targetSdkVersion='24'
appcompat、recyclerview、cardview 都是 minSdkVersion='9' , com.blankj.utilcode为 minSdkVersion='14'
所以在androidmianfest.xml加上
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="24" />
注意:
- 加上
android:targetSdkVersion="24"
会有一些问题,因为android6.0(23)
加入了动态权限,targetSdkVersion="24"
时需添加动态权限代码。 - 将
targetSdkVersion="24"
改为targetSdkVersion="22"
则不需要添加动态权限代码,但是会有编译错误,这里发现,只需要在编译一次此问题就会解决(比如加一个空格再上传代码编译,就会通过编译,以u27为例,暂不清楚为什么会这样,且只发现在constraint 库上有此问题,知道的同学望指教)
。 - 编译的apk安装,有几率
constraint
库会在setcontentview() 提示找不到此库。
完整android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := dictenginelib \
org.apache.http.legacy \
constraint-layout-solver-1.1.3_ \
yxp_support-new-v4-annotations-25.3.1 \
yxp_support-new-v4-compat-25.3.1 \
yxp_support-new-v4-core-ui-25.3.1 \
yxp_support-new-v4-fragment-25.3.1 \
yxp_support-new-v4-media-compat-25.3.1 \
yxp_support-new-v4-core-utils-25.3.1 \
yxp_support-new-vector-drawable-25.3.1 \
yxp_support-new-animated-vector-drawable-25.3.1 \
IntelligentLearningScheme_cupboard-2.2.0 \
IntelligentLearningScheme_glide-4.3.1 \
IntelligentLearningScheme_glide_annotations-4.3.1 \
IntelligentLearningScheme_glide_disklrucache-4.3.1 \
IntelligentLearningScheme_glide_gifdecoder-4.3.1\
IntelligentLearningScheme_gson-2.8.5 \
IntelligentLearningScheme_okgo-3.0.4 \
IntelligentLearningScheme_okhttp-3.8.1 \
IntelligentLearningScheme_okio-1.13.0
LOCAL_STATIC_JAVA_AAR_LIBRARIES := IntelligentLearningScheme_appcompat-v7-25.3.1 \
IntelligentLearningScheme_recyclerview-v7-25.3.1 \
IntelligentLearningScheme_constraint-layout-1.1.3 \
IntelligentLearningScheme_utilcode-1.9.9
#LOCAL_ASSET_DIR := $(LOCAL_PATH)/app/src/main/assets
LOCAL_JAVA_LIBRARIES := com.noahedu \
PenWriterLib \
com.writepen \
ucache
LOCAL_RESOURCE_DIR := \
$(addprefix $(LOCAL_PATH)/../../library/yxp_src/dictenginelib/, res) \
$(LOCAL_PATH)/res
# $(addprefix $(LOCAL_PATH)/,$(extrafunction)/src/main/res) \
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
src/com/noahedu/synclearning/engine/IBookService.aidl \
src/com/noahedu/synclearning/engine/IBookServiceCallback.aidl
# $(call all-java-files-under,$(extrafunction)/src/main/java)\
#需要引入library/aar的包名
LOCAL_AAPT_FLAGS := --auto-add-overlay \
--extra-packages com.noahedu.DictEngine \
--extra-packages android.support.v7.appcompat \
--extra-packages android.support.constraint \
--extra-packages android.support.v7.recyclerview \
--extra-packages com.blankj.utilcode
LOCAL_PACKAGE_NAME := IntelligentLearningScheme
LOCAL_CERTIFICATE := testkey
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
#libs/recyclerview-v7-25.3.1.aar
#$(LOCAL_PATH)/../../library/jars/recyclerview-v7-25.3.1.aar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := IntelligentLearningScheme_cupboard-2.2.0:libs/cupboard-2.2.0.jar \
IntelligentLearningScheme_glide-4.3.1:libs/glide-4.3.1.jar \
IntelligentLearningScheme_glide_annotations-4.3.1:libs/glide_annotations-4.3.1.jar \
IntelligentLearningScheme_glide_disklrucache-4.3.1:libs/glide_disklrucache-4.3.1.jar \
IntelligentLearningScheme_glide_gifdecoder-4.3.1:libs/glide_gifdecoder-4.3.1.jar \
IntelligentLearningScheme_gson-2.8.5:libs/gson-2.8.5.jar \
IntelligentLearningScheme_okgo-3.0.4:libs/okgo-3.0.4.jar \
IntelligentLearningScheme_okhttp-3.8.1:libs/okhttp-3.8.1.jar \
IntelligentLearningScheme_okio-1.13.0:libs/okio-1.13.0.jar \
constraint-layout-solver-1.1.3_:/../../library/third_jars/constraint-layout-solver-1.1.3.jar \
yxp_support-new-v4-annotations-25.3.1:/../../library/third_jars/support-annotations-25.3.1_.jar \
yxp_support-new-v4-compat-25.3.1:/../../library/third_jars/support-compat-25.3.1_.jar \
yxp_support-new-v4-core-ui-25.3.1:/../../library/third_jars/support-core-ui-25.3.1_.jar \
yxp_support-new-v4-fragment-25.3.1:/../../library/third_jars/support-fragment-25.3.1_.jar \
yxp_support-new-v4-media-compat-25.3.1:/../../library/third_jars/support-media-compat-25.3.1_.jar \
yxp_support-new-v4-core-utils-25.3.1:/../../library/third_jars/support-core-utils-25.3.1_.jar \
yxp_support-new-vector-drawable-25.3.1:/../../library/third_jars/support-vector-drawable-25.3.1_.jar \
yxp_support-new-animated-vector-drawable-25.3.1:/../../library/third_jars/support-animated-vector-drawable-25.3.1_.jar \
IntelligentLearningScheme_appcompat-v7-25.3.1:/../../library/aars/appcompat-v7-25.3.1.aar \
IntelligentLearningScheme_recyclerview-v7-25.3.1:/../../library/aars/recyclerview-v7-25.3.1.aar \
IntelligentLearningScheme_constraint-layout-1.1.3:/../../library/aars/constraint-layout-1.1.3.aar \
IntelligentLearningScheme_utilcode-1.9.9:libs/utilcode-1.9.9.aar
include $(BUILD_MULTI_PREBUILT)
#include $(BUILD_PREBUILT)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
如果代码或者资源不在默认路径,就需要额外指定,或者指定多个
# ‘:=’是赋值的意思;’+=’是追加的意思;‘\’表示连接符。
LOCAL_MODULE_TAGS :=user eng tests optional
- user: 指该模块只在user版本下才编译
- eng: 指该模块只在eng版本下才编译
- tests: 指该模块只在tests版本下才编译
- optional:指该模块在所有版本下都编译
#指定asset目录
LOCAL_ASSET_DIR := $(LOCAL_PATH)/app/src/main/assets
#指定res目录
LOCAL_RESOURCE_DIR := \
$(addprefix $(LOCAL_PATH)/../../library/yxp_src/dictenginelib/, res) \
$(LOCAL_PATH)/res
#指定src/aidl目录
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
src/com/noahedu/synclearning/engine/IBookService.aidl \
src/com/noahedu/synclearning/engine/IBookServiceCallback.aidl \
$(call all-Iaidl-files-under, src/com/noahedu/synclearning/engine/)
#指定arm目录
LOCAL_PREBUILT_JNI_LIBS_arm := libs/armeabi-v7a/libdecrypt.so \
libs/armeabi-v7a/liblasa.so \
libs/armeabi-v7a/libspeex.so \
libs/armeabi-v7a/libddsdns.so
#指定arm64目录
LOCAL_PREBUILT_JNI_LIBS_arm64 :=libs/arm64-v8a/libdecrypt.so \
libs/arm64-v8a/liblasa.so \
libs/arm64-v8a/libspeex.so \
libs/arm64-v8a/libddsdns.so
#指定androidmainfest.xml目录
LOCAL_MANIFEST_FILE := AndroidManifest.xml
发现总是编译不过的一个问题,看下面这段.mk片段
34行连接符 “ \ ”会把下面的35行的代码连成一片。 导致下面所有jar aar 都引用不到。因此编译就会报错
改正:
如果是注释:
连接符 “ \ ”后面要空一行。
连接符 “ \ ”后面要空一行。
连接符 “ \ ”后面要空一行。