Dagger generates map size check that cannot be optimized by R8
See original GitHub issueWhen inspecting the optimized and decompiled code of my app for a completely different reason, I noticed this weird code (obfuscation turned off for better readability):
LinkedHashMap newLinkedHashMapWithExpectedSize = DaggerCollections.newLinkedHashMapWithExpectedSize(27);
newLinkedHashMapWithExpectedSize.put(BaseActivity.class, daggerAppComponent.baseActivitySubcomponentFactoryProvider);
...
newLinkedHashMapWithExpectedSize.put(NotificationClearBroadcastReceiver.class, daggerAppComponent.notificationClearBroadcastReceiverSubcomponentFactoryProvider);
if (newLinkedHashMapWithExpectedSize.size() != 0) {
map = Collections.unmodifiableMap(newLinkedHashMapWithExpectedSize);
} else {
map = Collections.emptyMap();
}
The check for size() != 0
is unnecessary as the map obviously is not empty.
The generated code by dagger looks like this
MapBuilder.<Class<?>, Provider<AndroidInjector.Factory<?>>>newMapBuilder(27).put(BaseActivity.class, (Provider) baseActivitySubcomponentFactoryProvider). ... .build()
Seems like R8 is able to strip the MapBuilder
, but not the size check.
I think an easy fix could be checking for the size that has been passed to the constructor of the builder instead of calling size()
(R8 will be able to optimize that), an even better one to not generate that size check at all when the size is known at compile time (R8 doesn’t even need to optimize it).
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Android R8 Unable to find method - Stack Overflow
The issue is that you are using an R8 distribution version 1.5.68 ( classpath 'com.android.tools:r8:1.5.68' ) together with the Android Gradle ...
Read more >Class keys into map don't work in Kotlin 1.3.30 #1478 - GitHub
Setup: AGP 3.3.x Dagger 2.21+ Kotlin 1.3.30 Gradle 5.3.1 Error: e: ... based on KClass<out ViewModel keys produces an error.
Read more >Migrating the Google I/O app to Hilt | Android Developers
Migrating the Google I/O app to Hilt. Hilt is the new library built on top of Dagger that simplifies Dependency Injection (DI) in...
Read more >ProGuard Manual: Troubleshooting - Guardsquare
If you are keeping the LocalVariableTable or LocalVariableTypeTable attributes, ProGuard's optimizing step is sometimes unable to update them consistently. You ...
Read more >Optimize for Android (Go edition) : Lessons from Google apps ...
Try to reduce dalvik code size: Check for code that is never used at runtime, eg) large classes and auto-generated code.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes its Daggers MapBuilder, I should have been more clear about that.
@JakeWharton I would appreciate it if you do a feature request
Thanks! Since it’s a one-liner I won’t bother with a PR since it’ll actually make things harder.
@connyduck do you want to pursue the R8 feature request? If not I am happy to.