【问题标题】:Android DBFlow not generating databaseAndroid DBFlow 不生成数据库
【发布时间】:2016-12-20 21:35:15
【问题描述】:

我正在尝试将 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.* { *; }

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

【问题讨论】:

    标签: android dbflow


    【解决方案1】:

    在依赖项中将annotationProcessor 更改为apt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2019-04-02
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多