不幸的是项目在添加 admob 广告后停止了

unfortunately project has stopped after adding admob ads

这是我的应用构建 gradle 代码

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "com.example.personal.numbermania"
    minSdkVersion 10
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

dexOptions {
    incremental true
    javaMaxHeapSize "4g" //Here stablished how many cores you want to use your android studi 4g = 4 cores
}


buildTypes {
    debug
            {
                debuggable true
            }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.firebase:firebase-ads:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.android.gms:play-services:9.6.1'

}

apply plugin: 'com.google.gms.google-services'

这是我的项目构建 gradle

// 顶级构建文件,您可以在其中添加所有 sub-projects/modules.

通用的配置选项
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.1.3'
    classpath 'com.google.gms:google-services:3.0.0'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

allprojects {
repositories {
    jcenter()
}

}

task clean(type: Delete) {
delete rootProject.buildDir

}

这是我在 运行 error

时的错误

在调用 AdView 之前进行初始化

    MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
    //replace your ID
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

更多详情参考Here

编译代码后,每个 class 中都有 .class 个文件 你的程序。这些文件包括 Java 用来解释和 运行 你的程序的字节码。 JVM 试图找到一个 .class 文件,但是找不到它导致 运行 时间错误,NoClassDefError.

将以下代码添加到 build.gradle 文件可修复此问题:

dexOptions {
   incremental true
   javaMaxHeapSize "4g" 
}

为了debug/solve这个问题要尝试的其他事情

方法一:

  • Google Play 服务 更新为 修订版 30,从 Android SDK Manager > Extras
  • com.android.support.multidex:1.0.1 添加到您的依赖项或删除 multiDexEnabled true(如果不需要)。
  • 将属性 - android:name="android.support.multidex.MultiDexApplication" 添加到 AndroidManifest
  • 中的应用程序标记

注意:如果您的 AndroidManifest 已经定义了自定义应用程序 class,请扩展 MultiDexApplication 而不是 Application

方法二:

另一种更新方式Application class:

public class App extends Application {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ...
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

方法三:

如果方法 1 和方法 2 都不成功,请尝试将您的 Google Play 服务依赖项降级到 8.4.0