androidsqlciphercommonsware-cwac

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


I need help implementing android's R8 code shrinker with CWAC Saferoom.

Both are implemented well and are tested in debug mode, but when I generate a release APK this stack trace appears and the app crashes:

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)

I believe I am missing something, maybe excluding the saferoom's package in a proguard file (if I use proguard), or do I have to add the SQLCipher library in my dependencies when shrinking my code, if not, I am all out of ideas.

NOTE:

App's build 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
}

Solution

  • Based on this issue, add this to your ProGuard keep rules:

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