6
votes

I'm writing a new Kotlin library module to add commonly used classes and extension methods. I'm compiling each extensions file (eg BitmapExtensions.kt) into a single file called 'Extensions' using JvmMultifileClass:

@file:JvmName("Extensions")
@file:JvmMultifileClass

fun Bitmap.crop(cropRect: Rect) = ...

As a part of our company's IP policy I need to obfuscate the library via proguard before it makes it to any consumer project. I therefore need a proguard rule to keep these extension methods so that they are not thrown away.

At first glance the below seems to work, inspecting the generated AAR's classes.jar shows that the extension methods still exist and their internals have been obfusecated. But when actually compiling in a consumer project, I get unresolved references to any extension method I've referenced.

-keepclassmembernames class mycompany.kotlin.util.Extensions {
    <methods>;
}

When I disable minification, the project builds correctly.

How can I obfuscate my library and keep my kotlin extensions?

edit: It appears that using keep on the entire package, and even specifying -dontshrink is not enough. The methods simply cannot be referenced in the consumer app.

edit2: I believe I have narrowed this down to the .kotlin_module file under /META-INF/ being removed when minifyEnabled=true. This appears to be the ONLY difference between my minified and non-minified classes.jar. Something is removing this file... or preventing its generation.

2

2 Answers

2
votes

I upgraded to gradle 4.1 milestone 1. The .kotlin_module metadata file is no longer being stripped when minifyEnabled=true and my consumer app can now use extension methods from the lib correctly.

0
votes

For those who are still struggling with this in 2022. What helped me was to intentionally merge the .kotlin_module to the library by adding this snippet to the library's module build.gradle:

packagingOptions {
    merge "/META-INF/*.kotlin_module"
}