Android DBFlow没有生成数据库

时间:2016-12-20 21:35:15

标签: android dbflow

我正在尝试将DBFlow合并到Android应用程序中。我已经按照文档here进行了操作,但未能使该库工作。

以下是我写的相关文件:

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.**

在我的Application课程中,我在onCreate()回调期间运行了以下内容

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);
}

以下是各自的数据库类

AppDatabase.java

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

一个死的简单模型类,如下所示 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;

    // ...

}

以下代码会引发错误

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();

这会触发以下异常

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)

如果我尝试在事务之外使用creditCardDbModel.save()(即同步保存),我会收到类似的错误:

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?

我尝试了什么

  • 禁用即时运行
  • 删除SQLCipher集成,即将initFlowDb()更改为FlowManager.init(new FlowConfig.Builder(this).openDatabasesOnInit(true).build());
  • 阅读this后,我尝试将版本更改为4.0.0-beta2。没有骰子
  • 在我的proguard规则中添加了以下内容

-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.* { *; }

然而,我仍然遇到了同样的错误 - 数据库未注册。我真的看不出我做错了什么。我已经回去查看了几次文档,以确保所有内容都设置正确,但我仍然没有遇到此错误。任何帮助都将深表感谢!

1 个答案:

答案 0 :(得分:1)

在家属中将annotationProcessor更改为apt