Skip to content

How can I make "R8's optimization of ServiceLoader" take effect? #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
whataa opened this issue Jan 8, 2025 · 4 comments
Closed

How can I make "R8's optimization of ServiceLoader" take effect? #4324

whataa opened this issue Jan 8, 2025 · 4 comments
Labels

Comments

@whataa
Copy link

whataa commented Jan 8, 2025

This question is mainly about the initialization of android Dispatchers.Main, it has been discussed a long time ago, see #878 .
If I understand correctly, the final solution should be to rely on R8 to optimize ServiceLoader, see ServiceLoaderRewriter

But the problem I encountered is that after R8 processing, the bytecode of MainDispatcherLoader is still the corresponding source code, not the expected, I really don’t know what else I need to pay attention to, need help.

expect:

Arrays.asList(new MainDispatcherFactory[] { new AndroidDispatcherFactory() }).iterator()

actual:
image

I have confirmed that optimization is not turned off, no -dontoptimize, and all rules in configuration.txt containing the kotlinx keyword come from the coroutines only.

env:

  • Gradle: 8.8
  • AGP: 8.5.0
  • kotlin: 2.0.21
  • coroutine: 1.7.1
  • JDK: 17
  • OS: Mac OS X
  • arch: aarch64
  • R8: 8.5.10 (build d827614db6c8c0c52724a1b4850d00da8e51177a from go/r8bot (luci-r8-custom-ci-archive-0-p93x))
@whataa whataa added the bug label Jan 8, 2025
@dkhalanskyjb
Copy link
Collaborator

Hi! Would it be possible for you to provide a self-contained project where the issue reproduces, along with build instructions for it? There are many moving parts in Android build configurations, so it would greatly simplify the process of reproducing this if we had some code on our hands.

@whataa
Copy link
Author

whataa commented Jan 8, 2025

Thank you, the "self-contained project" you mentioned inspired me. I put the all rules from configuration.txt generated by our project into a new demo project and repeatedly executed r8 by excluding half the rules each time, finally found that the rule causing the problem is as follows:

-keep,allowobfuscation interface * {
    <methods>;
}
# or
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}

@dkhalanskyjb
Copy link
Collaborator

@whataa, I think it's reasonable that these two rules negate the effect of ServiceLoader optimization for MainDispatcherFactory. After all, R8 was explicitly to keep these ServiceLoader-only entities. I don't see this as a bug. Do you see this differently?

@whataa
Copy link
Author

whataa commented Jan 9, 2025

It's really hard to know why until figure out the two rules above. It's not a bug, I'll close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants