Postmortem: ProGuard is annoying

Posted on Feb 12, 2022

This morning I received several crash reports from last night's release.

I wasn't able to replicate this crash on my debug environment. So this should be a production-only crash.

The first thing that came to my mind while I read the crash report is that this is a Proguard issue. The crash was happening in this class

import android.content.SharedPreferences
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class OnlineJobsViewModelFactory(val sharedPreferences: SharedPreferences) : ViewModelProvider.Factory {

    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return modelClass.getConstructor(SharedPreferences::class.java).newInstance(sharedPreferences)
    }
}

The OnlineJobsViewModelFactory wasn't able to find the interface android.content.SharedPreferences so it is throwing a

java.lang.NoSuchMethodException

So, I went to the Official Android Lifecycle documentation to find a Proguard rule for help but there was none. I had to dig the internet and found the rules on this GitHub Issue.

And eventually it was the fix that I needed.

I just wish the Android team would just include the Proguard rules in their documentation.