0

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?

Community
  • 1
  • 1
Jon McClung
  • 1,619
  • 20
  • 28
  • 1
    Here are the Jackson jar files. It also has gradle dependencies. [Maven Repository](https://mvnrepository.com/artifact/com.fasterxml.jackson.core) – K Neeraj Lal Dec 18 '16 at 04:53
  • 2
    *I downloaded jackon.databind and its dependencies into my src folder* You don't do this. You declare the dependency in your Gradle build file. Check Central--there is almost *always* a jar there. – chrylis -cautiouslyoptimistic- Dec 18 '16 at 05:00
  • @KNeerajLal Thanks, that's super helpful. – Jon McClung Dec 18 '16 at 05:02
  • @chrylis That's exactly why I was asking the question, because (as I said) I've never done it before and didn't know how. I assume you're referring to http://search.maven.org/ (the first google result). I didn't know about that website before. Thank you for telling me that's a good place to look. – Jon McClung Dec 18 '16 at 05:03
  • 1
    Yes, that's the Central Repository (formerly Maven Central, before Gradle became a serious competitor). Learn to love it, especially the advanced search features (which can help you find packages by class name). – chrylis -cautiouslyoptimistic- Dec 18 '16 at 05:13

1 Answers1

2

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.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
  • This seems to work even without me adding the jars. Is it somehow downloading the appropriate files over the internet? How does it know where to look? Or is it possible it's simply cached the files and I really do need to download the .jars and put them somewhere special? – Jon McClung Dec 18 '16 at 05:20
  • 1
    YOu don't need to download the jars when using this technique- you're using versions of those libraries that were uploaded to a central repository. The gradle system downloads them for you and will include it in the final apk. – Gabe Sechan Dec 18 '16 at 05:23
  • 1
    There is a `repositories` tag inside your project level `build.gradle`. It looks for the dependencies in those locations. – K Neeraj Lal Dec 18 '16 at 05:23
  • Okay, it looks like it's going through something called jcenter (https://bintray.com/bintray/jcenter). I'm really glad there was a simple solution to this problem that actually makes sense to me now. I really felt like there should be, which is why I posted the question. – Jon McClung Dec 18 '16 at 05:49