Hilt Class Generation Fails After Upgrading to AGP 8.5.1 from 8.0.1
Imfarrik opened this issue · 6 comments
Description:
After upgrading our project to Gradle 8.5 and enabling minification in our modules, Hilt class generation fails. This results in missing bindings during the compilation process. The error points to missing @Provides-annotated methods, although these methods are correctly defined in the code.
Environment:
Gradle Version: 8.7
AGP Version: 8.5.1
Hilt Version: 2.51.1
ProGuard/R8: Enabled
R8 Version: 8.3.37
Steps to Reproduce:
Upgrade project to AGP 8.5.1.
Enable minification (ProGuard/R8) in build.gradle for the app and modules.
Build the project.
Expected Behavior:
Hilt should correctly generate all necessary classes and bindings during the build process.
Actual Behavior:
Compilation fails with the following error:
/Users/username/StudioProjects/projectname/app/build/generated/hilt/component_sources/developRelease/com/example/projectname/App_HiltComponents.java:443: error: [Dagger/MissingBinding] com.example.projectname.data.CardsProdRepository cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements FragmentGetContextFix.FragmentGetContextFixEntryPoint,
ProGuard/R8 Configuration:
ProGuard rules have been added to preserve Hilt classes:
# Hilt
-keep class dagger.hilt.** { *; }
-keep @dagger.hilt.android.lifecycle.HiltViewModel class * { *; }
-keep @dagger.hilt.DefineComponent.Builder class * { *; }
-keep @dagger.hilt.migration.DisableInstallInCheck class * { *; }
# Module-specific rules
-keep class com.example.projectname.data.CardsProdRepository { *; }
-keep class com.example.projectname.** { *; }
Relevant Code Snippets:
Repository Module:
@Module
@InstallIn(ViewModelComponent::class)
object InternalDataCardsModule {
@Provides
@Reusable
internal fun providesProdRepository(
localSource: CardsLocalSource,
remoteSource: CardsRemoteSource,
cacheRequestDataSource: CacheRequestDataSource
): CardsProdRepository = CardsProdRepositoryImpl(localSource, remoteSource,cacheRequestDataSource)
}
Build Configuration (module/build.gradle):
<img width="687" alt="Screenshot 2024-08-08 at 10 56 48" src="https://github.com/user-attachments/assets/05ae62ee-f0c9-4da8-8dfe-8284730fa835">
Workaround:
Disabling minification resolves the issue, but this is not a viable solution for production builds where minification is necessary.
Additional Context:
The error occurs across multiple modules that rely on Hilt for dependency injection.
The same configuration worked as expected prior to the upgrade to Gradle 8.5.
Thank you for your assistance.
Same in here, unable to make it work. Do you have any updates for this?
After upgrading our project to Gradle 8.5 and enabling minification in our modules, Hilt class generation fails.
Hilt does not work if you're minifying your modules independently.
Why are you enabling minification in your modules? Typically, minification is a whole program optimization so it would happen at the application level (since that's the place where you can actually know what your application will use from its module dependencies).
Same in here, unable to make it work. Do you have any updates for this?
I found a solution only by disabling the minification in the modules
After upgrading our project to Gradle 8.5 and enabling minification in our modules, Hilt class generation fails.
Hilt does not work if you're minifying your modules independently.
Why are you enabling minification in your modules? Typically, minification is a whole program optimization so it would happen at the application level (since that's the place where you can actually know what your application will use from its module dependencies).
Is there really no way to save generated hilt classes?
Is there really no way to save generated hilton classes?
IMO, it's not even really about Hilt.
AFAIK, minification is a whole program optimization and needs access to the whole program to work properly. For example, even if you were able to keep all of the Hilt classes in your module, that's not really what you want because some of those Hilt classes might not actually be used by your application. What you actually want is to run minification at the application level so it keeps whatever classes are used and removes whatever classes aren't used (which isn't really specific to Hilt).
Is there really no way to save generated hilton classes?
IMO, it's not even really about Hilt.
AFAIK, minification is a whole program optimization and needs access to the whole program to work properly. For example, even if you were able to keep all of the Hilt classes in your module, that's not really what you want because some of those Hilt classes might not actually be used by your application. What you actually want is to run minification at the application level so it keeps whatever classes are used and removes whatever classes aren't used (which isn't really specific to Hilt).
I think by disabling the minification in the modules I made the right decision, thanks for the quick response.