合成 lambda 时的警告 - 从 ThreeTenABP 迁移到 java.time

3

最近,我们启用了Java8 desugaring技术,以将https://github.com/JakeWharton/ThreeTenABP移植到java.time


build.gradle

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 29
    multiDexEnabled true

compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true

    targetCompatibility 1.8
    sourceCompatibility 1.8
}

dependencies {    
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'

没有编译问题。但是,当我们尝试将应用程序运行到模拟器 API 18 上时,会收到以下警告。

> Transform artifact desugar_jdk_libs_configuration-1.0.10.jar (com.android.tools:desugar_jdk_libs_configuration:1.0.10) with L8DexDesugarLibTransform
Warning: Type `j$.util.OptionalDouble` was not found, it is required for default or static interface methods desugaring of `j$.util.OptionalDouble j$.util.OptionalConversions.convert(java.util.OptionalDouble)`
Warning: Type `j$.util.OptionalLong` was not found, it is required for default or static interface methods desugaring of `j$.util.OptionalLong j$.util.OptionalConversions.convert(java.util.OptionalLong)`
Warning: Type `j$.util.OptionalInt` was not found, it is required for default or static interface methods desugaring of `j$.util.OptionalInt j$.util.OptionalConversions.convert(java.util.OptionalInt)`
Warning: Type `j$.time.ZonedDateTime` was not found, it is required for default or static interface methods desugaring of `j$.time.ZonedDateTime j$.time.TimeConversions.convert(java.time.ZonedDateTime)`
Warning: Type `j$.time.ZoneId` was not found, it is required for default or static interface methods desugaring of `j$.time.ZoneId j$.time.TimeConversions.convert(java.time.ZoneId)`
Warning: Type `j$.time.MonthDay` was not found, it is required for default or static interface methods desugaring of `j$.time.MonthDay j$.time.TimeConversions.convert(java.time.MonthDay)`
Warning: Type `j$.time.Instant` was not found, it is required for default or static interface methods desugaring of `j$.time.Instant j$.time.TimeConversions.convert(java.time.Instant)`
Warning: Type `j$.time.LocalDate` was not found, it is required for default or static interface methods desugaring of `j$.time.LocalDate j$.time.TimeConversions.convert(java.time.LocalDate)`
Warning: Type `j$.time.Duration` was not found, it is required for default or static interface methods desugaring of `j$.time.Duration j$.time.TimeConversions.convert(java.time.Duration)`
Warning: Type `j$.util.DesugarGregorianCalendar` was not found, it is required for default or static interface methods desugaring of `j$.time.ZonedDateTime j$.$r8$backportedMethods$utility$GregorianCalendar$toZonedDateTime$dispatchHolder.toZonedDateTime(java.util.GregorianCalendar)`
Warning: Type `j$.util.DesugarDate` was not found, it is required for default or static interface methods desugaring of `j$.time.Instant j$.$r8$backportedMethods$utility$Date$toInstant$dispatchHolder.toInstant(java.util.Date)`

> Transform artifact desugar_jdk_libs-1.0.10.jar (com.android.tools:desugar_jdk_libs:1.0.10) with L8DexDesugarLibTransform
Warning in synthesized for lambda desugaring:
  Type `j$.$r8$wrapper$java$util$function$ToDoubleFunction$-V-WRP` was not found, it is required for default or static interface methods desugaring of `java.util.Comparator java.time.chrono.-$$Lambda$AbstractChronology$5b0W7uLeaWkn0HLPDKwPXzJ7HPo.thenComparingDouble($-vivified-$.java.util.function.ToDoubleFunction)`
Warning in synthesized for lambda desugaring:
  Type `j$.$r8$wrapper$java$util$function$Function$-V-WRP` was not found, it is required for default or static interface methods desugaring of `java.util.Comparator java.time.chrono.-$$Lambda$AbstractChronology$5b0W7uLeaWkn0HLPDKwPXzJ7HPo.thenComparing($-vivified-$.java.util.function.Function)`
Warning in synthesized for lambda desugaring:
  Type `j$.$r8$wrapper$java$util$function$ToIntFunction$-V-WRP` was not found, it is required for default or static interface methods desugaring of `java.util.Comparator java.time.chrono.-$$Lambda$AbstractChronology$5b0W7uLeaWkn0HLPDKwPXzJ7HPo.thenComparingInt($-vivified-$.java.util.function.ToIntFunction)`
Warning in synthesized for lambda desugaring:
  Type `j$.$r8$wrapper$java$util$function$ToLongFunction$-V-WRP` was not found, it is required for default or static interface methods desugaring of `java.util.Comparator java.time.chrono.-$$Lambda$AbstractChronology$5b0W7uLeaWkn0HLPDKwPXzJ7HPo.thenComparingLong($-vivified-$.java.util.function.ToLongFunction)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/stream/Collectors$Partition$1.class:
  Type `j$.$r8$wrapper$java$util$Spliterator$-WRP` was not found, it is required for default or static interface methods desugaring of `$-vivified-$.java.util.Spliterator java.util.stream.Collectors$Partition$1.spliterator()`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/stream/Collectors$Partition$1.class:
  Type `j$.$r8$wrapper$java$util$function$Consumer$-V-WRP` was not found, it is required for default or static interface methods desugaring of `void java.util.stream.Collectors$Partition$1.forEach($-vivified-$.java.util.function.Consumer)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/stream/Collectors$Partition$1.class:
  Type `j$.$r8$wrapper$java$util$stream$Stream$-WRP` was not found, it is required for default or static interface methods desugaring of `$-vivified-$.java.util.stream.Stream java.util.stream.Collectors$Partition$1.parallelStream()`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/stream/Collectors$Partition$1.class:
  Type `j$.$r8$wrapper$java$util$function$Predicate$-V-WRP` was not found, it is required for default or static interface methods desugaring of `boolean java.util.stream.Collectors$Partition$1.removeIf($-vivified-$.java.util.function.Predicate)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/stream/Collectors$Partition$1.class:
  Type `j$.$r8$wrapper$java$util$function$IntFunction$-V-WRP` was not found, it is required for default or static interface methods desugaring of `java.lang.Object[] java.util.stream.Collectors$Partition$1.toArray($-vivified-$.java.util.function.IntFunction)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/DesugarCollections$SynchronizedMap.class:
  Type `j$.$r8$wrapper$java$util$function$BiFunction$-V-WRP` was not found, it is required for default or static interface methods desugaring of `void java.util.DesugarCollections$SynchronizedMap.replaceAll($-vivified-$.java.util.function.BiFunction)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/DesugarCollections$SynchronizedMap.class:
  Type `j$.$r8$wrapper$java$util$function$BiConsumer$-V-WRP` was not found, it is required for default or static interface methods desugaring of `void java.util.DesugarCollections$SynchronizedMap.forEach($-vivified-$.java.util.function.BiConsumer)`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/concurrent/ThreadLocalRandom.class:
  Type `j$.$r8$wrapper$java$util$stream$IntStream$-WRP` was not found, it is required for default or static interface methods desugaring of `$-vivified-$.java.util.stream.IntStream java.util.concurrent.ThreadLocalRandom.ints()`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/concurrent/ThreadLocalRandom.class:
  Type `j$.$r8$wrapper$java$util$stream$LongStream$-WRP` was not found, it is required for default or static interface methods desugaring of `$-vivified-$.java.util.stream.LongStream java.util.concurrent.ThreadLocalRandom.longs()`
Warning in C:\Users\yccheok\.gradle\caches\modules-2\files-2.1\com.android.tools\desugar_jdk_libs\1.0.10\61bd346a9d026c92028eb8c8c29e09368b22633c\desugar_jdk_libs-1.0.10.jar:java/util/concurrent/ThreadLocalRandom.class:
  Type `j$.$r8$wrapper$java$util$stream$DoubleStream$-WRP` was not found, it is required for default or static interface methods desugaring of `$-vivified-$.java.util.stream.DoubleStream java.util.concurrent.ThreadLocalRandom.doubles(long)`
Warning: Type `j$.util.OptionalConversions` was not found, it is required for default or static interface methods desugaring of `java.util.OptionalLong j$.$r8$wrapper$java$util$stream$LongStream$-WRP.reduce(java.util.function.LongBinaryOperator)`
Warning: Type `j$.util.LongSummaryStatisticsConversions` was not found, it is required for default or static interface methods desugaring of `java.util.LongSummaryStatistics j$.$r8$wrapper$java$util$stream$LongStream$-WRP.summaryStatistics()`
Warning: Type `j$.util.DoubleSummaryStatisticsConversions` was not found, it is required for default or static interface methods desugaring of `java.util.DoubleSummaryStatistics j$.$r8$wrapper$java$util$stream$DoubleStream$-WRP.summaryStatistics()`

> Task :app:processDebugResources

> Transform artifact desugar_jdk_libs-1.0.10.jar (com.android.tools:desugar_jdk_libs:1.0.10) with L8DexDesugarLibTransform
Warning: Type `j$.util.IntSummaryStatisticsConversions` was not found, it is required for default or static interface methods desugaring of `java.util.IntSummaryStatistics j$.$r8$wrapper$java$util$stream$IntStream$-WRP.summaryStatistics()`

然后我们会得到以下运行时错误。

10-29 15:48:43.167 3959-3959/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: androidx.arch.core.internal.SafeIterableMap
        at androidx.lifecycle.LiveData.<init>(LiveData.java:66)
        at androidx.lifecycle.MutableLiveData.<init>(MutableLiveData.java:40)
        at com.yocto.wenote.SingleLiveEvent.<init>(SingleLiveEvent.java:38)
        at com.yocto.wenote.WeNoteApplication.<init>(WeNoteApplication.java:94)
        at java.lang.Class.newInstanceImpl(Native Method)

WeNoteApplication.java:94 行是

private final SingleLiveEvent<Boolean> onResumePasswordPromptSingleLiveEvent = new SingleLiveEvent<>();

我觉得这很混乱,并且不太有意义。因为我们不确定Java8的解糖如何影响从MutableLiveData继承的SingleLiveEvent

你有什么想法可以消除这个问题吗?

1个回答

1

Java 8解糖需要启用MultiDex。从API 21开始,默认支持MultiDex,无需使用MultiDex支持库。

为了正确支持API < 21上的MultiDex,您需要更新您的Application类:

override fun attachBaseContext(base: Context?) {
    super.attachBaseContext(base)
    MultiDex.install(this)
}

如果不这样做,那些不在第一个dex文件中的类将无法在这些设备上找到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接