Skip to content

Android

Nikhil Purushe edited this page Jul 17, 2017 · 31 revisions

Android support is at the core of this project. requery is the most feature complete ORM available for Android that is also performant. The requery-android project provides classes specific to using Android's SQLite database within the requery core library as well as useful UI adapters/classes. requery works on Android API level 15 and later.

Quickstart

Requirements:

  • Android Studio 1.5 or later
  • Java JDK 8
  1. Add the apt gradle plug-in to your build as described here
  2. Add the requery dependencies to your app build.gradle file:
dependencies {
    compile 'io.requery:requery:<latestVersion>'
    compile 'io.requery:requery-android:<latestVersion>' // for android
    apt 'io.requery:requery-processor:<latestVersion>'   // use an APT plugin
}

Replace <latestVersion> with latest released version.

If you use lint you may need to disable the InvalidPackage error. The core library contains references to classes not available on android however these would never be executed on android so it is safe.

android {
    lintOptions {
        disable 'InvalidPackage'
    }
}

DatabaseSource

DatabaseSource is the Android specific connection source for use with EntityDataStore which is the core entity management API of requery. DatabaseSource extends SQLiteOpenHelper and using the requery API automatically creates tables from the EntityModel provided. It also provides basic upgrade support by adding missing columns and tables on a upgrade. Every time you change the model be sure to increment the schemaVersion passed to DatabaseSource. If you need to perform more complex migrations just override onUpgrade and perform the necessary migration steps.

SQLCipher is a popular extension to SQLite that encrypts the entire database. You can use requery with SQLCipher by using io.requery.android.sqlcipher.SqlCipherDatabaseSource instead of DatabaseSource when creating the store and providing a database password. Also be sure to include the SQLCipher dependency in your gradle file

compile 'net.zetetic:android-database-sqlcipher:<version>'

SQLite support library

See the project page for more information. This is an embedded version of SQLite you can include with your application instead of the default system version. To use it include the SQLite library dependency and use io.requery.android.sqlitex.SqlitexDatabaseSource instead of DatabaseSource when creating the database store.

Databinding

You can take advantage of the new databinding library from Google in your entities. Simply extend Observable and provide @Bindable on bindable properties.

@Entity
public interface Person extends Observable, Parcelable, Persistable {

    @Key @Generated
    int getId();

    @Bindable // android databinding annotation
    String getName();
...

With binding enabled you can automatically update your UI when the underlying database entity is changed or updated.

Adapters

A typical use of a query Result instance is to use it's items to populate a list. For this QueryRecyclerAdapter can be used to populate a RecyclerView with query result data.

Async operations

For async operations we recommend using RxJava. requery provides a complete API that encapsulates all common database operations (insert/update/delete/refresh) using the Rx Single API. See here for more information.

Example project

See the requery-android/example project for a working example.

Logging

Use databaseSource.setLoggingEnabled(true); to enable sql statements and entity operations output to the ADB log.