Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include constraint layout library in an AOSP project

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.

like image 711
kotsen Avatar asked Sep 25 '17 20:09

kotsen


People also ask

Can we use linear layout in ConstraintLayout?

You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.

Why do we use ConstraintLayout in Android?

Advantages of using ConstraintLayout in AndroidIt helps to improve the UI performance over other layouts. With the help of ConstraintLayout, we can control the group of widgets through a single line of code. With the help of ConstraintLayout, we can easily add animations to the UI components which we used in our app.

How do I change ConstraintLayout to LinearLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.


4 Answers

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

like image 89
Vitalii Dmitriev Avatar answered Nov 05 '22 06:11

Vitalii Dmitriev


https://stackoverflow.com/a/46414919/9237859 is right, except LOCAL_STATIC_JAVA_AAR_LIBRARIES should be used instead of LOCAL_STATIC_JAVA_LIBRARIES, since constraint-layout is a aar file.

like image 22
jxltom Avatar answered Nov 05 '22 04:11

jxltom


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

like image 23
UkFLSUI Avatar answered Nov 05 '22 05:11

UkFLSUI


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
like image 26
Nishanth K H Avatar answered Nov 05 '22 05:11

Nishanth K H