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. I tried to find a jar file, but I couldn't, so I just downloaded the raw source. 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. 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.

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. 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. Including the entire repository in my java folder gave errors about the package not matching the directory structure.

What is the canonical way to integrate an open-source project into a java codebase when no .jar is available?

1

There are 1 answers

4
Neerajlal K On BEST ANSWER

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. Add them to the app level build.gradle file.

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.

You either use the gradle or the jar. Not both of them together.