ungalcrys
ungalcrys

Reputation: 5610

how to create AAR archive file from Android.mk

I am trying to build a custom Android System, and for that I want to use a separate library to contain some custom views. For this I created a Java Library using

...
LOCAL_MODULE:= com.zzz.ui
include $(BUILD_JAVA_LIBRARY)
...

but the downside on this is that I can't use any resources and all of ui customization and layout arrangements have to be done programatically.

Now I want to change it and create a Android Library, to generate a *.aar, in which I could add the resources also and inflate the views directly from layout files along with some drawables. How can I achieve this ?

Upvotes: 2

Views: 4671

Answers (3)

Farhan Ar Rafi
Farhan Ar Rafi

Reputation: 148

Turns out to be pretty simple. Just run the following commands:

  1. First build the JAR make out/target/common/obj/JAVA_LIBRARIES/YourLibName_intermediates/javalib.jar This command generates classes.jar, classes-full-debug.jar and other files which are necessary for building the AAR package.
  2. Then make the AAR make out/target/common/obj/JAVA_LIBRARIES/YourLibName_intermediates/javalib.aar

Note:

  1. If there is any message regarding a missing "R.txt" file, just copy it from the src directory of 'out/target/common/obj/JAVA_LIBRARIES/YourLibName_intermediates/'.

  2. You do not need to make any changes in the Android.mk file.

Upvotes: 1

Ravi
Ravi

Reputation: 479

To build .aar from AOSP build system, you need to create the make file for your library,

Suppose you want to create lib named xyz.aar whose package name is com.xyz.xyz and internally your lib is using some other aar named abc.aar with package name com.abc.abc then the make file may look like below:-

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := xyz

LOCAL_STATIC_JAVA_LIBRARIES :=android-common \
    android-support-v4 \
    android-support-design \
    android-support-v7-appcompat \
    android-support-design-res \
    android-support-v7-cardview

LOCAL_STATIC_JAVA_AAR_LIBRARIES:= abc

LOCAL_SRC_FILES := $(call all-java-files-under, java)

res_dirs := res \

LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dirs)) \
    prebuilts/sdk/current/support/v7/cardview/res \
    prebuilts/sdk/current/support/v7/appcompat/res \
    prebuilts/sdk/current/support/design/res \

LOCAL_AAPT_FLAGS := \
    --auto-add-overlay \
    --extra-packages android.support.v7.cardview \
    --extra-packages android.support.v7.appcompat \
    --extra-packages android.support.design \
    --extra-packages com.abc.abc


LOCAL_JACK_ENABLED := disabled 
LOCAL_PROGUARD_ENABLED := disabled 

include $(BUILD_STATIC_JAVA_LIBRARY)

include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := all_static_libs

include $(BUILD_MULTI_PREBUILT)

include $(call all-makefiles-under,$(LOCAL_PATH))

In above make file instead of using include $(BUILD_STATIC_AAR_LIBRARY) use include $(BUILD_STATIC_JAVA_LIBRARY) and set the aar lib name in variable LOCAL_MODULE

To compile the make file use below command

> make xyz out/target/common/obj/JAVA_LIBRARIES/xyz_intermediates/xyz.aar

Your xyz.aar will be at location out/target/common/obj/JAVA_LIBRARIES/xyz_intermediates/xyz.aar

Upvotes: 0

foo
foo

Reputation: 611

After my investigation I've found something like this. http://thread.gmane.org/gmane.comp.handhelds.android.building/1020

This answer is scrappy though, however from this I was able to figure out that I can build AAR from literally any module I want using the steps:

  1. In your module provide a LOCAL_RESOURCE_DIR definition; problem here is that the toolchain automatically tries compiling/merging of all the xml files in it so if it does find any xmls inside (with valid android res folder structure and naming) it raises an error. To workaround this you can put in the given res folder for example 'values' folder and then put a non-empty dummy.xml within it, for example

    <?xml version="1.0" encoding="utf-8"?> <resources> <integer name="dummy">09</integer> </resources>

    (at least one valid resource name definition is essential)

  2. Provide an AndroidManifest.xml for the module / project. An empty manifest will do

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:androidprv="http://schemas.android.com/apk/prv/res/android" package="com.android.car.vehiclenetwork" > </manifest>

  3. Change the Android.mk so that it includes static java library targets makefile and points to the resource directory

    LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res include $(BUILD_STATIC_JAVA_LIBRARY)

  4. Now execute the build. For that a slightly different approach is required than using mmm (this must be launched at the very top level of AOSP source tree):

    make out/target/common/obj/JAVA_LIBRARIES/<module-name>_intermediates/javalib.aar

The aar will be then produced in out/target/common/obj/JAVA_LIBRARIES/<module-name>_intermediates/javalib.aar .

Notes:

  1. Creating aar requires javac instead of jack. Hence this will also produce classes.jar (as it was pre-M toolchain) within out/target/common/obj/JAVA_LIBRARIES/_intermediates/aar that contains your javac-compiled classes suitable to be used outside for external project compilation.

  2. You can modify the toolchain file @build/core/static_java_library.mk - add $(warning $(built_aar)) right before line

    built_aar := $(intermediates.COMMON)/javalib.aar

    This will show all modules suitable of building an aar of themselves when a topdir makefile of the AOSP code tree is invoked.

Upvotes: 0

Related Questions