简体   繁体   English

当没有jar文件可用时,将另一个开源项目集成到Android项目中的规范方法是什么?

[英]What is the canonical way to integrate another open source project into an Android project when no jar file is available?

I am writing an Android app, and I'd like to be able to decode data stored in JSON format, so I downloaded jackon.databind and its dependencies into my src folder. 我正在编写一个Android应用程序,并且希望能够解码以JSON格式存储的数据,因此我将jackon.databind 及其依赖项下载到了src文件夹中。 I tried to find a jar file, but I couldn't, so I just downloaded the raw source. 我试图找到一个jar文件,但是找不到,所以我只是下载了原始文件。 I'd never integrated another open source library into my source code, and I found it far more difficult than I imagined it would be. 我从来没有将另一个开源库集成到我的源代码中,我发现它比我想象的要困难得多。

Long complicated story 漫长而复杂的故事

Skip ahead to see my question 向前跳以查看我的问题

My directory structure now looks like this: 我的目录结构现在看起来像这样:

app/src/main/java
 +-- com
      |-- fasterxml
      |    +-- jackson
      |         |-- annotation
      |         |    +-- many .java files
      |         |-- core
      |         |    +-- many .java files
      |         +-- databind
      |              +-- many .java files
      +-- my_name
           +-- my_app_name
                +-- my .java files

This didn't work, because some of the files in jackson/databind/ext had unresolved references to other packages. 这是行不通的,因为jackson/databind/ext某些文件具有未解析的对其他软件包的引用。 I tried deleting the files that were giving errors, because they didn't seem to be used by the main stuff I wanted to use, but then I got this error: 我尝试删除出现错误的文件,因为我想使用的主要材料似乎并未使用它们,但是随后出现了此错误:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
    File1: C:\Users\my_name\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.4.1\b130bcfb5a9c410c3cbd2e0adec9437e69a39e2c\jackson-core-2.4.1.jar
    File2: C:\Users\my_name\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.4.1\55605135bd7b836612e0bba7037c9669f6ccf89f\jackson-annotations-2.4.1.jar
    File3: C:\Users\my_name\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.4.1\f07c773f7b3a03c3801d405cadbdc93f7548e321\jackson-databind-2.4.1.jar

I had no idea what it meant. 我不知道那是什么意思。 This answer recommended setting multiDexEnabled to true inside defaultConfig , but that didn't help until I also followed this answer's advice to exclude the duplicated files inside the packagingOptions . 此答案建议在defaultConfig内将multiDexEnabled设置为true ,但这没有帮助,直到我还遵循此答案的建议排除了packagingOptions内的重复文件。

After all that, com.fasterxml.jackson.databind.cfg.PackageVersion remained an unresolved reference until I found the same file with a .in tacked onto the end of it. 毕竟, com.fasterxml.jackson.databind.cfg.PackageVersion仍然是一个未解决的引用,直到我发现在文件末尾带有.in的相同文件。 Removing that and renaming the package finally got the app to compile. 删除该文件并重命名该软件包最终使该应用得以编译。

Now for my question: 现在我的问题是:

All of that was incredibly frustrating and complicated for someone who had never done it before, and it was mostly trial-and-error. 对于从来没有做过的人来说,所有这些都令人感到沮丧和复杂,而且大多数都是反复试验。 Surely there was a simpler and easier way to do it? 当然有一种更简单的方法吗? Furthermore, I am now stuck manually updating these packages, as the cloned repository contained a lot of extra stuff that I didn't need because I just needed the source code, so I can't just do a git pull and expect everything to come down all right. 此外,由于克隆的存储库包含许多我不需要的额外内容,因为我只需要源代码,所以我现在仍然无法手动更新这些软件包,因此我不能只做git pull并期望一切都会来临下来好吧。 Including the entire repository in my java folder gave errors about the package not matching the directory structure. 将整个存储库包含在我的java文件夹中,会导致有关软件包与目录结构不匹配的错误。

What is the canonical way to integrate an open-source project into a java codebase when no .jar is available? 没有可用的.jar时,将开源项目集成到Java代码库中的规范方法是什么?

You don't to import the source of a library in your own project unless you need to customize it to your own requirements. 除非您需要根据自己的需求对其进行自定义,否则不要在自己的项目中导入库的源代码。 (Also if the License agrees to do that) (此外,如果许可同意这样做)

Here are the gradle dependencies required for your Jackson. 这是Jackson所需的gradle依赖项。 Add them to the app level build.gradle file. 将它们添加到应用程序级别的build.gradle文件中。

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.5'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.5'

The jar files can also be found from Maven Repository . 也可以从Maven存储库中找到jar文件。

You either use the gradle or the jar. 您可以使用gradle或jar。 Not both of them together. 不是他们两个在一起。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM