Search code examples
androidandroid-constraintlayoutandroid-sourceandroid.mk

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.


Solution

  • 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