Can't build Android in Unity: UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.IllegalArgumentException: a

I have read this post: OBSOLETE - Providing Android resources in Assets/Plugins/Android/res is deprecated... - Unity Engine - Unity Discussions Didn’t help.

I have the latest version of Android Studio.

I have an Android Library for my Unity3D plugin.

I have to reference classes.jar in : C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer\release\bin

My plugin class extends BroadcastReceiver.

Inside a method I call: Activity currentActivity = UnityPlayer.currentActivity;

My question is: how can I NOT include the referenced classes.jar in my “aar” file?

I’m using an “aar” file because this is recommended over a “.jar” file implementation. I was trying to use a “.jar” file but was unable to get any of my images in the /res folder to be added to the “.jar”

So I’m going with the recommended library approach. But now, I’m getting the below TOP-LEVEL-EXCEPTION.

I only have a single reference in the /libs folder. I’m 100% sure I’m not referencing it twice in my library.
What can I do?

Thank you so very much!

Karl

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/mole/AppData/Local/Android/sdk\tools” -Dfile.encoding=UTF8 -jar “C:/Program Files/Unity/Editor/Data/BuildTargetTools/AndroidPlayer\sdktools.jar” -

stderr[

UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/a$1;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
at com.android.dx.command.dexer.Main.processClass(Main.java:732)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
at com.android.dx.command.dexer.Main.access$300(Main.java:83)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:229)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:158)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:632)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
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)
1 error; aborting
]
stdout[
processing archive bin\classes.jar…
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/a.class…
processing com/unity3d/player/b$1.class…
processing com/unity3d/player/b$2.class…
processing com/unity3d/player/b.class…
processing com/unity3d/player/c.class…
processing com/unity3d/player/d$1.class…
processing com/unity3d/player/d$2.class…
processing com/unity3d/player/d.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$1.class…
processing com/unity3d/player/j$2$1$1.class…
processing com/unity3d/player/j$2$1.class…
processing com/unity3d/player/j$2.class…
processing com/unity3d/player/j.class…
processing com/unity3d/player/k$1.class…
processing com/unity3d/player/k.class…
processing com/unity3d/player/l.class…
processing com/unity3d/player/m.class…
processing com/unity3d/player/n$1.class…
processing com/unity3d/player/n.class…
processing com/unity3d/player/NativeLoader.class…
processing com/unity3d/player/o.class…
processing com/unity3d/player/ReflectionHelper$1.class…
processing com/unity3d/player/ReflectionHelper$a.class…
processing com/unity3d/player/ReflectionHelper.class…
processing com/unity3d/player/p.class…
processing com/unity3d/player/q$1.class…
processing com/unity3d/player/q$2.class…
processing com/unity3d/player/q$3.class…
processing com/unity3d/player/q.class…
processing com/unity3d/player/r.class…
processing com/unity3d/player/s.class…
processing com/unity3d/player/UnityPlayer$1.class…
processing com/unity3d/player/UnityPlayer$2.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$10.class…
processing com/unity3d/player/UnityPlayer$11.class…
processing com/unity3d/player/UnityPlayer$12.class…
processing com/unity3d/player/UnityPlayer$13$1.class…
processing com/unity3d/player/UnityPlayer$13.class…
processing com/unity3d/player/UnityPlayer$3.class…
processing com/unity3d/player/UnityPlayer$14.class…
processing com/unity3d/player/UnityPlayer$15.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$a.class…
processing com/unity3d/player/UnityPlayer$b.class…
processing com/unity3d/player/UnityPlayer$c.class…
processing com/unity3d/player/UnityPlayer.class…
processing com/unity3d/player/UnityPlayerActivity.class…
processing com/unity3d/player/UnityPlayerNativeActivity.class…
processing com/unity3d/player/UnityPlayerProxyActivity.class…
processing com/unity3d/player/t.class…
processing com/unity3d/player/u$1.class…
processing com/unity3d/player/u.class…
processing com/unity3d/player/WWW.class
processing org/fmod/FMODAudioDevice$a.class…
processing org/fmod/FMODAudioDevice.class…
processing org/fmod/a.class…
processing bin\classes.\com\karlshifflett\notificationHelpMe\R$attr.class…
processing bin\classes.\com\karlshifflett\notificationHelpMe\R$drawable.class…
processing bin\classes.\com\karlshifflett\notificationHelpMe\R$mipmap.class…
processing bin\classes.\com\karlshifflett\notificationHelpMe\R$string.class…
processing bin\classes.\com\karlshifflett\notificationHelpMe\R.class…
processing bin\classes.\com\karlshifflett\notificationlibrary\R$attr.class…
processing bin\classes.\com\karlshifflett\notificationlibrary\R$drawable.class…
processing bin\classes.\com\karlshifflett\notificationlibrary\R$mipmap.class…
processing bin\classes.\com\karlshifflett\notificationlibrary\R$string.class…
processing bin\classes.\com\karlshifflett\notificationlibrary\R.class…
processing archive C:\Unity3d\Logical Advantage\NotificationHell\Temp\StagingArea\android-libraries\notificationlibrary-debug\libs.\classes.jar…
processing com/unity3d/player/a$1.class…
processing archive C:\Unity3d\Logical Advantage\NotificationHell\Temp\StagingArea\android-libraries\notificationlibrary-debug\bin\classes.jar…
ignored resource META-INF/MANIFEST.MF
processing com/karlshifflett/notificationlibrary/BuildConfig.class…
processing com/karlshifflett/notificationlibrary/UnityNotificationManager.class…
]
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandInternal (System.String javaExe, System.String sdkToolsDir, System.String[ ] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.Android.AndroidSDKTools.RunCommandSafe (System.String javaExe, System.String sdkToolsDir, System.String[ ] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
UnityEditor.HostView:OnGUI()

Are you sure you don’t have 2 copies of classes.jar in your UNITY project?

Thank you for the reply.

Yes. The real question is, how can I build my Android library and not include the classes.jar in the “aar” file. Many others have asked the same question.

I’ve tried for so many hours to figure this out, without any success.

Seems strange that Unity does not allow multiple “aar” library files to contain their dependencies. I wish there was a way to reference the Unity classes.jar without having it packaged in the “aar” library. Seems like a very standard workflow for developers.

I do have 7 .jar files in my Unity project. One of them “could” be including the “classes.jar”.

So its up to me to be a good player, and not include “classes.jar” in my library. This seems like such a simple task.

Best,

Karl

You should make sure your project doesn’t include the same JAR twice, and also not include the classes.jar file.

Regarding the .aar - how are you creating it ? Could you share the grafle build file ?

I’m using Android Studio. It’s including the classes.jar file.

How did you add classes.jar as a dependency ?
The build process uses gradle (you have a build.gradle file) that Android Studio uses to build your project.
Can you please check the contents of this file, specifically relating to classes.jar

I copied the classes.jar file into the /libs folder. Then right clicked on it and added as library. It updated the build.gradle file for me.

Is there another way to reference the classes.jar?

Set the dependency’s scope to “Provided” :
2162345--142928--upload_2015-6-16_21-58-28.png

Go to your library project, right click and select “Module settings” and from there set this under Dependencies.

Let me know if that helped.

Get back to you soon. Thank you so very much.

yes, this fixed the problem. Thank you so very much!

Just a quick update on this guy: If you’re extending the UnityPlayerActivity class, you’ll be wanting to also set the include [‘*.jar’] yadda yadda dependency to “provided” as well.

1 Like

getting the same error.
i made a new unity project exported it to android studio, where i get a unity-classes.jar in the libs folder.
I have only add one simple function to the UnityPlayerActivity class, build the project and copy the unity-classes.jar together with the Manifest.xml.
i wrote a simple testing script in unity and try to build the project to my phone. but get the exact same error as in this thread.
I’ve build android plugins before with eclipse and am getting confused with the new manner of exporting to android studio.
also tried the solution changing the dependency to provided but that didn’t fix it.

nevermind, i shouldn’t use unity-classes as jar :slight_smile:

Although following the above, you got error, please check up your android app:graddle
change below
compile filetree(include:[‘.jar’], dir:‘libs’)
to
compile filetree(dir:‘libs’, include:['
.jar’], exclude:[‘classes.jar’])

Hey, just for people having trouble working with this, here’s a bit of a step by step guide…

One more, if you try aar plugin,
you can open .aar file into a zip file and check out /libs/ folder
If you have classes.jar file in your /libs/ folder, then get rid of it.
Then build your project on Unity

Hey all, I’m having this issue as well (likely due to the setup). I’ve not used gradle before coming from an ant bg so bear w/me. Basically I’ve an existing sdk that i’m working on integrating… and am getting similar errors as noted in this thread.

CommandInvokationFailure: Unable to convert classes into dex format. See the Console for details.
C:/Program Files/Java/jdk1.8.0_65\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="C:/Program Files (x86)/Android/android-sdk\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$a;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/NativeLoader;
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/ReflectionHelper;
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;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$11;

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
]

I didn’t include the full stack above because its pretty much the same as everyone else’s. Here’s what my setup looks like in relation to the classes files…

I’ve got classes.jar added as a library as well, and have it set to Provided and not compiled. Everything builds outta Android studio just fint BUT when i build unity I get the duplicate errors. I’ve also dug into the JAR and do see the libraries from classes compiled in.

Am I missing anything? A step perhaps i didn’t take? If i don’t include the library (and remove my wrapper) the jar compiles file, unity loads it fine, and i’m able to call to the file w/o the app breaking (of course the sdk doesn’t work because its not extending unity).

Able to get it to build properly now with the exclude addition to the gradle files… but now I get a more interesting error… basically it can find my initial call but then the subsequent library calls (within the SDK) are missing. I see them in the JAR and everything IS there but for instance here’s one of the errors…

01-11 06:33:36.124: W/dalvikvm(11325): VFY: unable to resolve static method 6358: Lnet/danlew/android/joda/JodaTimeAndroid;.init (Landroid/content/Context;)V
01-11 06:33:36.124: W/dalvikvm(11325): Unable to resolve superclass of Lcom/jr/sdk/views/activities/LoginActivity; (155)
01-11 06:33:36.124: W/dalvikvm(11325): Link of class 'Lcom/jr/sdk/views/activities/LoginActivity;' failed
01-11 06:33:36.124: E/dalvikvm(11325): Could not find class 'com.jr.sdk.views.activities.LoginActivity', referenced from method com.jr.sdk.JR.showDashboard
01-11 06:33:36.124: W/dalvikvm(11325): VFY: unable to resolve const-class 642 (Lcom/jr/sdk/views/activities/LoginActivity;) in Lcom/jr/sdk/JR;

Here’s my manifest…

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jr.spacearcade"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:label="@string/app_name">
        <activity android:name="com.jr.sdk.UnityJR"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

And the call i’m using from within Unity…

        AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject> ("currentActivity");
        currentActivity.Call ("UnityInit", APIKey);

That hits the jar for sure, because its starting the INIT process from the original SDK but from there it all falls apart. Maybe i’m approaching this all wrong? Any suggestions on how to get the unity project to communicate wholly with a jar that includes a bunch of functionality?

many thanks!
Bryan