How can I remove a library from aar using gradle?

I am currently creating an Android plugin for Unity. Because of this, I need to include the single classes.jar which provides me with some methods in the com.unity3d.player package.

However, when I do ./gradlew assemble

, it also packs the .jar classes into an .aar file in libs / classes.jar.

Now when I add this .aar to Unity and try to build, I get the following error:

Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/a$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/a$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/a$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/b$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/b$2;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/b;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/c;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/d$1;

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:608)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
    at com.android.dx.command.dexer.Main.run(Main.java:277)
    at com.android.dx.command.dexer.Main.main(Main.java:245)
    at com.android.dx.command.Main.main(Main.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at SDKMain.main(SDKMain.java:129)

      

Unity tries to add the class.jar to it and it will be instantiated again, but since the class.jar is already in the .aar it won't work.

How can I exclude class.jar from .aar?

EDIT

This is what I can do with command line to remove classes.jar

zip --delete tapfortapunity-release.aar "libs/*"

      

How should I call this using gradle?

+3


source to share


1 answer


UPDATE: I just found out that this problem solves itself if I just remove the dependency unity.jar

on my file build.gradle

:

//dependencies {
//    compile files('src/main/libs/unity.jar')
//}

      

(the code compiles just fine, oddly enough, and not unity.jar

in the file .aar

...).

(just noticed that the code was compiled because there was no reference to UnityPlayer*

stuff ...)


Same problem. I looked a bit and it seems like there is no clean way to solve the problem (yet!), So I guess it all boils down to this:



  • create a task chain to install your only plugin (e.g. a task to create a directory Plugin/Android

    , one to copy the AAR file and native libraries if any, etc.)
  • remove the file from the zip using a task, the previous one will be dependsOn

Step 2 can be done in one of the following ways:

  • create a task Exec

    that will simply execute the command you specified (which is probably what I will do since I am not proficient with gradle ...). Lightweight but not portable (depends on additional utility).
  • create a task JavaExec

    and use Java7 support for zip to delete the entry (probably a better solution, but I don't really have much experience).
  • try to create a new jar using the suggestion given in this post: Post on gradle -user describing how to create a jar (or aar ...) using gradle as Gradle supports zip files but cannot modify them in place (which means that it cannot delete data from them). Harder, but don't rely on external tools, so you'll end up with portability as a result.

It may not be an elegant solution, but probably the best, but it is very simple and works well

Hope it helps

0


source







All Articles