How to include constraint layout library in an AOSP project
Asked Answered
M

4

5

I have an existing android application that I'd like to build inside AOSP (android source tree) using Android.mk. The app uses constraint layout which is not included in AOSP source tree (AFAIK). How can I satisfy this dependency? Other support libs are included such as recyclerview, v4 etc but not contraint layout.

Should I download the lib aar and if yes , how do I add/include it? Or should I get the source (where to download?) and build it somewhere in the source tree?

Thanks in advance for any help.

Minatory answered 25/9, 2017 at 20:37 Comment(0)
S
7

There are several ways to resolve your issue.

1. Add a prebuilt .apk

You don't have to put your source code to the AOSP tree. You can just add your .apk file, put it either in packages/apps/YourApp, or vendor/yourname/packages/apps/YourApp, or even your_dir_name/packages/apps/YourApp, and create an Android.mk file for build system to determine your application.

Android.mk will look like:

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

LOCAL_MODULE := YourApplication # your .apk name
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)

include $(BUILD_PREBUILT)

Pros: you can build your project with gradle.

2. Add source code to AOSP

If you still want to place your source code to packages/apps and build it there, you can put a ConstrainsLayout to your project's libs/ directory and add to your Android.mk something like:

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

  # List of static libraries to include in the package
  LOCAL_STATIC_JAVA_LIBRARIES := constraint-layout

  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)

  # Name of the APK
  LOCAL_PACKAGE_NAME := YourApplication

  # Tell it to build an APK
  include $(BUILD_PACKAGE)

In case you will not get it work (I haven't met this issue, but he did):

LOCAL_STATIC_JAVA_LIBRARIES := libconstraint-layout

include $(BUILD_PACKAGE)

Other stuff, and finally

include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libconstraint-layout:libs/constraint-layout.aar

Cons: You will have to build your code either with make by mma or mm -B, or to have a gradle as your second build system for development. The second option will work, but to establish a full build and to have your .apk built in out/ directory you will have to build it with make.

3. Adding a ConstraintLayout

In case you want to have several applications, which use a constraint layout, you can add it as a new library module as precompiled .aar. Can be somewhere in 'vendor/yourname/libs' or 'your_dir_name/libs' respectively. It is similar to adding a prebuilt .apk:

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

LOCAL_MODULE := constraint-layout
LOCAL_SRC_FILES := $(LOCAL_MODULE).aar
LOCAL_MODULE_SUFFIX := .aar

include $(BUILD_PREBUILT)

After that, in your application's Android.mk you will have to add:

LOCAL_STATIC_JAVA_LIBRARIES := constraint-layout

Alternatively, you can add a ConstraintLayout's .aar to the prebuilds/ as it eventually will be there someday.

There is a good topic about Android.mk: https://wladimir-tm4pda.github.io/porting/build_cookbook.html

Smallpox answered 25/9, 2017 at 22:8 Comment(1)
The constraint layout aar can be found here: mvnrepository.com/artifact/com.android.support.constraint/…Sisk
R
3

From Android 9.0, there is no need of adding .aar and .jar separately in the project for constraint-layout. We can use constraint-layout library built in to AOSP.

We need to add one extra line in Android.mk:

LOCAL_USE_AAPT2 := true

Then, we need to add:

LOCAL_AAPT_FLAGS := \
    --auto-add-overlay \
    --extra-packages android.support.constraint

LOCAL_STATIC_ANDROID_LIBRARIES += android-support-constraint-layout

LOCAL_STATIC_JAVA_LIBRARIES += android-support-constraint-layout-solver

For more detailed answer: How to use constraint-layout during AOSP build without including external .aar and .jar

Riant answered 25/7, 2020 at 19:9 Comment(0)
H
2

https://mcmap.net/q/1898410/-how-to-include-constraint-layout-library-in-an-aosp-project is right, except LOCAL_STATIC_JAVA_AAR_LIBRARIES should be used instead of LOCAL_STATIC_JAVA_LIBRARIES, since constraint-layout is a aar file.

Hallucinosis answered 21/10, 2019 at 1:58 Comment(0)
K
1

This can be done as follows:

  1. Download constraint-layout.aar and constraint-layout-solve.jar and put these files in lib folder.

  2. Add the following to your Android.mk file

LOCAL_STATIC_JAVA_AAR_LIBRARIES += constraint-layout
LOCAL_STATIC_JAVA_LIBRARIES += constraint-layout-solver

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := constraint-layout:libs/constraint-layout.aar
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := constraint-layput-solver:libs/constraint-layout-solver.jar
Kirschner answered 26/9, 2018 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.