简体   繁体   中英

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

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.

我升级到 gradle 4.1 里程碑 1。当minifyEnabled=true时, .kotlin_module元数据文件不再被剥离,我的消费者应用程序现在可以正确使用库中的扩展方法。

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"
}

EDIT: Turned out that the problem was caused by having the kotlin meta info excluded from the build result, which I didn't notice initially, so what you really have to do is finding and removing the following code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM