Fix "InitializationProvider" Error in the AGP 9 Era 🚀

Hi fellow Android devs! 🤖

You’ve finished your app, everything works perfectly in Debug mode, and you’re finally ready to hit that "Release" button. But then… CRASH. 💥

You look at the logs and see this scary message:


Fatal Exception: java.lang.RuntimeException: 
  Unable to get provider androidx.startup.InitializationProvider

Don't worry! You’re not alone, and your code isn't "broken." You've just run into a little disagreement between WorkManager and R8 (the code shrinker), especially if you're using the latest AGP 9 (Android Gradle Plugin).

Let’s fix it together in 3 minutes! ☕️

 

What’s happening under the hood? 🧐

When you set isMinifyEnabled = true for your release build, a smart tool called R8 starts cleaning up your code. It looks for anything "unused" and removes it to make your app tiny.

However, WorkManager (the tool that handles background tasks for things like AdMob or Firebase) has a little secret: it uses a class called WorkDatabase_Impl.

The problem? R8 doesn't see anyone "calling" this class in your code, so it thinks, "Hey, this is trash!" and throws it away. When your app starts, WorkManager looks for its database class, finds nothing, and—BOOM—the app crashes.

With AGP 9, R8 is stricter than ever, so we have to be very clear about what we want to keep.

You don't even need to update your library versions. Just follow these steps.

 

Step 1: Tell R8 to "Hands Off!" 🛑

Open your proguard-rules.pro file and add these lines. This tells the compiler: "I know it looks unused, but I need this! Please don't touch it."


# Keep the WorkManager internal database!
-keep class androidx.work.impl.WorkDatabase_Impl { *; }

# Also keep the "Worker" constructors so they can do their jobs
-keep class * extends androidx.work.ListenableWorker {
    <init>(android.content.Context, androidx.work.WorkerParameters);
}

 

Step 2: Give it a Fresh Start ✨

AGP 9 loves caching things. To make sure your new rules are applied:


Click Build > Clean Project.
Click Build > Rebuild Project.

 

Step 3: The "Magic" Re-install 📲

If your app tried to start and failed, it might have left some messy, half-finished files behind. Uninstall the app from your phone/emulator first, then install the new build. This ensures a 100% clean slate!

 

Wrapping Up 🎁

That’s it!

Your app should now be running smoothly even with isMinifyEnabled = true.

The AGP 9 era brings us faster and smaller apps, but it also means we have to be a bit more specific with our ProGuard/R8 rules. Keep an eye on those "Impl" classes, and you'll be a release-build master in no time!

Happy coding! 💻✨

👉 【Android/AGP9対応】AdMob起因?WorkManagerとApp Startupで頻発するクラッシュをProGuard設定で解決する


Related Categories :  AndroidAndroidStudioDevelopmemtGradleKotlinRecommended