I am trying to add external JAR files (e.g., gson or eventbus) to my AOSP build. I tried it in two different ways:
Adding them to prebuilts/misc/common/mylibs/ and creating a Android.mk file for the dir. Then, I merely include the symbolic name of the lib in my app's Android.mk file.
Adding them in a subdir libs in my custom app source code and add it via the app's Android.mk file directly.
However, both approaches are yielding me similar errors, the following for the second method:
ninja: error: 'packages/apps/Car/MyApp/packages/apps/Car/Myapp/libs/gson-2.6.2.jar', needed by 'out/target/common/obj/JAVA_LIBRARIES/gson_intermediate/classes.jack', missing and no known rule to make it.
I noticed the weird repeating path in the error message. Does anybody have an idea what I am doing wrong?
Here is my Android.mk file for the second way:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_PACKAGE_NAME := MyApp
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := optional
LOCAL_PRIVILEGED_MODULE := true
LOCAL_STATIC_JAVA_LIBRARIES += jsr305 gson
LOCAL_STATIC_ANDROID_LIBRARIES := android-support-v4
LOCAL_USE_AAPT2 := true
include packages/apps/Car/libs/car-stream-ui-lib/car-stream-ui-lib.mk
include packages/services/Car/car-support-lib/car-support.mk
include $(BUILD_PACKAGE)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := gson:$(LOCAL_PATH)/libs/gson-2.6.2.jar
include $(BUILD_MULTI_PREBUILT)
Removing $(LOCAL_PATH) from the path fixed it.
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?
In our Android project, we have several tens of C and CPP files which we #include through one container file for the build. The container file is listed in LOCAL_SRC_FILES, since that's what's compiled. However, when I edit one of the included .cpp files, that doesn't seem to trigger a build.
If I were in total control of the make file, I'd just include all those files as dependencies for the relevant compile step. However, android.mk handles making all those compile dependencies in general, so I don't know where to insert them.
I have a three CPP file in this path
..src\main\cpp
mainClass.cpp
native-handler.h
native-handler.cpp
And I include to in android.mk file like this
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libjson-c
LOCAL_SRC_FILES := ../prebuild/libjson/$(TARGET_ARCH_ABI)/libjson-c.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libcurl
LOCAL_SRC_FILES := ../prebuild/libcurl/$(TARGET_ARCH_ABI)/libcurl.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Native
LOCAL_SRC_FILES := ../cpp/mainClass.cpp ../cpp/native-handler.cpp
LOCAL_C_INCLUDES := ../cpp/native-handler.h ../prebuild/include/json-c/json.h ../prebuild/include/curl/curl.h
LOCAL_LDLIBS := -lz -llog -ljnigraphics
LOCAL_STATIC_LIBRARIES := libjson-c libcurl
include $(BUILD_SHARED_LIBRARY)
Android.mk offers some features that Android.bp/Soong deliberately avoids (e.g. include files from parent directories). The goal of Soong is to completely replace the Android.mk based build system eventually. So if you happen to find a solution that works with Android.mk files you might have the same problem again in a future Android release.
The Android build system will detect changes to all files specified (e.g. as LOCAL_SRC_FILES in Android.mk or srcs in Android.bp) and trigger a rebuild of the module. I guess you have to switch to adding all source files that your container file included to the module configuration.
I am writing an android app that wants to make JNI calls into a shared library built in using the NDK. The trick is this shared library calls functions provided by OTHER shared libraries. The other shared libraries are C libraries that have been compiled elsewhere.
Here's what I've tried:
My Environment:
I'm working in Eclipse. I've added native support and have a jni library. In that library I have my code and a \lib directory where I have copied my other .so files.
Attempt #1 Android.mk: Just telling it where the libs are
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds just fine, but when I try to run I get errors indicating that dlopen(libnative_lib) failed because it couldn't load libsupport_lib1.
Coming here I found this:
Can shared library call another shared library?
which said that I needed to call load library on all necessary libraries. Great!
Attempt #2 Opening each library first
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
Again, this builds just fine, however when I run I get a new error:
couldn't load libsupport_lib1. findLibrary returned null.
Now we're getting somewhere. It must not be loading the libraries over to the target.
Attempt #3 Copying .so files into project/libs/armeabi
Didn't work. When Eclipse builds it deleted the files I dropped in there.
Attempt #4 Creating a new module for each library
So then I found this:
Android NDK: Link using a pre-compiled static library
It's about static libraries, but maybe I am having a similar problem. The gist is that I need to declare a module for each library. So my new Android.mk looks like this:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(BUILD_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(BUILD_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds! Even better, armeabi has the sos now! Even BETTER I get the following messages when I try to run it (telling me that support_lib1 and 2 were opened by LoadLibrary:
Trying to load lib /data/app-lib/com.example.tst/libsupport_lib1.so
added shared lib /data/app-lib/com.example.tst/libsupport_lib1.so
no JNI_OnLoad found in /data/app-lib/com.example.tst/libsupport_lib1.so, skipping init
but then...
dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
Edit: Attempt 5: Use PREBUILT_SHARED_LIBRARY
So I found this:
How can i Link prebuilt shared Library to Android NDK project?
which seems to be exactly what I'm asking. Their answer seems to be 'don't use 'build_shared_library' but instead 'use PREBUILT_SHARED_LIBRARY
Okay, let's try.
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
Build... fails! The build complains about missing symbols now.
Edit: Attempt 6: Flatten everything
So I went back to the prebuilts documentation in the NDK. It says:
Each prebuilt library must be declared as a single independent module to the build system. Here is a trivial example where we assume that the file "libfoo.so" is located in the same directory than the Android.mk below:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
include $(PREBUILT_SHARED_LIBRARY)
Notice that, to declare such a module, you really only need the following:
Give the module a name (here 'foo-prebuilt'). This does not need to correspond to the name of the prebuilt library itself.
Assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. As usual, the path is relative to your LOCAL_PATH.
Include PREBUILT_SHARED_LIBRARY, instead of BUILD_SHARED_LIBRARY, if you are providing a shared, library. For static ones, use PREBUILT_STATIC_LIBRARY.
A prebuilt module does not build anything. However, a copy of your prebuilt shared library will be copied into $PROJECT/obj/local, and another will be copied and stripped into $PROJECT/libs/.
So let's try flattening everything out to match the trivial example. I copied my libraries out of their cozy /lib folder and put them in the jni root. I then did this:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
and... same error. Moreover I'm most definitely NOT seeing library files getting copied to $PROJECT/obj/local.
sooooo.... now what?
Your problem is with the naming convention. NDK and Android insist on the shared library names to always begin with lib. Otherwise, the libraries will not be linked properly, and not copied to the libs/armeabi folder properly, and not installed on the device (copied to /data/data/package/lib directory properly.
If you rename support_lib1.so to libsupport_1.so and support_lib2.so to libsupport_2.so, and put these two files in jni/lib directory, then your Attempt #5 will work with minor change:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := lib/libsupport_1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := lib/libsupport_2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
BTW, I don't think you need this -L$(SYSROOT)/../usr/lib.
PS Don't forget to update the Java side, too:
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
Not sure if this is exactly where you are at, but here's what I know about these sorts of things.
Make each prebuilt libary its own separate Makefile. Multiple targets in Android.mk tends to get wonky. Sad.
Include each make file using $(call import-add-path) and $(call import-module)
Export as much as you can from the prebuilt's make files, using the LOCAL_EXPORT_ family of variables.
Prebuilt Shared Library Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_module_name
MY_LIBRARY_NAME := shared_library_name
### export include path
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
### path to library
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib$(MY_LIBRARY_NAME).so
### export dependency on the library
LOCAL_EXPORT_LDLIBS := -L$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/
LOCAL_EXPORT_LDLIBS += -l$(MY_LIBRARY_NAME)
include $(PREBUILT_SHARED_LIBRARY)
This is assuming that the prebuilt libaries live in a dir structure like this
+ SharedProjectFolderName
+--- Android.mk
+--- include/
+-+- libs/$(TARGET_ARCH_ABI)/
|- libshared_library_name.so
If you are not building for multiple ABI, I guess you can leave that bit out
The Project's Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_jni_module
## source files here, etc...
### define dependency on the other library
LOCAL_SHARED_LIBRARIES := my_module_name
include $(BUILD_SHARED_LIBRARY)
$(call import-add-path,$(LOCAL_PATH)/path/to/myLibraries/)
$(call import-module,SharedProjectFolderName)
$(call import-module,AnotherSharedProject)
I recommend you put all shared libraries in one folder. When you say $(call import-module,SharedProjectFolderName) it looks for a folder containing an Android.mk along the search path you told it (import-add-path)
By the way, you probably shouldn't specify LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib. It should be finding the proper libs from NDK by itself. Adding more linker paths will probably confuse it. The proper way is to export the linker paths as flags from the sub-modules.
ALSO, you can use ndk-build V=1 to get a ton of info on why it can't find paths, etc
The -L option gives the linker a directory path in which to look for libraries. The -l option gives the linker a library file name to link in. Library file names must begin with "lib". Your libraries should be named libsupport_lib1.so and libsupport_lib2.so. If you do that, then this is probably what you should do (replacing attempt #1):
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog -lsupport_lib1 -lsupport_lib2
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib
The linker will prefix the library name you specify using -l with "lib" and suffix it with ".so". (Why do you have -L$(SYSROOT)/../usr/lib?)
I believe that attempts #1 and #2 failed because you did not link your libraries into your executable - they are not mentioned in a -l option. By the way, you can verify this yourself. Unzip the .apk file and look in the lib directory and subdirectories. Are your .so files in there?
Looking at the error:
but then... dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
Can you supply the entire message? dlopen() loads and links libraries into the running process.
I want to add some features into Browser app by start an activity from another android application. It gives me package does not exist while I make the Main Project. Notice that I see the AndroidLib is built successfully into an out/target/product/generic/data/app/AndroidLib.apk
Here are two android.mk files:
AndroidLib(a normal Android App)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_JAVA_LIBRARIES := \
google-ps \
android-support-v4 \
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
LOCAL_MODULE := AndroidLib
LOCAL_PACKAGE_NAME := AndroidLib
LOCAL_MODULE_TAGS := tests
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := google-ps:libs/google-play-services.jar
include $(BUILD_PACKAGE)
# additionally, build tests in sub-folders in a separate .apk
include $(call all-makefiles-under,$(LOCAL_PATH))
Browser App
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := \
android-common \
guava \
android-support-v13 \
android-support-v4 \
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
src/com/android/browser/EventLogTags.logtags
LOCAL_PACKAGE_NAME := Browser
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_EMMA_COVERAGE_FILTER := *,-com.android.common.*
# We need the sound recorder for the Media Capture API.
LOCAL_REQUIRED_MODULES := SoundRecorder, AndroidLib
include $(BUILD_PACKAGE)
# additionally, build tests in sub-folders in a separate .apk
include $(call all-makefiles-under,$(LOCAL_PATH))
Then I do:
make -j Browser
It gives me:
packages/apps/Browser/src/com/android/browser/BrowserActivity.java:36: package com.om.AndroidLib does not exist
import com.om.AndroidLib.MainActivity;
Update
Essentially your problem is that the line:
LOCAL_REQUIRED_MODULES := SoundRecorder, AndroidLib
Tells the Android build that your Browser depends on AndroidLib being built first, however it will not be linked to AndroidLib in any way. To do that you need:
LOCAL_JAVA_LIBRARIES := AndroidLib
Except that requires the AndroidLib to be a java library project, using the build type:
include $(BUILD_JAVA_LIBRARY)
And java libraries cannot include resources, unfortunately. And there is no way to link one app to another app in the Android build system.
We are working around that issue frequently, and it is frankly a bit of a mess to do so. But the only workaround we have found is to include all the resources directly in the apps that use those resources. Sorry.
How to make your AndroidLib a java library and link to it
Your android lib should probably be built as a java library, and not as a app package.
So instead of:
include $(BUILD_PACKAGE)
You should probably do:
include $(BUILD_JAVA_LIBRARY)
Then you lib will be placed in out/target/product/generic/system/framework/AndroidLib.jar along with the other system libs.
Also you do not need a LOCAL_MODULE_NAME for a lib, it is only applicable for an app. So strip that.
And you should probably consider changing your LOCAL_MODULE_TAGS to:
LOCAL_MODULE_TAGS := user
tests indicates that you lib should only be built for testing, user says to build you lib for all user builds (and implicitly also for engineering builds)
In you app Android.mk you should then add a line:
LOCAL_JAVA_LIBRARIES := AndroidLib
Such that your app will be built with AndroidLib in the classpath.
In you apps AndroidManifest.xml you also need to put a uses library in the application tag:
<uses-library android:name="AndroidLib" />
And last you need to create a permissions xml file, with the following content:
<?xml version="1.0" encoding="utf-8"?>
<permissions>
<library name="AndroidLib"
file="/system/framework/AndroidLib.jar"/>
</permissions>
And update your product configuration file such that the xml file will be deployed to target:
PRODUCT_COPY_FILES += device/XXX/libs/AndroidLib.xml:system/etc/permissions/AndroidLib.xml
Replace device/XXX/libs/ with the path to the permissions xml file.
This is how we add and use an Android library in our Android builds. I hope it helps. :)
I know I'm digging this out of the grave a little bit, but there was never really an answer for how to actually include an Android Library Project (or other apk with resources) within an AOSP built application.
Here is what I have done with two projects. "main" is the main application I am building the apk for. "util" is a utility library with resources I am linking against. Both of the projects reside in external/seabold/{package}:
Android.mk in seabold/
LOCAL_PATH := $(call my-dir)
UTIL_PATH := $(LOCAL_PATH)/util
UTIL_JAVA_SRC := $(call all-java-files-under, util/src)
#Filter out if the util library isn't present
UTIL_RES_DIR := $(wildcard $(UTIL_PATH)/res)
include $(call all-subdir-makefiles)
Android.mk in seabold/main/
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := main
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
#Since the BUILD_PACKAGE makefile expects java files to be relative paths,
#we need to add the ../ in front of each file (which is stored as relative
#to seabold/
LOCAL_SRC_FILES += $(patsubst %.java,../%.java,$(UTIL_JAVA_SRC))
#Make sure both packages' resources are included
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res $(UTIL_RES_DIR)
#Since we are including resouces for two packages (This app and the util lib),
#we need to tell aapt to generate an R.java for the util package as well
LOCAL_AAPT_FLAGS := --generate-dependencies --extra-packages com.seabold.util --auto-add-overlay
include $(BUILD_PACKAGE)
Be careful here to make sure you fiddle with the filenames correctly. For me, the files defined in UTIL_JAVA_SRC are in the form of util/src/com/seabold/util/*.java. Since, the build script for the main app expects java files to be relative paths to the location of Android.mk, I'm adding a ../ to properly get to the util library's path.
The key for resources is the LOCAL_AAPT_FLAGS. This tells the resource packager to make sure it generates an R.java for the package in the utility library. Without this, all of the utility's resources will get put into the main applications R.java, and the utility classes will fail to compile because its R.java will be missing.
To add a pre-build jar file to the framework I did that:
Locate a folder with the jar and the Android.mk file under
prebuilds/misc/common/
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <jar name>
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := <jar name>.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)
Run make on the Android.mk file: make prebuilds/misc/common/Android.mk
Add the jar file to the frameworks/base/services/Android.mk:
LOCAL_STATIC_JAVA_LIBRARIES := $(addprefix services.,$(services)) jar-name
I've stumbled over a solution, on how a java library can access resources. The resources are build as a separate apk. The jar-lib can be used from any app that says so in its Manifest. The java-library-code has the R.java of the resources-apk and uses createPackageContext() to gain access to the actual resources. createPackageContext() gets the app-context as parameter which each app must provide whenever it uses a java-lib-function that needs access to the resources.
I'm not that deep into the AOSP-build-system yet, so I'm hoping what I document here makes sense. It's not my solution either, I just improoved on something that some other developer created here at my job so I don't know where he got the approach from.
for the resource-only-apk we have:
Android.mk
include $(CLEAR_VARS)
LOCAL_PATH := $(call my-dir)
LOCAL_PACKAGE_NAME := shared-resources
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_APPS)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
include $(BUILD_PACKAGE)
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.company.common.resources">
<application android:hasCode="false"/>
</manifest>
For the jar-lib we have:
Android.mk
LOCAL_PATH:= $(call my-dir)
####################
# compile the jar #
include $(CLEAR_VARS)
LOCAL_MODULE := helper-lib
LOCAL_SRC_FILES := $(call all-java-files-under, src)
# add package-context-ressources
resource-intermediates := $(call intermediates-dir-for,APPS,shared-resources,,common)
shared_res := $(resource-intermediates)/src/my/company/common/resources/R.java
$(shared_res): $(resource-intermediates)/src/R.stamp
LOCAL_GENERATED_SOURCES := $(shared_res)
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_JAVA_LIBRARIES)
LOCAL_ADDITIONAL_DEPENDENCIES := helper-lib.xml
include $(BUILD_JAVA_LIBRARY)
######################
# deliver permisions #
include $(CLEAR_VARS)
LOCAL_MODULE := helper-lib.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/permissions
LOCAL_SRC_FILES := helper-lib.xml
include $(BUILD_PREBUILT)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.company.helper">
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<uses-permission android:name="android.permission.MANAGE_USERS" />
<application />
</manifest>
helper-lib.xml
<?xml version="1.0" encoding="utf-8"?>
<permissions>
<library name="helper-lib" file="/vendor/framework/helper-lib.jar" />
</permissions>
Last but not least here is how we declare them in that over-all-makefile that says what all to include -> base_packages.mk
PRODUCT_PACKAGES += \
my.company.common \
shared-resources \
helper-lib
I am trying to add my app in Android_source/Development/apps/ folder. I confirmed that my code is compiled, as if i add syntax error compiler stopped there.
But the thing is that my app is not getting shown in apps menu. should i add my app package somewhere else too.
Android.Mk file content(i copied these contents from Bluetooth App in packages/apps/Bluetooth)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := \
$(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := Bluetooth_LE
LOCAL_CERTIFICATE := platform
#LOCAL_JNI_SHARED_LIBRARIES := libbluetooth_jni
LOCAL_JAVA_LIBRARIES := javax.obex
LOCAL_STATIC_JAVA_LIBRARIES := com.android.vcard
LOCAL_REQUIRED_MODULES := libbluetooth_jni bluetooth.default
LOCAL_PROGUARD_ENABLED := disabled
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))
It seems that you've forgot to include your package into the build. Put the name of your module (Bluetooth_LE) into appropriate build file (for instance, into build/target/product/core.mk) into section PRODUCT_PACKAGES.