问题描述
首先,请注意,我不希望出现why do you want to obfuscate library
注释.这是我要问的一个真正的问题.
First of all, please note that I'm not expecting why do you want to obfuscate library
comments. This is a genuine problem I'm asking about.
在用Kotlin编写的Android库处理R8/混淆处理时,我一直遇到问题.
I have been having an issue dealing with R8/obfuscation with an Android library written in Kotlin.
我有一个用@JvmStatic
注释的公共API方法,该方法将Lambda
作为参数.
I've a public API method which is annotated with @JvmStatic
and that method takes a Lambda
as parameter.
例如,看下面的代码,
typealias MyLambdaCallback = (String, Map<String, Any>) -> Unit
@Keep
object MyApi {
private var callback: MyLambdaCallback? = null
@JvmStatic
fun setCallback(callback: MyLambdaCallback) {
this.callback = callback
}
}
我添加了@Jvmstatic
,以便Java
调用代码可以静态调用该方法,而不是执行MyApi.INSTANCE.setCallback()
I have added @Jvmstatic
so that Java
calling code can call the method statically rather than doing MyApi.INSTANCE.setCallback()
当我发布不带minification
的库时,一切都很好,并且Java
和Kotlin
的调用代码均按预期编写.
When I release the library without minification
, everything is fine and calling code from both Java
and Kotlin
is written as expected.
但是现在我想在打开minification
的同时释放该库.
But now I want to release the library while turning on minification
.
这会带来问题.
这是错误
我是在某个地方犯了错误还是应该认为是某种限制?
Am I making a mistake somewhere or this is expected as some kind of limitation ?
我尝试了什么?
-
删除
@Jvmstatic
可以解决问题,但它创建了难看的Java调用代码
Removing
@Jvmstatic
resolves the issue but it created ugly Java calling code
保留了@Jvmstatic
,但删除了Lambda
,将Lambda转换为interface with one method
,并且一切正常.不幸的是SAM for Kotlin classes
还没有,所以调用Kotlin
代码看起来很丑.
Kept @Jvmstatic
but removed Lambda
converting Lambda into an interface with one method
and everything is working fine. Unfortunately SAM for Kotlin classes
is not there yet, so calling Kotlin
code looks ugly.
推荐答案
R8团队已已修复此问题以及相关问题,2.1.42
R8 team has fixed this issue along with related issue b/158400283 in R8 version 2.1.42
修复程序应该已经在Android Studio 4.1 beta
或更高版本中可用,但是如果您使用稳定的Android Studio 4.0
,请在顶级build.gradle文件中添加以下内容:
Fix should already be available in Android Studio 4.1 beta
or higher, but if you're using stable Android Studio 4.0
then add following to your top-level build.gradle file:
buildscript {
repositories {
maven {
url 'https://storage.googleapis.com/r8-releases/raw'
}
}
dependencies {
classpath 'com.android.tools:r8:2.1.42' // Must be before the Gradle Plugin for Android.
classpath 'com.android.tools.build:gradle:X.Y.Z' // Your current AGP version.
}
}
这篇关于用Kotlin编写的Android库公共API中的R8 + JvmStatic注释+ Lambda处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!