2

Problem: Android local database doesn't work when I make a signed APK.

More Info: I tried to use the room database, it failed. I then tried Sugar ORM, still, it failed.

Everything works perfectly in debug mode until I make a signed APK. It cannot write or read from the local database. I have updated Android Studio but still, fail.

Navjot
  • 1,202
  • 1
  • 11
  • 24
MUHINDO
  • 788
  • 6
  • 10
  • check your obfuscation rules. Most likely your model classes are obfuscated and that interferes with Sugar's reflection logic. Must be something similar with Room too. You can prevent obfuscation of the models.. – Satya Aug 21 '20 at 20:08

2 Answers2

1

It's because of proguard obfuscation. Add your database models to proguard-rules.pro file to prevent them from being obfuscated during the build. For example, you have a class called ContactModel.java for your contacts table like so:

@Entity(tableName = "contacts")
public class ContactModel {
    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = "name")
    public String name;

    @ColumnInfo(name = "number")
    public String number;

}

And the address of the class is com.example.myapp.model.ContactModel.java. You should add a rule to proguard-rules.pro like this:

-keep public class com.example.myapp.model.ContactModel

If you have a lot of classes and you don't want to add them one by one you just need to add all of them to one package; for example model, and then put * instead of the class name at the end of the address like so:

-keep public class com.example.myapp.model.*
Squti
  • 4,171
  • 3
  • 10
  • 21
0

The problem was, I was not deleting the test generated folder. So, in a signed apk, files in the generated test folder throw errors which may not even lead the application to crash BUT rather stop your application from performing tasks search as saving on local database, or even fetching data from internet.

The solution is: delete generated test folder before making a signed apkenter image description here.

MUHINDO
  • 788
  • 6
  • 10