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.
问题原因
-
这是 Android 项目中使用 Apache POI 5.2.3 时遇到的兼容性问题
-
Apache POI 5.x 依赖了 log4j(日志框架),Log4j 使用了
java.lang.invoke.MethodHandle
API,这些 API 在 Android 7.1.1(API 级别 25)及以下版本中不存在 -
java.lang.invoke.MethodHandle
API 需要在 Android 8.0(API 级别 26)及以上版本中使用
处理策略
- 升级 minSdk 版本
android {
defaultConfig {
minSdk 26
}
}
- 或者,降级 Apache POI 版本
dependencies {
implementation 'org.apache.poi:poi:3.16'
implementation 'org.apache.poi:poi-ooxml:3.16'
}