Coder Social home page Coder Social logo

room-runtime's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.