Good day. In my application, I use a samopisky library in Java that uses the org.json dependency: json: 20151123.

In android, the default is another version. The question is - how can I use the JSON version from the library in the android project? I tried to prescribe in build.gradle

dependencies { compile "org.json:json:20151123" } 

I also copied the jar to the libs folder, but the first one gave rise to warnings, which said that the android-json version is more important, and the second one just did not allow to build apk with an error:

 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lorg/json/CDL; 

The question is - how can I use the version of org.json I need in android?

  • those. do you have two identical libraries in the project but different versions? - Kirill Stoianov
  • Show your gradle and manifest files - Kirill Stoianov
  • In android, a different version is used, but not in the config, and I have a standard config for all android projects working in AndroidStudio - A. Ovsyannikov

1 answer 1

On en-SO they write that you will not be able to replace the system dependency on any one with your own.

Most likely, if you fundamentally use a strictly defined version of any language embedded in the system, then you can use jarjar to put all your classes in another package and use these classes from this package in code. Those. change imports for the whole project.

In general, it is completely incomprehensible why you need this particular version.

  • The fact is that in this case, the application depends on whether or not, and JSONObject # keySet () is used in either, which is not in the android library. Naturally, to correct the lib in this case is wrong, since the application depends on whether it is, and not vice versa - A. Ovsyannikov
  • @ A.Ovsyannikov, it seems I didn’t quite understand your problem. Why don't you remove this dependency then, use the system library and just swap the missing method for its counterpart? - JuriySPb ♦
  • changed the description of the question, look, maybe it will be clearer - A. Ovsyannikov
  • @ A.Ovsyannikov, I'm afraid you have no choice and you need to edit - Yuriy SPb ♦
  • I had to use jarjar to “move” org.json along the path org.json.newest. But now everything works :) - A. Ovsyannikov