Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin AAR library w/ proguard: how to keep extension methods?

Tags:

android

kotlin

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.

like image 672
Tom Avatar asked Nov 07 '22 20:11

Tom


1 Answers

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.

like image 163
Tom Avatar answered Nov 15 '22 07:11

Tom