3

I want to export an android library to Unity, but that Android gives the following error:

05-14 12:58:10.601 18408-18408/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.joostverbraeken.weargame, PID: 18408
                                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joostverbraeken.weargame/com.joostverbraeken.weargame.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.joostverbraeken.weargame.MainActivity" on path: DexPathList[[zip file "/data/app/com.joostverbraeken.weargame-1/base.apk"],nativeLibraryDirectories=[/data/app/com.joostverbraeken.weargame-1/lib/arm, /data/app/com.joostverbraeken.weargame-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5436)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
                                                    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.joostverbraeken.weargame.MainActivity" on path: DexPathList[[zip file "/data/app/com.joostverbraeken.weargame-1/base.apk"],nativeLibraryDirectories=[/data/app/com.joostverbraeken.weargame-1/lib/arm, /data/app/com.joostverbraeken.weargame-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5436) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
                                                    Suppressed: java.lang.NoClassDefFoundError: com.joostverbraeken.weargame.MainActivity
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:338)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
        ... 13 more
                                                    Suppressed: java.lang.ClassNotFoundException: com.joostverbraeken.weargame.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 12 more
                                                    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

My AndroidManifest (I copied the one from my library into Unity) is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.joostverbraeken.weargame">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application android:icon="@mipmap/ic_launcher" android:label="@string/app_name">
        <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The dependencies are fairly straightforward and are declared in Gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'], exclude: ['classes.jar'])
    provided files('libs/classes.jar')
    wearApp project(':wear')
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:support-v4:23.4.0'
}

On a forum I read Unity needed to have the dependencies copied into Assets/Plugins/Android, although I'm using an aar instead of a jar. So I copied support-v4-23.3.0-sources.jar into Assets/Plugins/Android as well, but play-services-8.4.0.jar not because it's not included in the Android system files. play-services-8.4.0.aar is included, but if I copy that to Unity I get the following error:

Moving
Temp/StagingArea\android-libraries\play-services-8.4.0\classes.jar to
Temp/StagingArea\android-libraries\play-services-8.4.0\bin\classes.jar:
The system cannot find the file specified.

Does anybody have an idea how to solve the ClassNotFoundException?

1
  • Even I am facing the same problem. Do you find any solution for that?? Commented Jun 16, 2016 at 10:58

3 Answers 3

1

I believe this is caused by AndroidManifest.xml which has something conflict with Unity generated AndroidManiefst.xml. It's not friendly that unity does not give us any error messages in this case and it really sucks that only export eclipse project.

I came across the same exception today, so I did some further test and found the ClassNotFoundException can only be produced in Activity classes. So I exported the project and import the project into AndriodStudio and recompiled it. I got some error in AndroidManifest.xml like the incorrect theme or incorrect icon ,etc.

After update all incorrect configurations in AndriodManifest.xml and rebuild the AAR, everything worked fine.

Hope this can help you.

Sign up to request clarification or add additional context in comments.

3 Comments

You never got it to work without exporting the unity project into AndroidStudio project? We exceeded reference count and we thought about using the individual APIs of google play services instead of copying the full raw google_play_services_lib folder to our Plugins/Android folder. But the latest google play services are in *.aar now. And like Defi said, if we include the *.aar in the Plugins/Android folder, we get the moving Temp/StagingArea... error.
Unfortunately, our builds are automated and exporting Unity project to AndroidStudio won't be an option for us. I wonder how we could reduce the google play services library without breaking our app. :( We also get the classNotFoundException on our mainActivity.
@Psylocke Did you automated integrating aar in unity? I exported some android library as aar in unity but everytime i make some changes to my library i have to manually copy new aar file to unity and rebuild the project. Can you help me with automating this process?
1

If still someone with the problem or related... Finally I got the solution, it´s so so simple. As simple as copy manually "support-v4-24.1.1.aar" into the plugin unity project folder. Or other support aar android file. It comes with the SDK. In my case the file was in "C:\AndroidSDK\extras\android\m2repository\com\android\support\support-v4\24.1.1"

No matter the version but you should be sure that "FileProvider" class is inside the file. You can unzip the aar file simplily with winrar and be sure that the class is in there.

Comments

0

Long-time after this question was posted but I found this while trying to figure out how to solve a similar problem.

A better solution is using the following:

https://github.com/googlesamples/unity-jar-resolver

It's a much better way of managing dependencies. If you add the dependencies directly to the exported jar, then you run the risk of collisions with other plugins.

2 Comments

What advantage is there by adding it straight to the jar?
If you have two plugins in a project, both including the same dependencies in the binary, that will read to an error, with either or both plugins not loading. The unity jar resolver makes sure that only one copy of a library will be included in the unity project, regardless of how many plugins it's used by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.