greenDAO ORM for Android

repository·master·Indexed 11 days ago

https://github.com/greenrobot/greendao

A lightweight and fast Object-Relational Mapping (ORM) framework for Android that maps Java objects to SQLite databases. It uses intelligent code generation to optimize performance and minimize memory consumption, providing tools like DaoMaster, DaoSession, and QueryBuilder for database management.

Tokens
1.2K
Snippets
4
Records
5
Agent score
93%

What's inside greenDAO

  1. Add greenDAO to your Android project

    master

    To use greenDAO, you must add both the Gradle plugin to your root build file and the library dependency to your app module. The plugin is responsible for generating essential classes like DaoMaster, DaoSession, and individual DAOs during the build process.

    1. Root build.gradle: Add mavenCentral() to repositories and include the greendao-gradle-plugin in the dependencies block.
    2. App build.gradle: Apply the org.greenrobot.greendao plugin and add the greendao implementation dependency.
    // In your root build.gradle file:
    buildscript {
        repositories {
            jcenter()
            mavenCentral() // add repository
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:<agp-version>'
            classpath 'org.greenrobot:greendao-gradle-plugin:3.3.1' // add plugin
        }
    }
    
    // In your app modules app/build.gradle file:
    apply plugin: 'com.android.application'
    apply plugin: 'org.greenrobot.greendao' // apply plugin
     
    dependencies {
        implementation 'org.greenrobot:greendao:3.3.0' // add library
    }
  2. Create query conditions using Property objects

    master

    In greenDAO, you do not typically instantiate WhereCondition objects directly. Instead, you use the Property objects generated within your DAO classes to create conditions for queries. These conditions are used to filter results in a QueryBuilder (e.g., using operators like =, !=, >, etc.).

    When creating conditions, greenDAO handles type conversion for common types:

    • Dates: Accepts java.util.Date or Long (timestamp).
    • Booleans: Accepts Boolean, Number (0 or 1), or case-insensitive String ("TRUE" or "FALSE").
    • Arrays: Passing an array where a simple object is expected will throw a DaoException.
    // Conceptual usage pattern
    // Assuming UserDao.Properties.NAME is a Property object
    QueryBuilder<User> qb = userDao.queryBuilder()
        .where(UserDao.Properties.NAME.eq("John"))
        .build();
  3. Configure R8 or ProGuard rules for greenDAO

    master

    If your project uses R8 or ProGuard, you must add specific keep rules to prevent the obfuscation of classes and members required by the greenDAO code generation and runtime.

    Required rules for all users:

    • Keep members of classes extending org.greenrobot.greendao.AbstractDao that are public static java.lang.String TABLENAME.
    • Keep all members of classes ending in $Properties.

    Conditional rules:

    • If using SQLCipher: Keep org.greenrobot.greendao.database.SqlCipherEncryptedHelper.
    • If NOT using SQLCipher: Add -dontwarn net.sqlcipher.database.**.
    • If NOT using RxJava: Add -dontwarn rx.**.
    -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
    public static java.lang.String TABLENAME;
    }
    -keep class **$Properties { *; }
    
    # If you DO use SQLCipher:
    -keep class org.greenrobot.greendao.database.SqlCipherEncryptedHelper { *; }
    
    # If you do NOT use SQLCipher:
    -dontwarn net.sqlcipher.database.**
    
    # If you do NOT use RxJava:
    -dontwarn rx.**
  4. Define multi-column indexes with @Index

    master

    To create a multi-column (composite) index, you must use the @Index annotation within the indexes() method of the @Entity annotation, rather than applying it directly to a field.

    When used inside @Entity(indexes = ...), the value() parameter of @Index accepts a comma-separated list of property names. You can also specify the sort order by appending ASC or DESC to the property name.

    Example syntax for value():

    • `