Missing Android drawable resource in release build

I’m using flutter_local_notifications to implement a daily reminder for my app. I have added a drawable resource for the notification icon via Android Studio and am passing it to AndroidInitializationSettings.

final androidSettings = AndroidInitializationSettings(
  '@drawable/book_2_filled'
);

This all works great in debug builds. I can set notifications and see the icon when they arrive. However, in release builds, I get the following runtime error when trying to initialize FlutterLocalNotificationsPlugin:

PlatformException(invalid_icon, The resource @drawable/book_2_filled could not be found. Please make sure it has been added as a drawable resource to your Android head project., null, null)

Things I have tried:

  • Passing just ‘book_2_filled’ to AndroidInitializationSettings:
    Again, works in debug, not in release
  • Passing in ‘@mipmap/ic_launcher’ to AndroidInitializationSettings:
    Works both in debug and release!

So, apparently book_2_filled is getting lost somewhere in the release build (but ic_launcher isn’t). Does anyone have any ideas?

It looks like it has to do with shrinkResources. Adding buildTypes.release.shrinkResources = false works.

My understanding is that I need to add an r8-rules.pro file if I want shrinkResources on but to keep a specific file. Added this to buildTypes.release:

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'r8-rules.pro'

Working on what the correct incantation in the file is. Supposedly the following should work but it doesn’t:

-keep class your.package.name.R$drawable { *; }

Or?

-keep class your.package.name.R$drawable {
    public static int resource_name;
}

(Yes, I’m putting in my values for ‘your.package.name’ and ‘resource_name’. :slightly_smiling_face:)

Rather than using proguardFiles to keep my drawable resource from being pruned (which I could never get to work), I went with a keep.xml file.

https://developer.android.com/build/shrink-code#keep-resources

I dropped it into the same drawable directory my resource was in and gave it these contents.

<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@drawable/book_2_filled"
/>

And then, of course, the drawable resource disappears again when building an aab.

Retreat, retreat! (Turned off shrinkResources.)