简体   繁体   English

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

[英]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. 我正在尝试将Jackson JSON库添加到我的AOSP项目中。 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: 以下是我的每个Android.mk文件:


SOURCE Android.mk 来源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) 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 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 ). 我还在LOCAL_JAVA_LIBRARIES := Android.mk文件的部分添加了一个jackson条目,我想使用Jackson( frameworks/base/services )。 No matter what I've tried, I get a ClassNotFoundException . 无论我尝试过什么,我都会得到一个ClassNotFoundException

What am I missing? 我错过了什么? Have I done anything unnecessary? 我做了什么不必要的事吗?

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 ) 将库的源复制到$ANDROID_BUILD_TOP/external/下的目录中(例如: $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文件,并将其放在库的文件夹中(例如: $ANDROID_BUILD_TOP/external/jackson/Android.mk

    Contents of 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. Add the library to the PRODUCT_BOOT_JARS section of your mk file. 将库添加到mk文件的PRODUCT_BOOT_JARS部分。 Which file you edit depends on what you are building (ex: build/target/product/core_minimal.mk ) 您编辑的文件取决于您正在构建的内容(例如: 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: 对于 您希望访问库的AOSP项目的每个子模块 (例如: frameworks/base ),找到makefile(例如: $ANDROID_BUILD_TOP/frameworks/base/Android.mk并为您的库添加一个条目到LOCAL_JAVA_LIBRARIES行。示例:

    Original 原版的

     LOCAL_JAVA_LIBRARIES := guice gson 

    Modified 改性

     LOCAL_JAVA_LIBRARIES := guice gson jackson 
  5. Compile your project. 编译您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何使用第三方库? - Java: how to use 3rd-party library? 如何创建第三方应用程序可以使用的可嵌入Android组件? - How do I create an embeddable Android component that 3rd-party applications can use? 我可以记录在第三方(外部)库中引发并捕获的异常吗? - Can I log an exception thrown and caught in 3rd-party (external) library? 如何更改、替换或扩展 Scala 中的 3rd-party Enum (Java)? - How can I change, replace or expand a 3rd-party Enum (Java) in Scala? 如何将“aar”库添加到 AOSP? - How can I add a "aar" library to the AOSP? 如何使用第三方@ConfigurationProperties @Bean? - How work with 3rd-party @ConfigurationProperties @Bean? 将hapi第三方库(jar)添加到map-reduce - Add the hapi 3rd-party lib (jars) to map-reduce 如何从Android中的第三方库中剥离冗余软件包? - How to strip out redundant packages from 3rd-party libraries in Android? 在AOSP中,如何生成一个jar,其中包含可以稍后分发以供第三方应用使用的资源,类似于android.jar? - In AOSP how to generate a jar containing resources which can be later distributed to be used in 3rd party apps , similar to android.jar? 如何使用Maven2将多个模块和第三方JAR捆绑在ZIP中? - How do I bundle multiple modules and 3rd-party JARs in a ZIP using Maven2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM