2015-11-09 25 views
-1

在沒有ProGuard的情況下構建應用程序時,一切正常,但在我的項目中啓用ProGuard時,我會在啓動我的應用程序時收到nullpointerexceptionToolBar.getTitle()。 我已經閱讀了ProGuard的手冊和關於SO的多個主題,但還沒有找到我的解決方案。現在我可能已經有點瘋狂了,我的ProGuard文件中添加了一些東西。但因爲我不斷收到ToolBar.getTitle()的NPE,我一直在嘗試各種各樣的事情。工具欄getTitle nullpointerexception與Proguard

感謝您花時間閱讀我的問題和所有的代碼。

這是我的代碼:

的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:[email protected]' 
    compile 'joda-time:joda-time:2.8.2' 
    compile project(':libadapterinmobi') 
    compile project(':InMobi-5.0.1') 
    compile('com.mopub:mopub-sdk:[email protected]') { 
     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] 
+0

@Sheraz艾哈邁德Khilji請你讓我知道你爲什麼downvoted我的問題?我在我的問題上投入了時間,精力和研究。如果有什麼我不得不改變,以使它成爲一個更好的問題,請讓我知道。 –

+0

我沒有倒下它。我只是編輯它並更改代碼格式。關於解決方案,我相信你沒有在你的JAVA代碼中引用工具欄。在調用'setSupportActionBar'之前,你應該初始化你的工具欄,例如'mToolbar =(Toolbar)findViewById(R.id.toolbar);' –

+0

在使用proguard構建發佈版本後,我遇到同樣的問題... – JanBo

回答

1

我有同樣的問題。使用來自butterknife混帳回購協議的最新proguard的文件解決了這個問題:

Butterknife proguard file:

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

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

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