How to include constraint layout library in an AOSP project

做~自己de王妃 提交于 2019-12-06 12:04:15

问题


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.


回答1:


There are several ways to resolve your issue.

1. Add a prebuilt .apk

You don't have to put your source code in 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. Your Android.mk will be 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 in 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 (I didn't add any .aars like this, so this wasn't tested by me and can contain errors!):

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)

And 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




回答2:


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_LIBRARIES += constraint-layout LOCAL_STATIC_JAVA_AAR_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




回答3:


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.



来源:https://stackoverflow.com/questions/46413784/how-to-include-constraint-layout-library-in-an-aosp-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!