我试图通过makefile构建Android应用程序(该应用程序在Android Studio构建时运行良好)。
我遇到了使用的.aar模块的问题。 make成功完成,但.apk大小非常小(500kb而不是80mb)。 .aar在./libs中有多个.so文件,以及./assets,但两者似乎都不包括在内。
Android.mk:
LOCAL_SHARED_LIBRARIES := libnativelib
LOCAL_STATIC_JAVA_LIBRARIES:= aarmodulename
...
...
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES:=aarmodulename:libs/aarmodulename.aar
include $(BUILD_MULTI_PREBUILT)
我还尝试强制.so文件打包,但我甚至不知道这是否是正确的方法:
include $(CLEAR_VARS)
LOCAL_MODULE := libnativelib
LOCAL_SRC_FILES := libs/so/libnativelib.so
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
为什么不将* .so文件和aar的资产打包?我该如何解决?
答案 0 :(得分:1)
在阅读AOSP中的static_java_library.mk
之后,已经遇到了同样的问题,基本上找到了答案。
要解决此问题,您应在项目Android.mk文件中定义 LOCAL_USE_AAPT2 := true
。
以下是static_java_library.mk
中代码的一部分:
# Hack to build static Java library with Android resource
# See bug 5714516
all_resources :=
need_compile_res :=
# A static Java library needs to explicily set LOCAL_RESOURCE_DIR.
ifdef LOCAL_RESOURCE_DIR
need_compile_res := true
LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d)))
endif
ifdef LOCAL_USE_AAPT2
ifneq ($(LOCAL_STATIC_ANDROID_LIBRARIES),)
need_compile_res := true
endif
endif
ifeq ($(need_compile_res),true)
all_resources := $(strip \
$(foreach dir, $(LOCAL_RESOURCE_DIR), \
$(addprefix $(dir)/, \
$(patsubst res/%,%, \
$(call find-subdir-assets,$(dir)) \
) \
) \
))
关注是一个示例:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
ifeq ($(TARGET_BUILD_APPS),)
# Use AAPT2 only when TARGET_BUILD_APPS is empty because AAPT2 is not compatible with the current
# setup of prebuilt support libs used in unbundled builds. b/29836407
LOCAL_USE_AAPT2 := true
endif
# SRC files
LOCAL_SRC_FILES := \
$(call all-java-files-under, src)
LOCAL_RESOURCE_DIR := \
res
# JAR
LOCAL_STATIC_JAVA_LIBRARIES := \
ifdef LOCAL_USE_AAPT2
LOCAL_SHARED_ANDROID_LIBRARIES := \
android-support-annotations \
android-support-compat \
android-support-core-ui \
android-support-v7-appcompat \
android-support-v7-recyclerview
else
LOCAL_AAPT_FLAGS := --auto-add-overlay \
--extra-packages android.support.compat \
--extra-packages android.support.v7.appcompat \
--extra-packages android.support.v7.recyclerview
LOCAL_RESOURCE_DIR += \
frameworks/support/compat/res \
frameworks/support/v7/appcompat/res \
frameworks/support/v7/recyclerview/res
LOCAL_JAVA_LIBRARIES := \
android-support-annotations \
android-support-compat \
android-support-core-ui \
android-support-v7-appcompat \
android-support-v7-recyclerview
# OR define
LOCAL_STATIC_ANDROID_LIBRARIES := \
endif
LOCAL_MANIFEST_FILE := main/AndroidManifest.xml
LOCAL_PACKAGE_NAME := PACKAGE_NAME
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
# Comment for now unitl all private API dependencies are removed
# LOCAL_SDK_VERSION := system_current
include $(BUILD_PACKAGE)