android - 如何使用 Kotlin 为 Android 发布版本配置 ProGuard

标签 android kotlin proguard android-proguard

我将 Kotlin 1.2.71 添加到我的 Android (Java) 应用程序项目中。 该项目针对 Java 8 配置:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

以下是 Kotlin 的变化:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71"

...

apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"
apply plugin: "kotlin-kapt"

...

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.71"
kapt "org.parceler:parceler:1.1.11"

我注意到使用 ProGuard 的发布构建失败了。构建过程输出以下信息:

Warning: kotlin.internal.jdk8.JDK8PlatformImplementations: can't find referenced 
    method 'int start(java.lang.String)' in library class java.util.regex.Matcher
Warning: kotlin.internal.jdk8.JDK8PlatformImplementations: can't find referenced 
    method 'int end(java.lang.String)' in library class java.util.regex.Matcher
Warning: kotlin.internal.jdk8.JDK8PlatformImplementations: can't find referenced 
    method 'java.lang.String group(java.lang.String)' in library class java.util.regex.Matcher

Note: kotlin.internal.PlatformImplementationsKt: can't find dynamically 
    referenced class kotlin.internal.JRE8PlatformImplementations
Note: kotlin.internal.PlatformImplementationsKt: can't find dynamically 
    referenced class kotlin.internal.JRE7PlatformImplementations
Note: kotlin.jvm.internal.Reflection: can't find dynamically 
    referenced class kotlin.reflect.jvm.internal.ReflectionFactoryImpl

Note: the configuration keeps the entry point 
    'com.google.android.gms.flags.impl.FlagProviderImpl { void init(com.google.android.gms.dynamic.zzd); }', 
    but not the descriptor class 'com.google.android.gms.dynamic.zzd'

Note: there were 1 unkept descriptor classes in kept class members.
      You should consider explicitly keeping the mentioned classes
      (using '-keep').
      (http://proguard.sourceforge.net/manual/troubleshooting.html#descriptorclass)
Note: there were 3 unresolved dynamic references to classes or interfaces.
      You should check if you need to specify additional program jars.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)
Warning: there were 3 unresolved references to library class members.
         You probably need to update the library versions.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember)
Warning: Exception while processing task java.io.IOException: Please correct the above warnings first.
Thread(Tasks limiter_9): destruction

据我所知,Kotlin 不需要任何特殊的 ProGuard 规则。 People recommend (2015 年发布)简单地说规则:

-dontwarn kotlin.**

这仍然是推荐的解决方案吗?

相关

最佳答案

查看实际警告告诉您它无法在 java 标准库中找到特定方法。

引用 Matcher.end(String)表明这是一种仅在您的 minSdk 为 26 时可用的方法。这是添加 Java-8 支持时的 SDK 级别。

简单地说,您正在尝试将 java8 级 kotlin 库与不支持它的 android sdk 一起使用,从而导致您观察到的错误。

关于android - 如何使用 Kotlin 为 Android 发布版本配置 ProGuard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052721/

相关文章:

android - 无法使用 SmartLock 保存 IdentityProviders.GOOGLE 凭据

android - Android通知不会弹出

android - 如何在android compose中更改对话框的宽度

java - 有没有办法在 Kotlinx-Serialization 中序列化 map

android - 无法在 android studio 中创建 AVD

java - 为什么我的 Android 程序中会出现 PatternSyntaxException?

Android - 是否可以在文本上叠加文本?

java - 启用 Proguard 时使用 Retrofit2 的 NullException int java.util.List.size()

android - 使用带有 proguard 关闭力的 jsoup 关闭

java - 混淆共享的 Maven 模块?