Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle exclude java class from lib replaced by own class to avoid duplicate

In Android Studio, there is a specific file (src/org/luaj/vm2/lib/jse/JavaMethod.java) that I need to overwrite from a package that is pulled in via Gradle (dependencies {compile 'org.luaj:luaj-jse:3.0.1'}).

I copied the file into my source directory with the exact same path and made my changes to it. This was working fine for an individual JUnit test case that was using it. It also looks like it is working for a normal compile of my project (unable to easily confirm at the moment).

However, when I try to run all my tests at once via a configuration of ProjectType="Android Tests", I get Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lorg/luaj/vm2/lib/jse/JavaMethod$Overload;.

Is there a specific task or command that I need to add to my Gradle file to make sure the project selects the file in my local source directory? I tried the Copy task and the sourceSets->main->java->exclude command, but neither seemed to work (I may have done them wrong). I also tried the "exclude module/group" directive under "compile" from this post.

The non-default settings for the Run/Debug Confirmation:

  • Type=Android Tests
  • Module=My module
  • Test: All in package
  • Package: "test"

All my JUnit test cases are in the "test" package.

Any answer that gets this to work is fine. If not Gradle, perhaps something in the android manifest or the local source file itself.

[Edit on 2016-07-24] The error is also happening on a normal compile when my android emulator is running lower APIs. API 16 and 19 error out, but API 23 does not.

like image 400
Dakusan Avatar asked Jul 14 '16 20:07

Dakusan


1 Answers

issue: when linking your app the linker finds two versions

  • org.luaj:luaj-jse:3.0.1:org.luaj.vm2.lib.jse.JavaMethod and
  • {localProject}:org.luaj.vm2.lib.jse.JavaMethod

howto fix: tell gradle to exclude org.luaj:luaj-jse:3.0.1:org.luaj.vm2.lib.jse.JavaMethod from building

android {
    packagingOptions {
        exclude '**/JavaMethod.class'
    }
}

I have not tried this with "exclude class" but it works for removing duplicate gpl license files a la "COPYING".

If this "exclude" does not work you can

  • download the lib org.luaj:luaj-jse:3.0.1 to the local libs folder,
  • open jar/aar with a zip-app and manually remove the duplicate class.
  • remove org.luaj:luaj-jse:3.0.1 from dependencies since this is now loaded from lib folder
like image 65
k3b Avatar answered Sep 30 '22 02:09

k3b