0

I use Android + Kotlin + Proguard

Code

inferface Authentication {
 val authenticated: Boolean
 val lastAuthentication: Instant

 fun authenticate(force: Boolean = false): Flow<Event>
 suspend fun revokeAccess() 
}

And an implementation of that interface

internal sealed class AuthenticationInternal(
 private val trustProvider: TrustProvider) : Authentication {
 override val authenticated: Boolean
    get() = trustProvider.authenticatedBefore()

 override val lastAuthentication: Instant
    get() = trustProvider.getLastAuthenticatedTime()

 override fun authenticate() = flow {
    trustProvider.checkIntegrity()
    emitAll(internalAuthenticate())
 }.flowOn(Dispatchers.Default)

 override suspend fun revokeAccess() {
    trustProvider.revokeAccess()
 }
}

-> During Kotlin Annotation Processing this interface gets (as expected) a default implementation:

Kotlin generated code:

Class: Authentication$DefaultImpls.dex

-> Now my issue: Since this class is generated and not reachable from Kotlin code, how can I keep this class using progurard.

In my release build, this class is put out of the release dex and the application crashes with an RuntimeException.

Note: Adding this to the proguard mappings file: -keep class com.my.package.AuthenticationInternal$DefaultImpls { *; } will not work, since it is not reachable

How can I keep kapt-generated classes with proguard?

true-mt
  • 347
  • 3
  • 12
  • The keep rule that you mention `-keep class com.my.package.AuthenticationInternal$DefaultImpls { *; }` should keep the class generated by `kotlinc`. Adding a keep rule makes the class always stay, and as kotlinc generates the class it should be in teh input to R8. Please file a bug on https://issuetracker.google.com/issues/new?component=326788 with details on your build setup, if you continue to have issues. – sgjesse Aug 15 '22 at 11:02
  • No that is not possible, since kapt generated classes can not be called from Kotlin code and also not from proguard file – true-mt Aug 22 '22 at 17:13
  • The place where the class is generated should not matter, as the proguard file is applied to all code compiled. You can try to generate a [compiler dump](https://r8.googlesource.com/r8/+/1d07b2900464c95fbb57c54375cbd1873e394aff/doc/compilerdump.md), then you can see the code which is the actual input to R8. – sgjesse Aug 26 '22 at 12:58

0 Answers0