Ysch
Ysch

Reputation: 772

Include aar in android Makefile project

I'm trying to build an Android app through makefile (the app works perfectly when build by Android Studio).

I'm having problems with an .aar module that is used. the make completes successfully, but the .apk size is ridiculously small (500kb instead of 80mb). The .aar has multiple .so files in ./libs, as well as ./assets, but both seem not to be included.

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)

I've also tried forcing the .so files to be packaged, but I dpn't even know if this is the proper way to do 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)

Why don't the *.so files and the aar's assets get packaged? How do I fix it?

Upvotes: 3

Views: 2891

Answers (1)

Androina Yang
Androina Yang

Reputation: 145

Same problem has been meetted, after read the static_java_library.mk in AOSP, The answer is basically found.

To resolve this problem, you should define LOCAL_USE_AAPT2 := true in your project Android.mk file.

The following is a part of the code in 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)) \
    ) \
  ) \
))

Follow is a sample:

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)

Upvotes: 1

Related Questions