Android DBFlow not generating database

2k views Asked by At

I am trying to incorporate DBFlow into an Android application. I have followed through the documentation here but am failing to get the library to work.

Below are the relevant files I have written:

build.gradle

def dbflow_version = "4.0.0-beta3"
def sqlcipher_version = "3.3.1"

dependencies {
    // ...

    // DBFlow
    annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow:${dbflow_version}"
    compile "com.github.Raizlabs.DBFlow:dbflow-sqlcipher:${dbflow_version}"
    compile "net.zetetic:android-database-sqlcipher:${sqlcipher_version}@aar"

}

proguard-rules.pro

# sqlCipher
-keep class net.sqlcipher.** { *; }
-dontwarn net.sqlcipher.**

In my Application class I have the following that is run during the onCreate() callback

private void initFlowDb() {
    DatabaseConfig.OpenHelperCreator openHelperCreator = SqlCipherHelperImpl::new;

    DatabaseConfig databaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
            .openHelper(openHelperCreator)
            .build();

    FlowConfig flowConfig = new FlowConfig.Builder(this)
            .openDatabasesOnInit(true)
            .addDatabaseConfig(databaseConfig)
            .build();

    FlowManager.init(flowConfig);
}

And here are the respective database classes

AppDatabase.java

@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {
    public static final String NAME = "AppDatabase";
    public static final int VERSION = 1;
}

And a dead simple model class like below CreditCardDbModel.java

@Table(database = AppDatabase.class)
public class CreditCardDbModel extends BaseModel {

    @Column
    private String cardNumber;

    @Column
    private int expiryMonth;

    @Column
    private int expiryYear;

    @Column
    private int cvv;

    // ...

}

The following code is what throws the error

FlowManager.getDatabase(AppDatabase.class)
        .beginTransactionAsync(databaseWrapper -> {
            creditCardDbModel.save();
        })
        .success(transaction -> {
            Timber.tag(TAG).d("Done saving model");
        })
        .error((transaction, error) -> {
            Timber.tag(TAG).e("Error saving model");
            Timber.tag(TAG).e(error);
        })
        .build()
        .execute();

This triggers the following exception

E  com.raizlabs.android.dbflow.structure.InvalidDBConfiguration: Database: com.rperryng.android.db.AppDatabase is not a registered Database. Did you forget the @Database annotation?
E      at com.raizlabs.android.dbflow.config.FlowManager.getDatabase(FlowManager.java:115)
E      at com.rperryng.android.ui.main.other.AddPaymentActivity.onSaveCardClicked(AddPaymentActivity.java:107)

If I try to use creditCardDbModel.save() outside of the transaction (i.e., saving synchronously) I get a similar error:

E  com.raizlabs.android.dbflow.structure.InvalidDBConfiguration: Model object: com.rperryng.android.db.CreditCardDbModel is not registered with a Database. Did you forget an annotation?

What I've tried

  • Disabling instant run
  • Removing the SQLCipher integration, i.e. changing initFlowDb() to simply be FlowManager.init(new FlowConfig.Builder(this).openDatabasesOnInit(true).build());
  • After reading this I tried changing the version to 4.0.0-beta2. No dice
  • Added the following to my proguard rules

.

-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder { ; }
-keep class com.raizlabs.android.dbflow.config.GeneratedDatabaseHolder
-keep class * extends com.raizlabs.android.dbflow.config.BaseDatabaseDefinition { *; }
-keep class com.rperryng.android.db.* { *; }

Yet I am still met with the same error - that the Database is not registered. I really can't see what I'm doing wrong. I've gone back and looked through the documentation several times to make sure that everything is setup correctly, but nothing I am still met with this error. Any help would be deeply appreciated!

1

There are 1 answers

0
Steve Campos Vega On

In dependecies change annotationProcessor to apt.