0

I use this for loop to load a folder of json files

fun getJsons(): ArrayList<List<Item>> {

    var items = ArrayList<List<Item>>()

    try {

        var ims = MyApplication.appContext.assets.list("foldername")!!

        for (i in 0..ims.size - 1) {

            val res = MyApplication.appContext.assets.open("foldername/" + ims[i])

            val size = res.available()

            val buffer = ByteArray(size)

            res.read(buffer)

            val json = String(buffer, charset("UTF-8"))

            val item:AzAll =  Gson().fromJson(json,AzAll::class.java)

            val content = (0..item.header.count() -1).map { it:Int -> Item(item.header[it],item.items[it],item.count[it],item.notes[it]) }

            res.close()

            items.add(content)
        }

        return items

    } catch (ex: IOException) {
        return items
    }
}

The resources exists in assets folder inside foldername , now when i try to obfuscate the app with

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

it crashes with

java.lang.NullPointerException: Attempt to get length of null array at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)

When i get back to

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

everything works , what's the problem ?

sheko
  • 516
  • 4
  • 15

1 Answers1

1

The problem is almost certainly that your JSON model class (AzAll) is getting its fields renamed, and so the JSON is not being deserialized properly.

To fix it, you could either simply annotate the class with @Keep, or add rules to the proguard-rules.pro file to not rename the class's members.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • can you elaborate how to do this inside `proguard-rules.pro` given model name is `AzAll` ? – sheko Mar 10 '20 at 07:47
  • Also i have a txt file and it reads properly without issues on both cases and i have a model for it without `@keep` why it doesn't work with json only ? – sheko Mar 10 '20 at 08:19
  • after opening apk with jadx i see class names appearing inside com -> package name any way to obfuscate them also @Ryan – sheko Mar 10 '20 at 08:19