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.
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
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.
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
This can be done as follows:
Download constraint-layout.aar and constraint-layout-solve.jar and put these files in lib folder.
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
Related
I found similar posts but often the answer are not really correct and anyways seems to be not working for me.
I have an Android application created with Android Studio which includes java-websocket library. Now, I want to build this application inside AOSP thus I created a folder for the jar library with its own Android.mk and another folder which contains the application (with Android Studio structure) which contains its own Android.mk (modified in order to find AndroidManifest, res, java, AIDL files)
At first I had some troubles due to some incorrect parameter in the Android.mk for the jar file. Now the jar file seems to be correctly recognized and the intermediates are exported to out/ folder.
The problem now is during the build of the application since the classes exposed by the jar seems to be not available ending up in:
error: cannot find symbol WebSocket
and similar errors for any reference to the jar content.
The JAR folder contains: Android.mk Java-WebSocket-1.3.0.jar
And this is the Android.mk content
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := JavaWebSocket
LOCAL_MODULE_TAGS := optional
$(warning Going to build $(LOCAL_MODULE))
LOCAL_SRC_FILES := Java-WebSocket-1.3.0.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)
The application folder contains: Android.mk aidl_files app gradle build.gradle ..
(basically the Android Studio project plus the Android.mk and a folder containing AIDL (which I'll move outside later)
And this is the Android.mk file content:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := MyApplication
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := JavaWebSocket
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
LOCAL_SRC_FILES := $(call all-java-files-under, app/src/main/java) \
aidl_files/my_aidl_file.aidl
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/aidl_files
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/app/src/main/res
LOCAL_MANIFEST_FILE := app/src/main/AndroidManifest.xml
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_STATIC_ANDROID_LIBRARIES += \
androidx.appcompat_appcompat
include $(BUILD_PACKAGE)
I tried to merge all the informations I found in StackOverflow so far without success.
Is there any other LOCAL_something that I should set?
Good Day everyone. Still trying to figure out what's wrong with adding xml library.(Previos thread Cannot find libxml2 in android ndk project)
In jni folder: i jave prebuild libxml.so, which i successfully builded, android.mk and start-spice.c. Start-spice.c needs libxml in order to work.
Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE := libxml
LOCAL_SRC_FILES :=libxml.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := start-spice
LOCAL_SRC_FILES := start-spice.c
LOCAL_LDLIBS := -lxml
LOCAL_SHARED_LIBRARIES= xml
include $(BUILD_SHARED_LIBRARY)
And still it says that cannot find libxml/parser.h
Maybe someone could tell me what's wrong?
The think is that in .ci use libxml methods from linux and here i downloaded libxml2 and builded it - is there any difference?
Are you remembering to add the .h directories of libxml to the list of include directories for the other modules? I don't see any -I flags or LOCAL_C_INCLUDES being set
i've build my own androidrom from source and modified a few things.
But now i wan't to add an prebuilt .apk to the project,
read that i should make an folder in /packages/apps/
and add the .apk and an Android.mk to it with following code
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <folder name>
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
include $(BUILD_PREBUILT)
and the added the module to the in my case /device/sony/honami/full_honami.mk
when i now run brunch i get the error after some time that no rule for target *
so i tried some changes but nothing works for me and i can't find something where there write something other than this ...
so i added the .apk to the /vendor/cm/prebuilts folder and in the *.mk where i found the other .apks where copyed to the device i added my app and run brunch without an error.
But when i now start the app, which is on the device, it crash.
Via Logcat i found out that this apk have some .so files in it and that it can't find them.
This is because i only copied the app not the .so files in the lib directory. But i can't find some solution for my problem.
Should i now extract the .so file and copy it like the .apk to the path the app looks? or is there a better solution for doing this?
Cheers
Moritz
AOSP build system handles the shared libraries differently from Eclipse-ADT.
You will need to extract that .so from your apk and create an Android.mk to it.
To extract the .so, just unzip the apk and get it from libs folder.
Here is an example for the Android.mk to libfoo.so shared library:
LOCAL_PATH:= $(call my-dir)
# prebuilt shared library
include $(CLEAR_VARS)
LOCAL_MODULE := libfoo
LOCAL_SRC_FILES := libfoo.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_PATH := $(TARGET_OUT)/lib
LOCAL_MODULE_TAGS := optional
include $(BUILD_PREBUILT)
I am trying to build a custom rom using android custom build project. I want to add one android application to it in which there is one external jar. But whenever I run 'mm' (i.e make) in /package/apps/myApp it gives error 41 in which it is not able to recognize classes and functions from that external jar. Although eclipse is recognizing that jar and app is running perfect in eclipse but 'mm' is not able to compile it. What should i do?
Any help is greatly appreciated.
Refer to the Android.mk file inside the Calculator app. It uses external jars.
I am also pasting a sample mk file that i used for my own app. You can use it:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_TAGS := tests
LOCAL_STATIC_JAVA_LIBRARIES := libguava libgcm android-support-v4
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := SampleApp
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libguava:guava-11.0.jar libgcm:gcm.jar
include $(BUILD_MULTI_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH)
libguava and libgcm are just two namespaces(You can use your own names) for the two jars:guava-11.0.jar and gcm.jar(I have used these jars in my project) respectively. I have kept the jars inside the project folder. If you have kept the jars inside libs folder use libguava:libs/guava-11.0.jar.Also, don't use mm. Use make, but don't do make clean before it else it will remove the entire build directory and start from scratch(take a lot of time) Please accept as solution if it works....Thanks
I am having a library in vendor/xxx/libs/frameworks. I want to use that library in a separate application located at packages/apps/. when I am calling the classes of the library I am getting ClassNotFoundException. So should I declare this jar in my application makefile or manifest file. If yes How can I do that?
Here is make file of the jars
LOCAL_PATH := $(call my-dir)
# ============================================================
include $(CLEAR_VARS)
LOCAL_MODULE := xxx_core.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_PATH := $(TARGET_OUT_JAVA_LIBRARIES)
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)
==== permissions ========================
include $(CLEAR_VARS)
LOCAL_MODULE := xxx_core.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)
============================================================
You haven't mentioned if you added the jar to the buildpath of the app. To do this, right click on the project > Build Path > Add external Archives.
If this doesn't resolve this, then you should have a look at this tread: Android ClassNotFoundException
I hope this helps.
Follow this step to include the jar file in android application. It will be helpfull to you.
put your jar file in the lib folder.
Now right click on the jar file.
go to Build Path and then Configure build path..
A dialog box open now.
Click on the Add JARs.. Button.
Now select the jar file which you want to add in your app from the project list opened.
Now Click On OK.
your jar file is added in your application
If you haven't figured this out already, the build command you're looking for is:
LOCAL_STATIC_JAVA_LIBRARIES := android-common <YOUR-FRAMEWORK>
Assuming that your makefile in frameworks has a directive like
LOCAL_MODULE:= <YOUR-FRAMEWORK>
Good luck, I've found the Android build system to be a giant undocumented mess - but then they probably intended it to be that way
Use <uses-library> in application's manifest to make a reference to this .jar.