CWAC Saferoom 和代码压缩,如何在拥有 Saferoom 的同时压缩我的代码? [固定的]

CWAC Saferoom and Code Shrinking, How do I shrink my code while having Saferoom? [FIXED]

我需要帮助使用 CWAC Saferoom 实施 android 的 R8 代码收缩器。

两者都实现良好并在调试模式下进行了测试,但是当我生成发布 APK 时出现此堆栈跟踪并且应用程序崩溃:

2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166] No pending exception expected: java.lang.NoSuchFieldError: no "J" field "mNativeHandle" in class "Lnet/sqlcipher/database/SQLiteDatabase;" or its superclasses
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader) (Runtime.java:-2)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void java.lang.Runtime.loadLibrary0(java.lang.ClassLoader, java.lang.String) (Runtime.java:1014)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void java.lang.System.loadLibrary(java.lang.String) (System.java:1672)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void net.sqlcipher.database.SQLiteDatabase$a.a(java.lang.String[]) (:-1)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void net.sqlcipher.database.SQLiteDatabase.a(net.sqlcipher.database.SQLiteDatabase$e) (:-1)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void net.sqlcipher.database.SQLiteDatabase.a(android.content.Context, java.io.File) (:-1)
2019-10-08 14:10:32.890 22013-22013/? A/.sample: thread.cc:2166]   at void net.sqlcipher.database.SQLiteDatabase.a(android.content.Context) (:-1)

我相信我遗漏了一些东西,可能在 proguard 文件中排除了 saferoom 的包(如果我使用 proguard),或者在压缩我的代码时我是否必须在我的依赖项中添加 SQLCipher 库,如果没有,我是完全没有想法。

注意:

应用程序的构建 gradle

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "sample.id"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "0.1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            useProguard false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    // JUnit Library
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // Room Database Library
    implementation "android.arch.persistence.room:runtime:1.1.1"
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-annotations:28.0.0'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

    // CWAC SafeRoom Library
    implementation "com.commonsware.cwac:saferoom:1.2.1"

//... some unimportant android libraries
}


解决方案:

根据@commonsware,将以下内容添加到您的 proguard 文件中(无论您使用 Android R8 还是 proguard),它应该可以工作:

-keep class net.sqlcipher.** { *; }
-keep class net.sqlcipher.database.* { *; }

基于this issue,将此添加到您的 ProGuard 保留规则中:

-keep class net.sqlcipher.** { *; }
-keep class net.sqlcipher.database.* { *; }