Android 使用 Apache POI 问题:MethodHandle.invoke and MethodHandle.invokeExact are only supported...

dependencies {
    implementation 'org.apache.poi:poi:5.2.3'
    implementation 'org.apache.poi:poi-ooxml:5.2.3'
}
  • 在 Android 项目中,使用 Apache POI 时,报如下错误
MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26): 
Lorg/apache/logging/log4j/util/ServiceLoaderUtil;callServiceLoader(Ljava/lang/invoke/MethodHandles$Lookup;
Ljava/lang/Class;Ljava/lang/ClassLoader;Z)Ljava/lang/Iterable;
Execution failed for task ':bluetoothdevelopplus:mergeExtDexDebug'.
> Could not resolve all files for configuration ':bluetoothdevelopplus:debugRuntimeClasspath'.
   > Failed to transform poi-5.2.3.jar (org.apache.poi:poi:5.2.3) to match attributes {artifactType=android-dex, dexing-component-attributes=ComponentSpecificParameters(minSdkVersion=24, debuggable=true, enableCoreLibraryDesugaring=false, enableGlobalSynthetics=false, enableApiModeling=false, dependenciesClassesAreInstrumented=false, asmTransformComponent=null, useJacocoTransformInstrumentation=false, enableDesugaring=true, needsClasspath=false, useFullClasspath=false, componentIfUsingFullClasspath=null), org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: D:\Gradle\caches\modules-2\files-2.1\org.apache.poi\poi\5.2.3\2fb22ae74ad5aea6af1a9c64b9542f2ccf348604\poi-5.2.3.jar.
         > Error while dexing.
           Increase the minSdkVersion to 26 or above.
问题原因
  1. 这是 Android 项目中使用 Apache POI 5.2.3 时遇到的兼容性问题

  2. Apache POI 5.x 依赖了 log4j(日志框架),Log4j 使用了 java.lang.invoke.MethodHandle API,这些 API 在 Android 7.1.1(API 级别 25)及以下版本中不存在

  3. java.lang.invoke.MethodHandle API 需要在 Android 8.0(API 级别 26)及以上版本中使用

处理策略
  1. 升级 minSdk 版本
android {
    defaultConfig {
        minSdk 26
    }
}
  1. 或者,降级 Apache POI 版本
dependencies {
    implementation 'org.apache.poi:poi:3.16'
    implementation 'org.apache.poi:poi-ooxml:3.16'
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值