Search code examples
androidsqliteandroid-roomandroidx

Error Inserting : NOT NULL constraint failed: (code 1299) in Room Database


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


Solution

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