FATAL EXCEPTION: main java.lang.NoClassDefFoundError: org.andengine.engine.options.SoundOptions

FATAL EXCEPTION: main java.lang.NoClassDefFoundError: org.andengine.engine.options.SoundOptions

我在 eclipse 中有一个现有的 andengine 项目,我已将其迁移到 Android Studio。 gradle 显示没有错误,我能够成功构建 apk。但是每当我 open/run 我的 phone 上的 apk 时,它都会在开始时崩溃并出现上述错误。我在这里和那里阅读了很多随机的东西,但一直无法理解这个问题。

下面是我从 logcat 得到的详细错误。我注意到有一行是致命异常:main java.lang.NoClassDefFoundError: org.andengine.engine.options.SoundOptions

所以不知何故,andengine 在运行时找不到 SoundOptions 和其他一些 类。

这是我的 onCreateEngineOptions 代码,它显示了崩溃。如果您需要我这边的任何其他信息,请告诉我。

@Override
    public EngineOptions onCreateEngineOptions() {
        abc();
        prefs = this.getSharedPreferences(
                "MY_PACKAGE", Context.MODE_PRIVATE);
        camera = new FollowCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        EngineOptions engineOption = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), camera);
        engineOption.getAudioOptions().setNeedsMusic(true);
        engineOption.getAudioOptions().setNeedsSound(true);
        engineOption.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
        engineOption.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        engineOption.getTouchOptions().setNeedsMultiTouch(true);
        return engineOption;
    }

这是我的 build.gradle(module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'
    defaultConfig {
        applicationId "MY_PACKAGE"
        minSdkVersion 9
        targetSdkVersion 22
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = [] //disable automatic ndk-build call
    }
    productFlavors {
    }
}
repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile project(':BaseGameUtils')
    compile project(':facebook')
    compile project(':AndEngine')
    compile project(':andEnginePhysicsBox2D')
}

日志:

06-18 16:22:03.761  14042-14042/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
06-18 16:22:03.771  14042-14042/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
06-18 16:22:05.453    4974-4974/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-18 16:22:05.623    4974-4974/? E/dalvikvm﹕ Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method b.a
06-18 16:22:06.033    4974-4974/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-18 16:22:06.133    4974-4974/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-18 16:22:06.133    4974-4974/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-18 16:22:06.143    4974-4974/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-18 16:22:06.334    4974-4974/? E/dalvikvm﹕ Could not find class 'android.app.RemoteInput[]', referenced from method b.a
06-18 16:22:06.394    4974-4974/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.b
06-18 16:22:14.932    5041-5148/MY_PACKAGE E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-18 16:22:14.962    5041-5148/MY_PACKAGE E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
06-18 16:22:15.022    5041-5148/MY_PACKAGE E/dalvikvm﹕ Could not find class 'com.google.android.gms.ads.identifier.AdvertisingIdClient$zza', referenced from method com.google.android.gms.ads.identifier.AdvertisingIdClient.zzaJ
06-18 16:22:15.052    5041-5148/MY_PACKAGE E/GooglePlayServicesUtil﹕ The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
06-18 16:22:15.282    5041-5041/MY_PACKAGE E/dalvikvm﹕ Could not find class 'org.andengine.engine.handler.UpdateHandlerList', referenced from method org.andengine.engine.camera.Camera.allocateUpdateHandlers
06-18 16:22:15.613    5041-5041/MY_PACKAGE E/dalvikvm﹕ Could not find class 'org.andengine.engine.options.SoundOptions', referenced from method org.andengine.engine.options.AudioOptions.<init>
06-18 16:22:15.793    5041-5041/MY_PACKAGE E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: org.andengine.engine.options.SoundOptions
            at org.andengine.engine.options.AudioOptions.<init>(AudioOptions.java:18)
            at org.andengine.engine.options.EngineOptions.<init>(EngineOptions.java:32)
            at MY_PACKAGE.GameActivity.onCreateEngineOptions(GameActivity.java:209)
            at org.andengine.ui.activity.BaseGameActivity.onCreate(BaseGameActivity.java:82)
            at MY_PACKAGE.GameActivity.onCreate(GameActivity.java:289)
            at android.app.Activity.performCreate(Activity.java:4538)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
            at android.app.ActivityThread.access0(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:156)
            at android.app.ActivityThread.main(ActivityThread.java:4987)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
06-18 16:22:15.843    1601-5647/? E/EmbeddedLogger﹕ App crashed! Process: MY_PACKAGE
06-18 16:22:15.843    1601-5647/? E/EmbeddedLogger﹕ App crashed! Package: MY_PACKAGE v1 (1.0)
06-18 16:22:15.843    1601-5647/? E/EmbeddedLogger﹕ Application Label: MY_APP_LABEL

您的项目是否达到了 65k+ 方法(dex 限制)?您似乎在 build.gradle 中启用了 multidex。但是这样做还不足以支持 multidex,你需要在你的 Application class.

中扩展 MultidexApplication
public class YourApplication extends MultiDexApplication {

.

.

您还需要在 build.gradle

中指定对 multidex 库的依赖
compile 'com.android.support:multidex:1.0.0'

你做到了吗?

更多信息你可以参考这个文档 https://developer.android.com/tools/building/multidex.html

问题可能出在 multiDex 函数上。禁用它,并尝试减少项目中的方法数量。 Google Play Services 有很多,你可以尝试使用更细粒度的库(例如,拆分成更小的模块,只使用你真正需要的模块)。在 this guide

之后,我将花费您大约 30 秒的时间