使用Proguard的工具栏getTitle nullpointerexception

时间:2015-11-09 09:17:18

标签: android proguard appcompatactivity

在没有ProGuard的情况下构建应用程序时,一切正常,但在我的项目中启用ProGuard时,我在启动应用程序时会收到nullpointerexception ToolBar.getTitle()。 我已经阅读了ProGuard的手册和关于SO的多个主题,但尚未找到我的解决方案。我现在可能有点狂热了,我在ProGuard文件中添加了东西。但因为我一直在接受ToolBar.getTitle()的NPE,所以我一直在尝试各种各样的事情。

感谢您抽出宝贵时间阅读我的问题和所有代码。

这些是我的代码:

的build.gradle

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

android {
   compileSdkVersion 22
   buildToolsVersion "22.0.0"

   packagingOptions {
       exclude 'META-INF/LICENSE'
       exclude 'META-INF/LICENSE-FIREBASE.txt'
       exclude 'META-INF/NOTICE'
   }

   defaultConfig {
       applicationId "com.koeck.verdienapp"
       minSdkVersion 14
       targetSdkVersion 22
       versionCode 23
       versionName "1.4.5"
       multiDexEnabled true
   }

   dexOptions {
       preDexLibraries = false
   }

   buildTypes {
       release {
           minifyEnabled true
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
   }
}
repositories {
   mavenCentral()
   maven {
       url 'http://cdn.adnxs.com/anx-sdk/maven'
   }
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:appcompat-v7:22.2.0'
   compile 'com.android.support:support-v13:22.2.0'
   compile 'com.jakewharton:butterknife:5.1.1'
   compile 'com.squareup:otto:1.3.5'
   compile 'com.squareup.picasso:picasso:2.3.2'
   compile 'com.google.android.gms:play-services:8.1.0'
   compile 'com.firebase:firebase-client-android:2.2.4+'
   compile 'com.android.support:design:22.2.0'
   compile 'com.android.support:cardview-v7:22.+'
   compile 'com.google.android.gms:play-services-analytics:8.1.0'
   compile 'com.android.support:multidex:1.0.1'
   compile 'com.squareup.picasso:picasso:2.5.2'
   compile 'com.firebase:geofire:1.1.1'
   compile 'me.relex:circleindicator:1.1.5@aar'
   compile 'joda-time:joda-time:2.8.2'
   compile project(':libadapterinmobi')
   compile project(':InMobi-5.0.1')
   compile('com.mopub:mopub-sdk:4.0.0@aar') {
       transitive = true
   }
   compile project(':libAdapterSDKMoPub')
   compile project(':AdSdk_7.0.10')
   compile project(':mobfoxmediationadapter2')
   compile project(':simple-xml-2.7.12')
}

ProGuard的:

#General

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  public *;
}

-keepclassmembers class com.example.verdienapp.** {
  *;
 }

-keep class com.google.analytics.** { *; }

-keep public class * extends android.support.v7.app.AppCompatActivity
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MultiDexApplication
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v7.app.Fragment
-keep public class * extends android.app.Fragment
-keep public class com.android.vending.licensing.ILicensingService
-keep class * extends android.support.v7.app.ActionBarDrawerToggle
-keep class * extends android.support.v7.app.ActionBar
-keep class * extends android.support.v7.widget.Toolbar

-keepclasseswithmembernames class * {
   native <methods>;
}

-keepclasseswithmembers class * {
   public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
   public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers,allowshrinking,allowobfuscation class  android.support.** {
   !static final <fields>;
}

-keepclassmembers class * extends android.app.Activity {
  public void *(android.view.View);
}

-keepclassmembers class * extends android.support.v7.app.AppCompatActivity {
  public void *(android.view.View);
}

-keepclassmembers enum * {
   public static **[] values();
   public static ** valueOf(java.lang.String);
}

-keep public class * extends android.support.design.widget.CoordinatorLayout$Behavior {
   public <init>(android.content.Context, android.util.AttributeSet);
}

# Keep the BuildConfig

-keep class com.example.BuildConfig { *; }

# Hide warnings about references to newer platforms in the library

-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keep class android.support.v7.internal.** { *; }
-keep class android.support.v7.internal.view.menu.** {*;}
-keep interface android.support.v7.internal.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
   public <init>(android.content.Context);
}

-keep public class * extends android.support.v7.view.ActionProvider {
   public <init>(android.content.Context);
}

-dontwarn android.support.**

-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
-keep class android.support.v13.** { *; }
-keep interface android.support.v13.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

-keepattributes *Annotation*

-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

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

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

# For mediation
-keepattributes *Annotation*

-keep public class com.google.ads.mediation.** {*;}

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

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

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

# For circle indicator

-keep class me.relex.** { *; }
-dontwarn me.relex.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

# For Flurry Ads

-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keep class com.google.** { *; }
-dontwarn com.google.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

# For InMobi Ads

-keepattributes SourceFile,LineNumberTable
-keep class com.inmobi.** { *; }
-dontwarn com.inmobi.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

# For MoPub Ads

-keep class com.mopub.** { *; }
-dontwarn com.mopub.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class com.mopub.** { public *; }
-keep public class com.mopub.**
-keep public class android.webkit.JavascriptInterface {}

-keep class * extends com.mopub.mobileads.CustomEventBanner {}
-keep class * extends com.mopub.mobileads.CustomEventInterstitial {}
-keep class * extends com.mopub.nativeads.CustomEventNative {}

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



# For MobFox Ads

-keep class com.adsdk.** { *; }
-dontwarn com.adsdk.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keep class com.mobfox.** { *; }
-dontwarn com.mobfox.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keep class org.simpleframework.** { *; }
-dontwarn org.simpleframework.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

# For AppNexus Ads

-keep public class com.appnexus.opensdk.** {public *;}
-keep public interface com.appnexus.opensdk.* {*;}

# For Picasso Photoloader

-keep class com.squareup.** { *; }
-dontwarn com.squareup.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
  public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

-keepclassmembers class ** {
   @com.squareup.otto.Subscribe public *;
   @com.squareup.otto.Produce public *;
}

# For Joda Time

-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
-dontwarn org.joda.convert.FromString
-dontwarn org.joda.convert.ToString

# For Butterknife

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-dontwarn butterknife.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
   @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
   @butterknife.* <methods>;
}

# For Firebase

-keepattributes *Annotation*,EnclosingMethod,Signature
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**
-dontwarn com.fasterxml.jackson.**
-keep class org.codehaus.** { *; }
-keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {
public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; }
-keep public class your.class.** {
 public void set*(***);
 public *** get*();
}

-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**
-dontwarn com.firebase.**


# Don’t note stuff

-dontnote android.**
-dontnote java.**
-dontnote javax.**
-dontnote junit.**
-dontnote org.**
-dontnote dalvik.**
-dontnote com.android.internal.**
-dontnote com.google.android.**

activity_main.xml中:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.verdienapp.ui.main.MainActivity">

   <android.support.design.widget.CoordinatorLayout
       android:id="@+id/rootLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.AppBarLayout
           android:layout_width="match_parent"
           android:layout_height="260dp"
           android:id="@+id/appBarLayout" >

           <android.support.design.widget.CollapsingToolbarLayout
               android:id="@+id/collapsingToolbarLayout"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               app:contentScrim="?attr/colorPrimary"
               app:layout_scrollFlags="scroll|exitUntilCollapsed">

               <ImageView
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:id="@+id/headerimage"
                   android:scaleType="centerCrop"
                   android:src="@drawable/mvarken1klein"
                   app:layout_collapseMode="parallax"
                   app:layout_collapseParallaxMultiplier="0.7" />

              <android.support.v7.widget.Toolbar
                   android:id="@+id/toolbar"
                   android:layout_width="match_parent"
                   android:layout_height="?attr/actionBarSize"
                   android:minHeight="?attr/actionBarSize"
                   app:layout_collapseMode="pin"
                   android:textColor="@android:color/white"
                   />

           </android.support.design.widget.CollapsingToolbarLayout>

       </android.support.design.widget.AppBarLayout>

       <android.support.v4.widget.NestedScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:fillViewport="true"
           app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <RelativeLayout
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:layout_below="@id/appBarLayout">

           <include layout="@layout/view_status_container"/>

           <RelativeLayout
               android:layout_height="match_parent"
               android:layout_width="match_parent"
               android:id="@+id/fragment_container"/>

       </RelativeLayout>

       </android.support.v4.widget.NestedScrollView>

   </android.support.design.widget.CoordinatorLayout>

   <RelativeLayout
       android:layout_height="match_parent"
       android:layout_width="@dimen/navigation_drawer_width"
       android:id="@+id/drawer_container"
       android:background="#ffffff"
       android:layout_gravity="left">

   <ListView
       android:layout_height="match_parent"
       android:layout_width="match_parent"
       android:id="@+id/drawer_listview"
       android:dividerHeight="1dp"
       android:choiceMode="singleChoice"/>

   </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           Firebase.setAndroidContext(getApplicationContext());

           startService(new Intent(this, OverlayService.class));

           ButterKnife.inject(this);
           setSupportActionBar(this.mToolbar);
           assert getSupportActionBar() != null;
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);
           getSupportActionBar().setHomeButtonEnabled(true);
    }
}

logcat错误:

961  1094 I am_crash: [6986,0,app,8961604,java.lang.NullPointerException,Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference,ToolbarWidgetWrapper.java,94]
    11-08 01:50:29.491   961  1094 I am_finish_activity: [0,629484266,19250,app/com.example.ui.main.MainActivity,crashed]
    11-08 01:50:29.491   961  1094 I wm_task_moved: [19249,1,0]
    11-08 01:50:29.801   961  1094 I am_pause_activity: [0,629484266,app/com.example.ui.main.MainActivity]

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。使用butterknife git repo中的最新proguard文件解决了这个问题:

Butterknife proguard file:

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}