question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dagger generates map size check that cannot be optimized by R8

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
connyduckcommented, Jun 3, 2020

Yes its Daggers MapBuilder, I should have been more clear about that.

@JakeWharton I would appreciate it if you do a feature request

0reactions
JakeWhartoncommented, Jun 3, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found