启用 Proguard 时未找到 kotlin.Any

Posted

技术标签:

【中文标题】启用 Proguard 时未找到 kotlin.Any【英文标题】:kotlin.Any not found when enabling Proguard 【发布时间】:2021-12-28 23:18:48 【问题描述】:

当启用 Proguard 时,我看到运行时崩溃。我基本上将所有内容都保留在 Proguard 规则中。

-dontobfuscate
-dontoptimize
-dontshrink
-addconfigurationdebugging
-dontpreverify
-dontwarn ***

-keep class ***  *** *; <methods>; <init>(...);
-keep interface ***  *** *; 
-keep @interface ***  *** *; 
-keep enum ***  *** *; 
-keepattributes ***
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!code/allocation/variable,!field/*,!class/merging/*,!method/removal/*,!class/unboxing/enum

是的,这可能不是 Proguard 的最佳用途,但即使这样也会导致以下崩溃

11-17 20:23:17.168 22976 23157 E androidRuntime: java.lang.AssertionError: Built-in class kotlin.Any is not found
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns$3.invoke(KotlinBuiltIns.java:113)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns$3.invoke(KotlinBuiltIns.java:108)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$MapBasedMemoizedFunction.invoke(LockBasedStorageManager.java:512)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$MapBasedMemoizedFunctionToNotNull.invoke(LockBasedStorageManager.java:587)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.getBuiltInClassByName(KotlinBuiltIns.java:362)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.getAny(KotlinBuiltIns.java:367)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.getAnyType(KotlinBuiltIns.java:642)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.getNullableAnyType(KotlinBuiltIns.java:647)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.getDefaultBound(KotlinBuiltIns.java:652)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.descriptors.impl.TypeParameterDescriptorImpl.createWithDefaultBound(TypeParameterDescriptorImpl.java:56)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.descriptors.NotFoundClasses$MockClassDescriptor.<init>(NotFoundClasses.kt:55)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.descriptors.NotFoundClasses$classes$1.invoke(NotFoundClasses.kt:44)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.descriptors.NotFoundClasses$classes$1.invoke(NotFoundClasses.kt:22)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$MapBasedMemoizedFunction.invoke(LockBasedStorageManager.java:512)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$MapBasedMemoizedFunctionToNotNull.invoke(LockBasedStorageManager.java:587)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.descriptors.NotFoundClasses.getClass(NotFoundClasses.kt:92)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.TypeDeserializer$typeConstructor$1.invoke(TypeDeserializer.kt:109)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.TypeDeserializer.typeConstructor(TypeDeserializer.kt:113)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.TypeDeserializer.simpleType(TypeDeserializer.kt:75)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.TypeDeserializer.type(TypeDeserializer.kt:63)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.MemberDeserializer.valueParameters(MemberDeserializer.kt:417)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.MemberDeserializer.loadConstructor(MemberDeserializer.kt:342)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.computePrimaryConstructor(DeserializedClassDescriptor.kt:122)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.access$computePrimaryConstructor(DeserializedClassDescriptor.kt:34)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor$primaryConstructor$1.invoke(DeserializedClassDescriptor.kt:65)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor$primaryConstructor$1.invoke(DeserializedClassDescriptor.kt:34)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:355)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.getUnsubstitutedPrimaryConstructor(DeserializedClassDescriptor.kt:126)
11-17 20:23:17.168 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.computeConstructors(DeserializedClassDescriptor.kt:129)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.access$computeConstructors(DeserializedClassDescriptor.kt:34)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor$constructors$1.invoke(DeserializedClassDescriptor.kt:66)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor$constructors$1.invoke(DeserializedClassDescriptor.kt:34)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedLazyValue.invoke(LockBasedStorageManager.java:355)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager$LockBasedNotNullLazyValue.invoke(LockBasedStorageManager.java:474)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.impl.serialization.deserialization.descriptors.DeserializedClassDescriptor.getConstructors(DeserializedClassDescriptor.kt:137)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.KClassImpl.getConstructorDescriptors(KClassImpl.kt:200)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:91)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.invoke(KClassImpl.kt:44)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:92)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:31)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.KClassImpl$Data.getConstructors(Unknown Source:7)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.internal.KClassImpl.getConstructors(KClassImpl.kt:235)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at kotlin.reflect.jvm.ReflectJvmMapping.getKotlinFunction(ReflectJvmMapping.kt:144)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector.findKotlinParameterName(KotlinNamesAnnotationIntrospector.kt:115)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector.findImplicitPropertyName(KotlinNamesAnnotationIntrospector.kt:31)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.findImplicitPropertyName(AnnotationIntrospectorPair.java:490)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addCreatorParam(POJOPropertiesCollector.java:485)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addCreators(POJOPropertiesCollector.java:465)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collectAll(POJOPropertiesCollector.java:313)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getPropertyMap(POJOPropertiesCollector.java:287)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getProperties(POJOPropertiesCollector.java:170)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription._properties(BasicBeanDescription.java:164)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription.findProperties(BasicBeanDescription.java:239)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._findCreatorsFromProperties(BasicDeserializerFactory.java:292)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:276)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:224)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:220)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:143)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:414)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
11-17 20:23:17.169 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:479)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4405)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4214)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3214)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3182)
11-17 20:23:17.172 22976 23157 E AndroidRuntime:    at 
...

build.gradle 中没有排除 Kotlin 相关文件。包含 std 库,因为禁用 Proguard 时没有问题

当前使用 Proguard 7.1.1,与 6.0.3 相同的问题。任何帮助表示赞赏

【问题讨论】:

【参考方案1】:

这不是最佳解决方案,但您可以通过逐步调查自己测试以下代码

-dontskipnonpubliclibraryclassmembers
    -dontshrink
    -dontoptimize
    -printmapping build/libs/output/obfuscation.map
    -keepattributes
    -adaptclassstrings
    -dontnote
    -dontwarn

# Keep Android classes
-keep class ** extends android.** 
    <fields>;
    <methods>;


# Keep serializable classes & fields
-keep class ** extends java.io.Serializable 
    <fields>;


# Keep - Applications. Keep all application classes, along with their 'main'
# methods.
-keepclasseswithmembers public class * 
    public static void main(java.lang.String[]);


# Also keep - Enumerations. Keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum  * 
    public static **[] values();
    public static ** valueOf(java.lang.String);



-assumenosideeffects class android.util.Log 
    public static *** d(...);
    public static *** e(...);
    public static *** w(...);

【讨论】:

这比我贴的更严格吧? 你知道每个原始类型都扩展类 Any ,所以你的问题是混淆这个类,下面的代码必须避免混淆 kotlin 类 -keep class kotlin.reflect.jvm.internal.** *; -keep class kotlin.Metadata *; 如果我的回答正确,请标记为答案 添加了这些规则,还是同样的问题 看起来 APK 根目录中的整个 kotlin 文件夹已被删除。有谁知道为什么会这样?我在这里发布了另一个问题:***.com/questions/70207298/…

以上是关于启用 Proguard 时未找到 kotlin.Any的主要内容,如果未能解决你的问题,请参考以下文章

启用proguard后Realm中的ClassNotFoundException

启用 proguard,我的构建版本应用程序不会再次安装

Android ProGuard +MultiDex 导致 ClassNotFoundException

启用 Proguard 时,Xamarin Android 应用程序因缺少 google play 服务类而崩溃

Proguard 启用 NoSuchMethodException

启用 PROGUARD - Xamarin.Android