如何将第三方库添加到Android AOSP构建中?

时间:2015-08-24 16:14:52

标签: java android makefile android-source

我正在尝试将Jackson JSON库添加到我的AOSP项目中。我能够编译我的项目并将其刷新到手机,但我收到运行时错误:

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]]

我试过从源头和jar中包括杰克逊。以下是我的每个Android.mk文件:

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(上文引用)

<?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)

我还在Android.mk文件的jackson部分添加了LOCAL_JAVA_LIBRARIES :=条目,我想要使用Jackson(frameworks/base/services)。无论我尝试过什么,我都会得到ClassNotFoundException

我错过了什么?我做了什么不必要的事吗?

1 个答案:

答案 0 :(得分:6)

要包含第三方库来自

  1. 将图书馆的来源复制到$ANDROID_BUILD_TOP/external/下的目录中(例如:$ANDROID_BUILD_TOP/external/jackson
  2. 创建Android.mk文件,并将其放在库的文件夹中(例如:$ANDROID_BUILD_TOP/external/jackson/Android.mk

    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. 将库添加到PRODUCT_BOOT_JARS文件的mk部分。您编辑的文件取决于您正在构建的内容(例如:build/target/product/core_minimal.mk

    <强>原始

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

    <强>修饰

    PRODUCT_BOOT_JARS := \
        okhttp \
        core-junit \
        bouncycastle \
        ext \
        gson \
        jackson
    
  4. 对于您的AOSP项目的每个子模块(例如:frameworks/base),您想要访问该库,找到makefile (例如:$ANDROID_BUILD_TOP/frameworks/base/Android.mk并将您的资料库条目添加到LOCAL_JAVA_LIBRARIES行。示例:

    <强>原始

    LOCAL_JAVA_LIBRARIES := guice gson
    

    <强>修饰

    LOCAL_JAVA_LIBRARIES := guice gson jackson
    
  5. 编译项目。