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

时间:2016-01-20 06:04:35

标签: java android admob proguard

我很确定这与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无法保护源代码,并希望我永远不会使用proguard。

错误堆栈

  

FATAL EXCEPTION:main java.lang.RuntimeException:无法启动   活动   ComponentInfo {com.company.project / com.company.project.MainActivity}:   java.lang.IllegalStateException:您需要使用Theme.AppCompat   主题(或后代)与此活动。       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)       在android.app.ActivityThread.access $ 600(ActivityThread.java:162)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366)       在android.os.Handler.dispatchMessage(Handler.java:99)       在android.os.Looper.loop(Looper.java:158)       在android.app.ActivityThread.main(ActivityThread.java:5751)       at java.lang.reflect.Method.invokeNative(Native Method)       在java.lang.reflect.Method.invoke(Method.java:511)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1083)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)       at dalvik.system.NativeStart.main(Native Method)引起:java.lang.IllegalStateException:你需要使用Theme.AppCompat   主题(或后代)与此活动。       在android.support.v7.a.ae.q(未知来源)       在android.support.v7.a.ae.p(未知来源)       在android.support.v7.a.ae.a(未知来源)       在android.support.v7.a.u.setContentView(未知来源)       在com.company.project.MainActivity.onCreate(未知来源)       在android.app.Activity.performCreate(Activity.java:5165)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)       在android.app.ActivityThread.access $ 600(ActivityThread.java:162)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1366)       在android.os.Handler.dispatchMessage(Handler.java:99)       在android.os.Looper.loop(Looper.java:158)       在android.app.ActivityThread.main(ActivityThread.java:5751)       at java.lang.reflect.Method.invokeNative(Native Method)       在java.lang.reflect.Method.invoke(Method.java:511)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1083)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)       在dalvik.system.NativeStart.main(本地方法)

摇篮:

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

1 个答案:

答案 0 :(得分:2)

以下是您在admob的proguard规则中所需要的内容:

-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.** {*;}

检查是否有任何缺失或只是添加所有(没有差异),然后检查您的应用程序是否正常工作。

此外,哪个类是您的活动java文件扩展,如果它正在扩展ActionBarActivity,请将其更改为Activity