Android AGP 8.4 and hilt
Asked Answered
O

2

13

After updating Android Studio to the new release (Jellyfish) and updating the Android Gradle Plugin to 8.4, my release builds are failing with a slew of issues related to things injected with daggar/hilt. I spent the afternoon adding keep rules for everything that's being injected, and ended up an error saying I need a @Provides rule for CoroutineScope, which I could not manage to resolve.

Can anyone give me a concise set of rules for Daggar/Hilt and AGP 8.4?

This project is multi-module project. I was also running into issues with name clashes (two modules mapping different classes to a.a). To solve that I putting:

repackageclasses 'x'

with a different value for x for each module.

In the end, I reverted AGP. But long term, I'll need to figure out a solution (or hopefully updates will fix it).

Osmious answered 5/5, 2024 at 21:45 Comment(13)
I'm having the same issue using hilt version 2.51.1 The name clashing can be related to Library classes are shrunk adding android.disableMinifyLocalDependenciesForLibraries=false didn't produce the name clashes anymore but it's disabling the feature. So for now I ended up reverting AGP.Heavyfooted
@BassemWissa Thank you. That explains quite a lot. So I guess it was obfuscating them, but not minify them? Or was it not obfuscating local dependencies either.Osmious
I am also facing the exact same issue, I also have multi module project. I spent a day to troubleshoot and fix the issue but no luck. At the end I needed to use previous AGP (gradle) version until we have any solution.Excaudate
developer.android.com/build/releases/… This link is something related to it.Excaudate
I now having almost everything working (after spending about 2 days adding new rules). The problem I'm currently see is a service that's in module of its own (mostly, the module contains the service and things that support it). The injected fields aren't being injected. It's fine with old old tools, it's fine with a debug build. The dependency injection is working in the other modules.Osmious
Why is everyone reverting to AGP 8.4? It's clear that, from now on, R8 will be more aggressive, and we should add the rules to the library modules.Cordovan
@NicolasJafelle The issue for me is the app I'm working on is large. Adding the new rules is taking a lot of time. My app takes 7-8 minutes to compile with isMinifyEnabled=true. So it's a very long task add of all new rules.Osmious
@Osmious you mean by adding rules is using repackageclasses 'x' @NicolasJafelle the thing is what rules should we add in case of multiple obfuscated classes that have same package name?Heavyfooted
@BassemWissa repackageclasses 'x', where x is replaced by a unique value per module, just prefixes the obfuscated class names. so instead of a.a you'll get x.a.a.Osmious
I solved my issue by using "isMinifyEnabled = false" in all library modules and only enable minify from app module. After those changed Now I migrated AGP to latest.Excaudate
@MitulVarmora do you mean as a workaround? Or is the way to go from now on? I am also in a big multi 40+ module project which will be a nightmare.Cordovan
@NicolasJafelle That's the right way going forward. It is not temporary workaround. I have posted an answer with full information.Excaudate
@MitulVarmora awesome thank you! I have work to do then... =DCordovan
E
11

Root cause:

If you have enabled isMinifyEnabled = true in library modules then library module will minify it-self before providing aar file to app or any other module.

It means that library will be minified without knowing that which classes are used by other modules (Everything will be minified except some keep rules).

Link to official documentation which might help you understand the changes: Library classes are shrunk

Solution:

Use isMinifyEnabled = false in library modules and add consumerProguardFiles("consumer-rules.pro") to tell app module what to keep, leave it empty if no special case.

Above changes will let app module can minify all codes including your library module codes during proguard process.

After this changes we can now update AGP to 8.4.1 and gradle wrapper to 8.6 without any hilt or proguard issues.

This is how I fixed mine issue. Still if you have any issues please let us know in the comment. It should be fixable by updating proguard rules.

Note: This answer applies to multi-module projects and not to libraries which are getting published somewhere like maven because they might want to minify their library before publishing.

Excaudate answered 23/5, 2024 at 6:3 Comment(2)
What happens to the library modules after 'isMinifyEnabled = false'? Does it still gets minified by the app module or is it skipped completely?Swampy
@Swampy Yes, The app module will minify all library modules considering consumerProguardFiles provided by those modules. Note: You must have isMinifyEnabled = true in app module to make it work.Excaudate
S
2

This issue has been burning me for the last few days. The problem I'm facing is a bug in the implementation of this feature. We have a project library that we want to minify (because it gets shipped to other companies). We also have local tests for it. The local tests do not want to use a minified build, because they are testing individual internals. With jUnit, the classes are located by name, so obfuscation breaks it. If we add the keep rules, then the shipping AAR ends up with classes that should be obfuscated, but aren't. If we don't add the keep rules, the test harness kicks off with a minified release build and can't find the internal classes. The options I'm seeing are to either run all the test cases as debug or create a new build type for tests. Despite Google providing a beautiful way to integrate tests into the product, it appears this is a weakness in AGP 8.4+.

Skinner answered 6/6, 2024 at 23:23 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hesperides

© 2022 - 2025 — McMap. All rights reserved.