如果启用 proguard,应用程序崩溃

App crash if enabled proguard

如果使用此代码在我的应用程序中启用混淆器:

release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

仅当尝试从 Android Studio 应用程序在调试或正常模式下 运行 时在 Play 商店中发布时应用程序才会崩溃。

崩溃时出错:

Error inflating class android.support.v7.widget.Toolbar

Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.widget.Toolbar

 Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.v7.widget.Toolbar

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.aj.a()' on a null object reference

我正在使用最新版本的 support.v7:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:27.0.2'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'      
}

今天之前一切正常=(

德克斯特说:

-dontwarn android.support.v7.**
-keep class android.support.v7.widget.** { *; }

并且有效,但为什么在更新 android studio 后我总是遇到问题??为什么 Android Studio 在最新版本的 today proguard 之后取消小部件?

也许 proguard 删除了这个 class,你可以禁用 proguard 来避免这个错误,如果你想保留它你将不得不更改 proguard 设置

-dontwarn android.support.v7.**
-keep class android.support.v7.widget.** { *; }

尝试添加 Gradle:

android {  
    defaultConfig {  
        vectorDrawables.useSupportLibrary = true  
    }
}

并对所有项目执行完全清理和重建。