Alston
Alston

Reputation: 1226

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

I'm pretty sure this is nothing about the Theme.AppCompat, because the proguard-enabled app was originally works well without com.google.android.gms:play-services-ads:7.5.0, things went wrong after added com.google.android.gms:play-services-ads:7.5.0.

With com.google.android.gms:play-services-ads included, it only works when proguard disabled.

Can someone please help? I really hate java's inability in protecting source code and wish I could never use proguard.

Error Stack

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.access$600(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.access$600(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>

Proguard for admob (copied from admob's example on github):

# 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;
}

Upvotes: 0

Views: 2814

Answers (1)

Viral Patel
Viral Patel

Reputation: 33438

The following is what you need in your proguard rules for 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.** {*;}

Check if any is missing or just add all (no difference) and then check if your app works fine.

Also, which class is your Activity java file extending, if it is extending ActionBarActivity, change it to just Activity.

Upvotes: 3

Related Questions