Skip to content
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

Android R8 seems to alter the tag name when companion object extends KLogging() #115

Closed
1951FDG opened this issue May 6, 2020 · 8 comments

Comments

@1951FDG
Copy link

1951FDG commented May 6, 2020

Hello,

Thanks for this library, I'm using it :)

I have a class MainActivity with a companion object that extends KLogging():

I noticed that the tag name changes when R8 is on:

When R8 is on: io.github.getsixtyfour.openpyn.MainActivity$b

When R8 is off: io.github.getsixtyfour.openpyn.MainActivity

Browsing the code I noticed that KLoggerNameResolver is responsible for unwrapping the companion class to the enclosing class, I am however uncertain if there is a bug in the implementation or if R8 is doing something funky of which I am unfamiliar with...

Thanks

@1951FDG 1951FDG changed the title R8 on Android seems to alter the generated tag name when Companion object extends KLogging() Android R8 seems to alter the tag name when companion object extends KLogging() May 6, 2020
@oshai
Copy link
Owner

oshai commented May 11, 2020

It might be so. You can try the alternative syntax of file level field if that helps.
If you can add a project that reproduces it I can try to debug the issue.

@1951FDG
Copy link
Author

1951FDG commented Jun 16, 2020

Sorry for the late reply, the issue occurs only when R8 is enabled. I have created a clean sample project that reproduces the issue in release build variant, I'm grateful if you can try to debug the issue.

https://github.com/1951FDG/MyApplicaton

@oshai
Copy link
Owner

oshai commented Jun 17, 2020

@1951FDG Thanks, can you please add the steps to reproduce the issue in that repo?

@oshai
Copy link
Owner

oshai commented Jun 24, 2020

@1951FDG I am still not sure what are the steps to reproduce the issue.

@1951FDG
Copy link
Author

1951FDG commented Jun 27, 2020

Sorry for the delay again

  1. Open Android Studio
  2. Open repo folder in Android Studio
  3. Go to Build > Select Build Variant...
  4. Change Active Build Variant from debug to release
  5. Go to Run > Run 'app' (use emulator or your android phone if you have one)

The GUI of the app will show a text field, which is set to the name of the logger,

When active build is release build, the text field will show:
com.example.myapplication.MainActivity$a

When active build is debug build, the text field will show:
com.example.myapplication.MainActivity

The issue occurs in release build when R8 (shrinker in the Android build process) is on (minifyEnabled == true)

@oshai
Copy link
Owner

oshai commented Jun 28, 2020

The minification process caused the companion object to have different characteristics than a usual Kotlin Companion object. Since we are using a fragile mechanism that uses reflection, it doesn't work for this case, and I am not sure it is worth allowing it as it might break other use cases.
Anyway, I suggest that you use the following configuration that should work for the release build:

companion object {
        private val logger = KotlinLogging.logger {}
}

Please let me know if that is good enough as a solution.

@1951FDG
Copy link
Author

1951FDG commented Jun 29, 2020

Thanks, this works perfectly!

Maybe include this info in the documentation for Android devs...

@1951FDG 1951FDG closed this as completed Jun 29, 2020
@oshai
Copy link
Owner

oshai commented Jun 29, 2020

Added it here to the wiki.

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

No branches or pull requests

2 participants