如何将第3方库添加到Android AOSP版本? [英] How can I add a 3rd-party library to an Android AOSP build?

查看:174
本文介绍了如何将第3方库添加到Android AOSP版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试从源代码和jar都包含Jackson.这是我的每个Android.mk文件:

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

来源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


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)


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


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.

我想念什么?我是否做了不必要的事情?

推荐答案

要包含来自第三方的第三方库 :

To include a 3rd party library from source:

  1. 将库的源代码复制到$ANDROID_BUILD_TOP/external/下的目录中(例如:$ANDROID_BUILD_TOP/external/jackson)
  2. 创建一个Android.mk文件,并将其放置在库的文件夹中(例如:$ANDROID_BUILD_TOP/external/jackson/Android.mk

  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

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)

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

    原始

    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行.例如:

  • 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:

    原始

    LOCAL_JAVA_LIBRARIES := guice gson
    

    已修改

    LOCAL_JAVA_LIBRARIES := guice gson jackson
    

  • 编译您的项目.

  • Compile your project.

    这篇关于如何将第3方库添加到Android AOSP版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆