Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a 3rd-party library to an Android AOSP build?

I am trying to add the Jackson JSON library to my AOSP project. I am able to compile my project and flash it to a phone, but I get a runtime error:

E/JavaBinder( 1689): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/fasterxml/jackson/core/JsonFactory;
...
E/JavaBinder( 1689): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.fasterxml.jackson.core.JsonFactory" on path: DexPathList[[zip file "/system/framework/guice.jar", zip file "/system/framework/beanshell.jar", zip file "/system/framework/services.jar", zip file "/system/framework/ethernet-service.jar", zip file "/system/framework/wifi-service.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

I have tried including Jackson both from source, and jar. Here are my Android.mk files for each:


SOURCE Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under,.)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE:= com.fasterxml.jackson.core
include $(BUILD_JAVA_LIBRARY)

# Copy XML to /system/etc/permissions/
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := com.fasterxml.jackson.core.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)

SOURCE com.fasterxml.jackson.core.xml (referenced above)

<?xml version="1.0" encoding="utf-8"?>
<permissions>
    <library name="com.fasterxml.jackson.core.xml"
        file="/system/framework/com.fasterxml.jackson.jar" />
</permissions>

JAR Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jackson
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := jackson-core-2.5.0.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)

I have also added a jackson entry for in LOCAL_JAVA_LIBRARIES := section of the Android.mk file where I want to use Jackson (frameworks/base/services). No matter what I've tried, I get a ClassNotFoundException.

What am I missing? Have I done anything unnecessary?

like image 976
BLuFeNiX Avatar asked Aug 24 '15 16:08

BLuFeNiX


People also ask

What is a library in Android studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

What is AOSP build?

Put simply, AOSP is an open-source operating system development project maintained by Google. Since it's open-source, anyone is free to review and contribute code and fixes to the project repository. However, Google oversees its general direction and has the final say in the bulk of its development.


1 Answers

To include a 3rd party library from source:

  1. copy the library's source into a directory under $ANDROID_BUILD_TOP/external/ (ex: $ANDROID_BUILD_TOP/external/jackson)
  2. Create an Android.mk file, and place it in the library's folder (ex: $ANDROID_BUILD_TOP/external/jackson/Android.mk

    Contents of Android.mk:

    # required (setup the build environment)
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    
    # optional step to automate some pre-compilation steps for this library
    # run `mvn generate-sources` before we compile
    $(info $(shell (mvn generate-sources -f $(LOCAL_PATH)/pom.xml)))
    
    # required (the name of the library we are building)
    LOCAL_MODULE := jackson
    
    # required (paths to all directories that include source code)
    # note the difference between the := (first line) and += (every other line)
    LOCAL_SRC_FILES := $(call all-java-files-under, src/main)
    LOCAL_SRC_FILES += $(call all-java-files-under, target/generated-sources)
    
    # required (tell the build system what kind of thing we are building)
    include $(BUILD_JAVA_LIBRARY)
    
  3. Add the library to the PRODUCT_BOOT_JARS section of your mk file. Which file you edit depends on what you are building (ex: build/target/product/core_minimal.mk)

    Original

    PRODUCT_BOOT_JARS := \
        okhttp \
        core-junit \
        bouncycastle \
        ext \
        gson
    

    Modified

    PRODUCT_BOOT_JARS := \
        okhttp \
        core-junit \
        bouncycastle \
        ext \
        gson \
        jackson
    
  4. For each submodule of your AOSP project (ex: frameworks/base), that you want to have access to the library, find the makefile (ex: $ANDROID_BUILD_TOP/frameworks/base/Android.mk and add an entry for your library to the LOCAL_JAVA_LIBRARIES line. Example:

    Original

    LOCAL_JAVA_LIBRARIES := guice gson
    

    Modified

    LOCAL_JAVA_LIBRARIES := guice gson jackson
    
  5. Compile your project.

like image 157
BLuFeNiX Avatar answered Sep 27 '22 18:09

BLuFeNiX