英文:
Error Inserting : NOT NULL constraint failed: (code 1299) in Room Database
问题
我正在尝试向我的表中插入值,但每次都会给我以下错误:android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: UserModel.Id (code 1299)。很奇怪的是,相同的代码在调试构建中是可以工作的,但当它是一个签名的 APK 时,就会出现这样的错误。
我的 UserModel 如下所示:
@Entity
public class UserModel {
@PrimaryKey
@NonNull
String Id;
String Fname, Sname;
@NonNull
public String getId() {
return Id;
}
public void setId(@NonNull String id) {
Id = id;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
}
在将数据插入表中时,我使用了 Gson,代码如下所示:
JSONArray payloadArray = new JSONArray(response);
Type type = new TypeToken<List<UserModel>>() {}.getType();
List<UserModel> userList = new Gson().fromJson(payloadArray.toString(), type);
database.userDao().insertData(userList);
在我的构建变体中,我进行了以下设置:
storeFile file('E:\\KeystoreFile\\Sample.jks')
------------------------------------------------------------------
debug {
minifyEnabled false
debuggable true
initWith debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER_URL", project.properties["server.url"]
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher"
]
applicationIdSuffix ".qa"
}
我的 Room 版本是 2.0.0。我已经尝试过最新版本 2.2.3 和其他版本,似乎没有任何作用。此外,我已经迁移到 AndroidX,不确定是什么原因导致了这个错误,任何帮助将会很棒。
谢谢
英文:
I am trying to insert values into my table but every time it gives me the following error , android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: UserModel.Id (code 1299) . It's very strange that the same code works in debug build but when it's a signed apk it gives me such error.
My UserModel Looks like this:
@Entity
public class UserModel {
@PrimaryKey
@NonNull
String Id;
String Fname,Sname;
@NonNull
public String getId() {
return Id;
}
public void setId(@NonNull String id) {
Id = id;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname= fname;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname= sname;
}
}
While inserting the data in to table I've used Gson where it looks like:
JSONArray payloadArray = new JSONArray(response);
Type type = new TypeToken<List<UserModel>>() {}.getType();
List<UserModel> userList = new Gson().fromJson(payloadArray.toString(), type);
database.userDao().insertData(userList);
In my build variant I've set the following:
storeFile file('E:\\KeystoreFile\\Sample.jks')
------------------------------------------------------------------
debug {
minifyEnabled false
debuggable true
initWith debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "SERVER_URL", project.properties["server.url"]
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
manifestPlaceholders = [
appIcon: "@mipmap/ic_launcher"
]
applicationIdSuffix ".qa"
}
and my room version is 2.0.0. I tired with latest version 2.2.3 and other versions as well doesn't seem to do anything .Also I've migrated to Androidx not sure what causing this error, any help would be great
Thank you
答案1
得分: 2
你可以查看以下链接获取更多信息 ProGuard规则
至于我的问题,我已经设置了ProGuard规则如下:
-keep class app.mypackage.model.** { *; }
英文:
For more info you can check the following link ProGuard Rules
And for my problem I've set proGuard rule as :
-keep class app.mypackage.model.** { *; }
答案2
得分: 0
当您创建签名 APK 时,您的 UserModel
类变量发生了变化,这会在插入时引发问题。
请添加以下代码到 proguard 规则 中:
-keep class * extends androidx.room.RoomDatabase
-dontwarn androidx.room.paging.**
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
英文:
when you create sign apk, your UserModel
class variable changed. and its create issue when insert.
add below code for proguard rules
-keep class * extends androidx.room.RoomDatabase
-dontwarn androidx.room.paging.**
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论