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

11

我正在尝试将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包中引入Jackson。下面是每个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)

源代码来自于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文件的LOCAL_JAVA_LIBRARIES :=部分中添加了一个jackson条目,以便在使用Jackson(frameworks/base/services)时使用。无论我尝试什么,我都会得到一个ClassNotFoundException

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


可能是重复的问题,类似于https://dev59.com/e3XZa4cB1Zd3GeqPAfcJ。看起来你需要在你的清单文件中添加一个依赖项<uses-library android:name="yourlib" />。 - Mixaz
1个回答

15

要包含第三方库的源代码:

  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)
    
    将库添加到mk文件的PRODUCT_BOOT_JARS部分。你需要编辑哪个文件取决于你正在构建什么(例如: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
    
    对于您的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
    
  3. 编译你的项目。


你能帮我解决这个问题吗?http://stackoverflow.com/questions/40114369/two-external-libraries-that-one-depends-on-the-other - shlatchz
你使用的是哪个安卓版本? - Bharat Vankar
@BharatVankar 我在使用Android 5.x和6.x时编写了这篇文章。它可能适用于其他版本,也可能不适用。 - BLuFeNiX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接