Search code examples
androidproguardobfuscationminifyandroid-viewbinding

Missing required view with ID when enabling proguard


When making seemingly irrelevant changes like updating the androidx.activity:activity-ktx dependency or changing the xml in an activity that hasn't been called yet, the application fails on start up with errors like:

java.lang.NullPointerException: Missing required view with ID: com.example:id/skipped
    at com.example.databinding.ActivityMainBinding.bind(Unknown Source:111)
    at com.example.databinding.ActivityMainBinding.inflate(Unknown Source:13)
    at com.example.databinding.ActivityMainBinding.inflate(Unknown Source:2)
    at com.example.MainActivity.onCreate(Unknown Source:7)
    at android.app.Activity.performCreate(Activity.java:8305)
    at android.app.Activity.performCreate(Activity.java:8284)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3626)

The exact ID is different depending on what is changed.

If I set minifyEnabled to false. These errors do not appear.

Perhaps related to ViewBinding, I tried:

-keep class com.example.databinding.** { *; } But that didn't change it either


Solution

  • you could try

    -keep class com.example.R$id { *; }
    

    This tells ProGuard to keep all fields in the R.id class, which contains the integer values of all view IDs defined in your layout files