Modified Room runtime library for Proguard/R8 obfuscation
Go to file
2019-10-26 17:30:32 -04:00
gradle/wrapper Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00
runtime Update to 2.2.1 2019-10-26 17:30:32 -04:00
.gitignore Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00
build.gradle Update to 2.2.0 2019-10-11 02:25:21 -04:00
gradle.properties Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00
gradlew Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00
gradlew.bat Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00
README.md Update README 2019-06-06 21:55:38 -07:00
settings.gradle Add room-runtime 2.1.0-rc01 source code 2019-06-06 02:47:37 -07:00

Room-Runtime

This is a modified version of the AndroidX Room Persistent library, specifically the room-runtime component.

Why

The goal is to allow full Proguard/R8 class obfuscation. In official room runtime, this proguard rule -keep class * extends androidx.room.RoomDatabase prevents any possible obfuscation for RoomDatabase classes. The reason why this rule is needed is because the generated RoomDatabase implementation will be found and created via classname reflection at runtime.

Download

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    // Remove androidx.room:room-runtime and replace it with the following
    modules {
        module('androidx.room:room-runtime') {
            replacedBy('com.github.topjohnwu:room-runtime')
        }
    }
    implementation "com.github.topjohnwu:room-runtime:${vRoom}"
}

Usage

In order to remove the usage of reflection, you will have to create a RoomDatabase.Factory to create database instances. Implement the factory and set it as shown below in static blocks of your Application or main Activity:

Room.setFactory(clazz -> {
    switch (clazz) {
        case MyRoomDB1.class:
            return new MyRoomDB1_Impl();
        case MyRoomDB2.class:
            return new MyRoomDB2_Impl();
        default:
            return null;
    }
});

Or in Kotlin:

Room.setFactory {
    when(it) {
        MyRoomDB1::class.java -> MyRoomDB1_Impl()
        MyRoomDB2::class.java -> MyRoomDB2_Impl()
        else -> null
    }
}

Note that your factory class has to handle ALL RoomDatabase throughout your app. There might be additional RoomDatabase used in your dependencies (for example, WorkManager). To find out all possible RoomDatabase used in your application, the easiest way is to add this to your proguard rules: -whyareyoukeeping class * extends androidx.room.RoomDatabase, and build your project before switching to this implementation. It will print out all RoomDatabase classes in your project, and you can implement your RoomDatabase.Factory accordingly.