Monday 13 March 2017

Add and use External Library or jar file in android source code(AOSP)

Hello Guys !!! Hope you all doing well !!!

Adding an external library or Jar file in android is very easy. Many of you are aware of this thing. But What about if you have to add in android source code (AOSP) .

In one of my previous post I have mention how to create a .so file from existing native (c/c++ ) code in AOSP and its uses.

In Toady post I am going to discuss addition or utilization of existing java library or Jar file in android sour code framework (AOSP).
So let say you’ve downloaded or built the library into a jar file, which we’ll call mylib.jar .


Now you want to add this lib file in android source code(AOSP) so that any one access it easily in App layer or in framework layer.

Step by step procedure is as follow :-

Step 1 Adding library to prebuilt or external folder
Copy mylib.jar file into the Android prebuilts directory, somewhere such as
<aosp-working-dir>/prebuilts/misc/common/mylib/
Or you can add it in external folder like this
<aosp-working-dir>/external/mylib/
In that same directory( here directory is mylib ), create a makefile called Android.mk with the following contents

$LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := mylib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)
You can specify any name you wish for LOCAL_MODULE, but ensure that LOCAL_SRC_FILES specifies your jar file exactly as it is named.

Step 2 Building the added jar file
Now it is time to check that the added library file is building or not . For Building, go to the top-level Android source directory and run make prebuilts, if you have added your library in prebuilt folder.
Or, navigate to the directory where you added the library (example external/mylib ) and run mm -B command.
The output of the build should include one “Install:” line per library that you added.

Step 3 Add and use the Library in the Android Frameworks
Now that we have the library properly setup and built. For using this library, we have to include it in framework/base folder android.mk file. The complete path is :-
<aosp- working-dir>frameworks/base/Android.mk


After that we can use it by including it as a static library in the makefile of any other Android module.

For example, if we wanted to use it in a system service like the AlarmManagerService or any other code in the frameworks/base project.

Taking Android M 6.0.1 as an example, we should insert the LOCAL_STATIC_JAVA_LIBRARIES in Line 427 of the framework base’s Android.mk file

...
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-libart conscrypt okhttp core-junit bouncycastle ext
LOCAL_STATIC_JAVA_LIBRARIES += mylib anotherlib  ### add this line
LOCAL_MODULE := framework
...
Now every thing is set properly. Give a make update-api command . Build your system image and start using newly added jar file in your app or in framework services.

Adding additional libraries in the same directory

If you have more jar files and you want to use it. In that case, you can put them in the same directory, and even add them to the same Android.mk make file. Follow the same step mention above.
Just add the other library in the same Android.mk file such as :

$LOCAL_PATH := $(call my-dir)
## for mylib
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := mylib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)

## for anotherlib
include $(CLEAR_VARS)
LOCAL_MODULE := anotherlib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := anotherlib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)

Using the library in App or frameworks services
You can use your library in app just putting your library name inside your app Android.mk file like this
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 mylib
Then Import your method in your application and use it easily.

For Using this library in any framework services, it is easy. Because we already mention this libraries inside framework/base/Android.mkfile. 
So no need to do any extra effort for accessing it. Just import your jar library method in your .java file and use it.

Thanks
Saurabh
Happy Coding !!!


3 comments:

  1. Replace:
    $LOCAL_PATH := $(call my-dir)
    With:
    LOCAL_PATH := $(call my-dir)

    And it worked just fine!

    ReplyDelete
  2. hello,
    i just want to say that you have clearly mention the process very clearly and i also found it is very much easy to understand. keep sharing.

    iphone app development company in chennai

    ReplyDelete
  3. i got the No dex files in zip file '/system/framework/mylib.jar': Entry not found
    error.
    Can you help?

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...