如何使用 Android Make 在应用程序中包含 AAR?
How do I include an AAR in an app using Android Make?
我试图在使用 Make 构建应用程序时包含一个库 (LeakCanary),但我 运行 遇到了几个不同的问题,具体取决于我如何构建和包含它.
这是我用来构建 LeakCanary 的 make 文件:
# Build LeakCanary aar
#=========================
include $(CLEAR_VARS)
LOCAL_MODULE := LeakCanary
LOCAL_MODULE_TAGS := optional
LOCAL_MANIFEST_FILE := ./leakcanary/leakcanary-android/src/main/AndroidManifest.xml
LOCAL_SRC_FILES := $(call all-java-files-under, leakcanary/leakcanary-android/src/main/java)
LOCAL_SRC_FILES += $(call all-java-files-under, leakcanary/leakcanary-analyzer/src/main/java)
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/leakcanary/leakcanary-android/src/main/res
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4
LOCAL_JAVACFLAGS := -encoding UTF-8
LOCAL_USE_AAPT2 := true
LOCAL_SDK_VERSION := current
LOCAL_JAR_EXCLUDE_FILES := none
LOCAL_STATIC_JAVA_LIBRARIES += \
haha \
leakcanary_watcher \
trove4j
include $(BUILD_STATIC_JAVA_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := haha
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/haha-2.0.4.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := leakcanary_watcher
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/leakcanary-watcher-1.6.1.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := trove4j
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/trove4j-20160824.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
然后我将库添加到应用程序:
LOCAL_STATIC_ANDROID_LIBRARIES += \
LeakCanary
如果我以这种方式构建它,应用 APK 将包含来自 LeakCanary 的所有资源,但清单
没有合并。 LeakCanary 的清单文件声明了一个 activity 然后找不到运行时。
如果我改为:
make LeakCanary /usr/build/out/p/target/common/obj/JAVA_LIBRARIES/LeakCanary_intermediates/javalib.aar
然后将其复制到应用程序并像这样包含它:
LOCAL_STATIC_JAVA_AAR_LIBRARIES += \
leakcanary
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += \
leakcanary:libs/leakcanary.aar
清单似乎已合并,但库资源尚未(还?)包括在内:
FAILED: /usr/build/out/p/target/product/platform/obj/APPS/app_intermediates/package-res.apk /usr/build/out/p/target/common/obj/APPS/app_intermediates/aapt2.srcjar /usr/build/out/p/target/common/obj/APPS/app_intermediates/extra_packages /usr/build/out/p/target/common/obj/APPS/app_intermediates/src/R.stamp /usr/build/out/p/target/common/obj/APPS/app_intermediates/proguard_options
/bin/bash /usr/build/out/p/target/product/platform/obj/APPS/app_intermediates/package-res.apk.rsp
warn: ignoring density 'mdpi-v4' for -c option.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:139: error: resource xml/leak_canary_file_paths (aka com.company.app:xml/leak_canary_file_paths) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource string/leak_canary_display_activity_label (aka com.company.app:string/leak_canary_display_activity_label) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource style/leak_canary_LeakCanary.Base (aka com.company.app:style/leak_canary_LeakCanary.Base) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource string/leak_canary_storage_permission_activity_label (aka com.company.app:string/leak_canary_storage_permission_activity_label) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource style/leak_canary_Theme.Transparent (aka com.company.app:style/leak_canary_Theme.Transparent) not found.
error: failed processing manifest.
但是,如果我随后像这样包含 LeakCanary 资源,则构建工作正常:
LOCAL_RESOURCE_DIR += \
$(LOCAL_PATH)/leakcanary/leakcanary-android/src/main/res
我有点迷茫 LOCAL_STATIC_ANDROID_LIBRARIES
和 LOCAL_STATIC_JAVA_AAR_LIBRARIES
之间的区别,我应该
在这里使用。
根据我所见,LOCAL_STATIC_JAVA_AAR_LIBRARIES
似乎包含并合并了清单,但不包含资源,除非它们是用 LOCAL_RESOURCE_DIR
指定的。另一方面,LOCAL_STATIC_ANDROID_LIBRARIES
无需指定 LOCAL_RESOURCE_DIR
即可包含资源,但它不会合并清单。
我正在寻找某种方法来使清单合并和资源包含一起工作,而不必将资源显式添加到 LOCAL_RESOURCE_DIR
。
关于我可以做些什么来完成这项工作有什么想法吗?我也想让 LeakCanary 可用于其他应用程序,这就是我尝试将其构建为模块的原因。
我试图避免的最后一个选择(如我所见)是让所有应用程序构建并在其 make 文件中包含 LeakCanary 依赖项。
我能够通过构建两个模块 LeakCanary 和 LeakCanary-res 来解决这个问题。
LOCAL_MODULE := LeakCanary-res
LOCAL_MODULE_TAGS := optional
LOCAL_MANIFEST_FILE := leakcanary-android/src/main/AndroidManifest.xml
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/leakcanary-android/src/main/res
LOCAL_USE_AAPT2 := true
LOCAL_SDK_VERSION := current
include $(BUILD_STATIC_JAVA_LIBRARY)
然后像这样包含它们:
LOCAL_STATIC_ANDROID_LIBRARIES += \
LeakCanary-res
LOCAL_STATIC_JAVA_AAR_LIBRARIES += \
LeakCanary-prebuilt
我试图在使用 Make 构建应用程序时包含一个库 (LeakCanary),但我 运行 遇到了几个不同的问题,具体取决于我如何构建和包含它.
这是我用来构建 LeakCanary 的 make 文件:
# Build LeakCanary aar
#=========================
include $(CLEAR_VARS)
LOCAL_MODULE := LeakCanary
LOCAL_MODULE_TAGS := optional
LOCAL_MANIFEST_FILE := ./leakcanary/leakcanary-android/src/main/AndroidManifest.xml
LOCAL_SRC_FILES := $(call all-java-files-under, leakcanary/leakcanary-android/src/main/java)
LOCAL_SRC_FILES += $(call all-java-files-under, leakcanary/leakcanary-analyzer/src/main/java)
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/leakcanary/leakcanary-android/src/main/res
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4
LOCAL_JAVACFLAGS := -encoding UTF-8
LOCAL_USE_AAPT2 := true
LOCAL_SDK_VERSION := current
LOCAL_JAR_EXCLUDE_FILES := none
LOCAL_STATIC_JAVA_LIBRARIES += \
haha \
leakcanary_watcher \
trove4j
include $(BUILD_STATIC_JAVA_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := haha
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/haha-2.0.4.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := leakcanary_watcher
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/leakcanary-watcher-1.6.1.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE := trove4j
LOCAL_SDK_VERSION := current
LOCAL_SRC_FILES := leakcanary/libs/trove4j-20160824.jar
LOCAL_UNINSTALLABLE_MODULE := true
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
然后我将库添加到应用程序:
LOCAL_STATIC_ANDROID_LIBRARIES += \
LeakCanary
如果我以这种方式构建它,应用 APK 将包含来自 LeakCanary 的所有资源,但清单 没有合并。 LeakCanary 的清单文件声明了一个 activity 然后找不到运行时。
如果我改为:
make LeakCanary /usr/build/out/p/target/common/obj/JAVA_LIBRARIES/LeakCanary_intermediates/javalib.aar
然后将其复制到应用程序并像这样包含它:
LOCAL_STATIC_JAVA_AAR_LIBRARIES += \
leakcanary
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += \
leakcanary:libs/leakcanary.aar
清单似乎已合并,但库资源尚未(还?)包括在内:
FAILED: /usr/build/out/p/target/product/platform/obj/APPS/app_intermediates/package-res.apk /usr/build/out/p/target/common/obj/APPS/app_intermediates/aapt2.srcjar /usr/build/out/p/target/common/obj/APPS/app_intermediates/extra_packages /usr/build/out/p/target/common/obj/APPS/app_intermediates/src/R.stamp /usr/build/out/p/target/common/obj/APPS/app_intermediates/proguard_options
/bin/bash /usr/build/out/p/target/product/platform/obj/APPS/app_intermediates/package-res.apk.rsp
warn: ignoring density 'mdpi-v4' for -c option.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:139: error: resource xml/leak_canary_file_paths (aka com.company.app:xml/leak_canary_file_paths) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource string/leak_canary_display_activity_label (aka com.company.app:string/leak_canary_display_activity_label) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:144: error: resource style/leak_canary_LeakCanary.Base (aka com.company.app:style/leak_canary_LeakCanary.Base) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource string/leak_canary_storage_permission_activity_label (aka com.company.app:string/leak_canary_storage_permission_activity_label) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource mipmap/leak_canary_icon (aka com.company.app:mipmap/leak_canary_icon) not found.
/usr/build/out/p/target/common/obj/APPS/app_intermediates/AndroidManifest.xml:158: error: resource style/leak_canary_Theme.Transparent (aka com.company.app:style/leak_canary_Theme.Transparent) not found.
error: failed processing manifest.
但是,如果我随后像这样包含 LeakCanary 资源,则构建工作正常:
LOCAL_RESOURCE_DIR += \
$(LOCAL_PATH)/leakcanary/leakcanary-android/src/main/res
我有点迷茫 LOCAL_STATIC_ANDROID_LIBRARIES
和 LOCAL_STATIC_JAVA_AAR_LIBRARIES
之间的区别,我应该
在这里使用。
根据我所见,LOCAL_STATIC_JAVA_AAR_LIBRARIES
似乎包含并合并了清单,但不包含资源,除非它们是用 LOCAL_RESOURCE_DIR
指定的。另一方面,LOCAL_STATIC_ANDROID_LIBRARIES
无需指定 LOCAL_RESOURCE_DIR
即可包含资源,但它不会合并清单。
我正在寻找某种方法来使清单合并和资源包含一起工作,而不必将资源显式添加到 LOCAL_RESOURCE_DIR
。
关于我可以做些什么来完成这项工作有什么想法吗?我也想让 LeakCanary 可用于其他应用程序,这就是我尝试将其构建为模块的原因。 我试图避免的最后一个选择(如我所见)是让所有应用程序构建并在其 make 文件中包含 LeakCanary 依赖项。
我能够通过构建两个模块 LeakCanary 和 LeakCanary-res 来解决这个问题。
LOCAL_MODULE := LeakCanary-res
LOCAL_MODULE_TAGS := optional
LOCAL_MANIFEST_FILE := leakcanary-android/src/main/AndroidManifest.xml
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/leakcanary-android/src/main/res
LOCAL_USE_AAPT2 := true
LOCAL_SDK_VERSION := current
include $(BUILD_STATIC_JAVA_LIBRARY)
然后像这样包含它们:
LOCAL_STATIC_ANDROID_LIBRARIES += \
LeakCanary-res
LOCAL_STATIC_JAVA_AAR_LIBRARIES += \
LeakCanary-prebuilt