D8:未找到“ sun.misc.Unsafe”类型

时间:2018-12-07 07:33:59

标签: android android-guava android-d8

在我的android项目中启用D8后,我开始看到以下警告:

/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `void com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper.<clinit>()`
/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `sun.misc.Unsafe com.google.common.cache.Striped64.getUnsafe()`
/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `sun.misc.Unsafe com.google.common.hash.LittleEndianByteArray$UnsafeByteArray.getUnsafe()`
/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `void com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper.<clinit>()`
/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `sun.misc.Unsafe com.google.common.cache.Striped64.getUnsafe()`
/Users/yashasvi/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/27.0.1-android/b7e1c37f66ef193796ccd7ea6e80c2b05426182d/guava-27.0.1-android.jar: D8: Type `sun.misc.Unsafe` was not found, it is required for default or static interface methods desugaring of `sun.misc.Unsafe com.google.common.hash.LittleEndianByteArray$UnsafeByteArray.getUnsafe()`

项目构建成功,但是在运行时,我会看到这些错误日志,因此,应用程序功能会受到影响。

 java.lang.AbstractMethodError: abstract method "java.lang.Object com.google.common.base.e.a(java.lang.Object)"
        at com.google.common.e.a.i$c.a(SourceFile:1464)
        at com.google.common.e.a.i$c.a(SourceFile:1453)
        at com.google.common.e.a.i$a.run(SourceFile:1408)
        at com.google.common.e.a.l$a.execute(SourceFile:456)
        at com.google.common.e.a.i$f.a(SourceFile:153)
        at com.google.common.e.a.i.a(SourceFile:1234)

搜索了一会儿之后,我找不到任何有用的东西。请帮忙。

是的,如果禁用了D8并且其他所有内容都保持不变,则在运行时不会遇到此错误。

2 个答案:

答案 0 :(得分:1)

您需要保留sun.misc.Unsafe

-keep class sun.misc.Unsafe { *; }
-dontnote sun.misc.Unsafe

或者,带有includedescriptorclasses的规则应该动态地保持它:

-keep,includedescriptorclasses class com.google.common.**

通常:

  • 添加开关-verbose对于编写ProGuard配置规则很有帮助。

  • 添加开关-dontoptimize可用于禁用所有优化以进行测试。

刚刚发现this

  

R8现在了解META-INF / proguard中的proguard规格。

,但除非已添加,否则必须添加自定义规则-而不是使用使用者规则。

答案 1 :(得分:0)

在项目保护文件中。请添加:

class Products extends Component {
    constructor(props) {
        super(props);
        const { products } = this.props;
        this.state = {
            products, 
            filteredProducts: products,
        };
    }
    ...

这似乎是一种错误。但是有时候,当您使用D8时,它往往会重命名类名或将其完全删除。