2017-07-16 258 views
1

我試圖通過makefile構建一個Android應用程序(該應用程序在完成時由Android Studio構建)。在Android Makefile項目中包含aar

我遇到了使用的.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文件就可以打包,但我甚至dpn't知道這是不是這樣做的正確方法:

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的資產沒有打包?我如何解決它?

回答

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) 
+0

我嘗試使用LOCAL_USE_AAPT2:=真實的,但沒有奏效。不記得是什麼確切的結果.. – Ysch

+0

@ysch我使用這個解決方案解決了我的問題。我在答案中添加了一個示例,您可以再次嘗試,如果您解決了此問題,歡迎分享您的方法。 –