java.lang.RuntimeException: 使用 admob sdk 时无法启动 activity ComponentInfo

java.lang.RuntimeException: Unable to start activity ComponentInfo when using admob sdk

我很确定这与 Theme.AppCompat 无关,因为支持 proguard 的应用程序最初在没有 com.google.android.gms:play-services-ads:7.5.0 的情况下运行良好,添加 com.google.android.gms:play-services-ads:7.5.0 后出现问题。

包含 com.google.android.gms:play-services-ads 后,它仅在 proguard 禁用时有效。

有人可以帮忙吗?我真的很讨厌 java 无法保护源代码,希望我永远不会使用混淆器。

错误堆栈

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.project/com.company.project.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) at android.app.ActivityThread.access0(ActivityThread.java:162) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:5751) 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:1083) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.a.ae.q(Unknown Source) at android.support.v7.a.ae.p(Unknown Source) at android.support.v7.a.ae.a(Unknown Source) at android.support.v7.a.u.setContentView(Unknown Source) at com.company.project.MainActivity.onCreate(Unknown Source) at android.app.Activity.performCreate(Activity.java:5165) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)  at android.app.ActivityThread.access0(ActivityThread.java:162)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:5751)  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:1083)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)  at dalvik.system.NativeStart.main(Native Method)

Gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.company.project"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
        // proguard enabled for debug
            minifyEnabled true
            FileCollection proGuardFileCollection = files { file('./proguard').listFiles() }
            proguardFiles(proGuardFileCollection)
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
        release {
            minifyEnabled true
            FileCollection proGuardFileCollection = files { file('./proguard').listFiles() }
            proguardFiles(proGuardFileCollection)
            proguardFiles getDefaultProguardFile('proguard-android.txt')

        }
    }
}

dependencies {
    compile project(':vitamio')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.facebook.fresco:fresco:0.8.1'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'in.srain.cube:ultra-ptr:1.0.11'
    compile 'in.srain.cube:grid-view-with-header-footer:1.0.12'

    compile 'com.google.android.gms:play-services-ads:7.5.0'

    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }

}

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

AndroidManifes.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

<!-- note: @style/AppTheme is defined as
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style> 
-->

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".VideoPlayerActivity" android:theme="@style/AppTheme.NoActionBar"/>
    <activity android:name=".VideoActivity" android:theme="@style/AppTheme.NoActionBar"/>

    <!--AdMob-->
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />


</application>

admob 的 Proguard(从 github 上的 admob 示例复制):

# For Google Play Services
-keep public class com.google.android.gms.ads.**{
   public *;
}

# For old ads classes
-keep public class com.google.ads.**{
   public *;
}

# For mediation
#-keepattributes *Annotation*

# Other required classes for Google Play Services
# Read more at http://developer.android.com/google/play-services/setup.html
-keep class * extends java.util.ListResourceBundle {
   protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
   public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
   @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
   public static final ** CREATOR;
}

以下是您在 admob 混淆规则中需要的内容:

-keep class com.google.android.gms.common.GooglePlayServicesUtil {*;}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {*;}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {*;}

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**
-keep class com.google.ads.** {*;}

检查是否有遗漏或全部添加(没有区别),然后检查您的应用是否正常工作。

此外,class 是您的 Activity java 文件扩展,如果扩展 ActionBarActivity,请将其更改为仅 Activity.