1

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

以下是我写的相关文件:

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());
  • 阅读内容后,我尝试将版本更改为 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.* { *; }

然而我仍然遇到同样的错误——数据库没有注册。我真的看不出我做错了什么。我已经回去并多次查看文档以确保一切设置正确,但我仍然没有遇到此错误。任何帮助将不胜感激!

4

1 回答 1

1

在依赖项中更改annotationProcessorapt.

于 2017-03-08T15:48:42.860 回答