Android Unity插件 - 未找到类异常

时间:2016-05-14 11:12:51

标签: android unity3d plugins classnotfoundexception aar

我想将一个安卓库导出到Unity,但是Android会出现以下错误:

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

我的AndroidManifest(我将我的库中的那个复制到Unity中)如下:

<?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>

依赖关系非常简单,并在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'
}

在论坛上我读到Unity需要将依赖项复制到Assets / Plugins / Android中,尽管我使用的是aar而不是jar。所以我也将support-v4-23.3.0-sources.jar复制到Assets / Plugins / Android中,但play-services-8.4.0.jar并不是因为它没有包含在Android系统文件中。包含play-services-8.4.0.aar,但如果我将其复制到Unity,我会收到以下错误:

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.

有没有人知道如何解决ClassNotFoundException?

3 个答案:

答案 0 :(得分:1)

我认为这是由AndroidManifest.xml引起的,它与Unity生成的AndroidManiefst.xml存在冲突。在这种情况下,团结并没有给我们任何错误信息,并且只是导出eclipse项目真的很糟糕。

我今天遇到了同样的例外,所以我做了一些进一步的测试,发现ClassNotFoundException只能在Activity类中生成。所以我导出了项目并将项目导入AndriodStudio并重新编译。我在AndroidManifest.xml中遇到了一些错误,例如错误的主题或不正确的图标等。

更新AndriodManifest.xml中的所有错误配置并重建AAR后,一切正常。

希望这可以帮到你。

答案 1 :(得分:1)

如果还有问题或相关问题的人... 终于我得到了解决方案,它是如此的简单。只需将“ support-v4-24.1.1.aar”手动复制到插件unity项目文件夹中即可。或其他支持Aar的android文件。它随SDK一起提供。就我而言,该文件位于“ C:\ AndroidSDK \ extras \ android \ m2repository \ com \ android \ support \ support-v4 \ 24.1.1”

无论版本如何,但您都应确保“ FileProvider”类位于文件内部。您可以使用winrar简单地解压缩aar文件,并确保该类在其中。

答案 2 :(得分:0)

发布此问题后很长时间,但是我在试图弄清楚如何解决类似问题时发现了这一点。

更好的解决方案是使用以下内容:

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

这是管理依赖项的一种更好的方法。如果直接将依赖项添加到导出的jar中,则存在与其他插件发生冲突的风险。