Home » Android » Error building Android Library for Unity 3D 5.3 – Android Studio

Error building Android Library for Unity 3D 5.3 – Android Studio

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m working with Android Studio trying to build an Android Library for Unity3d 5.3. For that, I created an Android Library module and I added the classes.jar file to work with UnityPlayer as you can see in the next image:

enter image description here

Then, I generated the module and I’ve got the file ‘wrapper-debug.aar’ that I put it inside the ‘\Assets\Plugins\Android’ . Finally, when I build, I’ve got the following error:

Error building Player: CommandInvokationFailure: Unable to convert classes   into dex format. See the Console for details.
C:/Program Files/Java/jdk1.8.0_45\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Users/.../AppData/Local/Android\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar" -
stderr[
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/NativeLoader;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$1;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$10;
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)
Caused by: java.lang.InterruptedException: Too many errors
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:600)
... 9 more
]
stdout[
processing archive G:\...\Unity5\Projects\AndroidPlugin\AndroidPlugin\Temp\StagingArea\android-libraries\wrapper-debug\bin\classes.jar...
processing unitywrapper/wrapper/BuildConfig.class...
processing unitywrapper/wrapper/Wrapper.class...
processing archive G:\...\Unity5\Projects\AndroidPlugin\AndroidPlugin\Temp\StagingArea\android-libraries\wrapper-debug\libs\.\classes.jar...
processing bitter/jnibridge/JNIBridge.class...
processing bitter/jnibridge/JNIBridge$a.class...
processing com/unity3d/player/NativeLoader.class...
processing com/unity3d/player/ReflectionHelper.class...
processing com/unity3d/player/ReflectionHelper$1.class...
processing com/unity3d/player/ReflectionHelper$a.class...
processing com/unity3d/player/UnityPlayer.class...
processing com/unity3d/player/UnityPlayer$1.class...
processing com/unity3d/player/UnityPlayer$10.class...
processing com/unity3d/player/UnityPlayer$11.class...
processing com/unity3d/player/UnityPlayer$12.class...
processing com/unity3d/player/UnityPlayer$13.class...
processing com/unity3d/player/UnityPlayer$14.class...
processing com/unity3d/player/UnityPlayer$15.class...
processing com/unity3d/player/UnityPlayer$15$1.class...
processing com/unity3d/player/UnityPlayer$16.class...
processing com/unity3d/player/UnityPlayer$17.class...
processing com/unity3d/player/UnityPlayer$18.class...
processing com/unity3d/player/UnityPlayer$19.class...
processing com/unity3d/player/UnityPlayer$2.class...
processing com/unity3d/player/UnityPlayer$20.class...
processing com/unity3d/player/UnityPlayer$3.class...
processing com/unity3d/player/UnityPlayer$4.class...
processing com/unity3d/player/UnityPlayer$5.class...
processing com/unity3d/player/UnityPlayer$6.class...
processing com/unity3d/player/UnityPlayer$7.class...
processing com/unity3d/player/UnityPlayer$8.class...
processing com/unity3d/player/UnityPlayer$9.class...
processing com/unity3d/player/UnityPlayer$a.class...
processing com/unity3d/player/UnityPlayer$b.class...
processing com/unity3d/player/UnityPlayer$c.class...
processing com/unity3d/player/UnityPlayerActivity.class...
processing com/unity3d/player/UnityPlayerNativeActivity.class...
processing com/unity3d/player/UnityPlayerProxyActivity.class...
processing com/unity3d/player/UnityWebRequest.class...
processing com/unity3d/player/WWW.class...
processing com/unity3d/player/a.class...
processing com/unity3d/player/a$1.class...
processing com/unity3d/player/a$2.class...
processing com/unity3d/player/a$a.class...
processing com/unity3d/player/b.class...
processing com/unity3d/player/b$1.class...
processing com/unity3d/player/b$2.class...
processing com/unity3d/player/c.class...
processing com/unity3d/player/d.class...
processing com/unity3d/player/d$1.class...
processing com/unity3d/player/d$2.class...
processing com/unity3d/player/e.class...
processing com/unity3d/player/f.class...
processing com/unity3d/player/g.class...
processing com/unity3d/player/h.class...
processing com/unity3d/player/i.class...
processing com/unity3d/player/j.class...
processing com/unity3d/player/k.class...
processing com/unity3d/player/k$1.class...
processing com/unity3d/player/k$2.class...
processing com/unity3d/player/k$2$1.class...
processing com/unity3d/player/k$2$1$1.class...
processing com/unity3d/player/l.class...
processing com/unity3d/player/l$1.class...
processing com/unity3d/player/m.class...
processing com/unity3d/player/n.class...
processing com/unity3d/player/n$1.class...
processing com/unity3d/player/o.class...
processing com/unity3d/player/p.class...
processing com/unity3d/player/p$1.class...
processing com/unity3d/player/q.class...
processing com/unity3d/player/r.class...
processing com/unity3d/player/s.class...
processing com/unity3d/player/s$1.class...
processing com/unity3d/player/s$2.class...
processing com/unity3d/player/s$3.class...
processing com/unity3d/player/t.class...
processing com/unity3d/player/u.class...
processing com/unity3d/player/v.class...
processing com/unity3d/player/w.class...
processing com/unity3d/player/w$1.class...
processing org/fmod/FMODAudioDevice.class...
processing org/fmod/a.class...
processing bin\classes\.\com\androidplugin\android\R.class...
processing bin\classes\.\com\androidplugin\android\R$attr.class...
processing bin\classes\.\com\androidplugin\android\R$drawable.class...
processing bin\classes\.\com\androidplugin\android\R$string.class...
processing bin\classes\.\unitywrapper\wrapper\R.class...
processing bin\classes\.\unitywrapper\wrapper\R$attr.class...
processing bin\classes\.\unitywrapper\wrapper\R$drawable.class...
processing bin\classes\.\unitywrapper\wrapper\R$string.class...
processing archive bin\classes.jar...
processing bitter/jnibridge/JNIBridge.class...
processing bitter/jnibridge/JNIBridge$a.class...
processing com/unity3d/player/NativeLoader.class...
processing com/unity3d/player/ReflectionHelper.class...
processing com/unity3d/player/ReflectionHelper$1.class...
processing com/unity3d/player/ReflectionHelper$a.class...
processing com/unity3d/player/UnityPlayer.class...
processing com/unity3d/player/UnityPlayer$1.class...
processing com/unity3d/player/UnityPlayer$10.class...
processing com/unity3d/player/UnityPlayer$11.class...
processing com/unity3d/player/UnityPlayer$12.class...
processing com/unity3d/player/UnityPlayer$13.class...
processing com/unity3d/player/UnityPlayer$14.class...
processing com/unity3d/player/UnityPlayer$15.class...
processing com/unity3d/player/UnityPlayer$15$1.class...
processing com/unity3d/player/UnityPlayer$16.class...
processing com/unity3d/player/UnityPlayer$17.class...
processing com/unity3d/player/UnityPlayer$18.class...
processing com/unity3d/player/UnityPlayer$19.class...
processing com/unity3d/player/UnityPlayer$2.class...
processing com/unity3d/player/UnityPlayer$20.class...
processing com/unity3d/player/UnityPlayer$3.class...
processing com/unity3d/player/UnityPlayer$4.class...
processing com/unity3d/player/UnityPlayer$5.class...
processing com/unity3d/player/UnityPlayer$6.class...
processing com/unity3d/player/UnityPlayer$7.class...
processing com/unity3d/player/UnityPlayer$8.class...
processing com/unity3d/player/UnityPlayer$9.class...
processing com/unity3d/player/UnityPlayer$a.class...
processing com/unity3d/player/UnityPlayer$b.class...
processing com/unity3d/player/UnityPlayer$c.class...
processing com/unity3d/player/UnityPlayerActivity.class...
processing com/unity3d/player/UnityPlayerNativeActivity.class...
processing com/unity3d/player/UnityPlayerProxyActivity.class...
processing com/unity3d/player/UnityWebRequest.class...
processing com/unity3d/player/WWW.class...
processing com/unity3d/player/a.class...
processing com/unity3d/player/a$1.class...
processing com/unity3d/player/a$2.class...
processing com/unity3d/player/a$a.class...
processing com/unity3d/player/b.class...
processing com/unity3d/player/b$1.class...
processing com/unity3d/player/b$2.class...
processing com/unity3d/player/c.class...
processing com/unity3d/player/d.class...
processing com/unity3d/player/d$1.class...
processing com/unity3d/player/d$2.class...
processing com/unity3d/player/e.class...
processing com/unity3d/player/f.class...
processing com/unity3d/player/g.class...
processing com/unity3d/player/h.class...
processing com/unity3d/player/i.class...
processing com/unity3d/player/j.class...
processing com/unity3d/player/k.class...
processing com/unity3d/player/k$1.class...
processing com/unity3d/player/k$2.class...
processing com/unity3d/player/k$2$1.class...
processing com/unity3d/player/k$2$1$1.class...
processing com/unity3d/player/l.class...
processing com/unity3d/player/l$1.class...
processing com/unity3d/player/m.class...
processing com/unity3d/player/n.class...
processing com/unity3d/player/n$1.class...
processing com/unity3d/player/o.class...
processing com/unity3d/player/p.class...
processing com/unity3d/player/p$1.class...
processing com/unity3d/player/q.class...
processing com/unity3d/player/r.class...
processing com/unity3d/player/s.class...
processing com/unity3d/player/s$1.class...
processing com/unity3d/player/s$2.class...
processing com/unity3d/player/s$3.class...
processing com/unity3d/player/t.class...
processing com/unity3d/player/u.class...
processing com/unity3d/player/v.class...
processing com/unity3d/player/w.class...
processing com/unity3d/player/w$1.class...
processing org/fmod/FMODAudioDevice.class...
processing org/fmod/a.class...
]

What I can understand is that I’m adding classes that are already added. The problems is with the file classes.jar. If I take it off, the plugin works fine but without it, I can’t use the unityPlayer in my library and it’s something that I really need.

The other way is do a .jar library but in this case, I have to put the res folder inside the android plugin folder from Unity but this is something that is deprecated.

Is there another way to work with android library for Unity 5.x?

Thank you so much
If you need more information, please let me know it.

How to&Answers:

Unity adds its own classes automatically to the project when building the application. If you bundle them with your library, they will get added twice.

Android’s Gradle plugin defines a configuration called provided in addition to the compile configuration that you’re currently using. It’s different in the sense that it assumes the dependency will be present on the application’s classpath at runtime, and doesn’t include the classes in the built library.

To use the provided configuration with that particular JAR, replace these two lines

compile fileTree(include: ['*.jar'], dir: 'libs') compile files('libs/classes.jar') 

with these

compile fileTree(dir: 'libs', include: ['*.jar'], exclude: ['classes.jar']) provided files('libs/classes.jar') 

This will make Gradle treat every JAR file placed in libs/ as a dependency that is bundled with your library, except for classes.jar, which is used only to compile your library.

Note that if you’re using the Android Gradle plugin 3.0.0 or newer, you should use implementation instead of compile and compileOnly instead of provided. These configurations were renamed: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#new_configurations.