gpt4 book ai didi

Android DBFlow 不生成数据库

转载 作者:行者123 更新时间:2023-11-30 00:49:45 24 4
gpt4 key购买 nike

我正在尝试合并 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)。构建());
  • 看完this我尝试将版本更改为 4.0.0-beta2。没有骰子
  • 将以下内容添加到我的混淆器规则中

.

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

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

最佳答案

在依赖项中将 annotationProcessor 更改为 apt

关于Android DBFlow 不生成数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41251615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com