问题
I've used Apache POI successfully on Java desktop app, and would like to use it on Android for reading & writing Excel files.
Here's my Github repo: https://github.com/anta40/StockChecker
Everytime I try to open an XLSX file, eventually the app crashes because of
org.apache.poi.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found
Here's the content of my build.gradle: apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.anta40.app.stockchecker"
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.github.SUPERCILEX.poi-android:poi:3.17'
implementation 'com.github.angads25:filepicker:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
How to solve this issue?
Adding this line on build.gradle:
implementation 'com.fasterxml:aalto-xml:1.1.0'
doesn't work. You'll get lots of error messages like these:
Duplicate class org.codehaus.stax2.ri.typed.ValueDecoderFactory$IntDecoder found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1) Duplicate class org.codehaus.stax2.ri.typed.ValueDecoderFactory$IntegerDecoder found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1) Duplicate class org.codehaus.stax2.ri.typed.ValueDecoderFactory$LongArrayDecoder found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1) Duplicate class org.codehaus.stax2.ri.typed.ValueDecoderFactory$LongDecoder found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1) Duplicate class org.codehaus.stax2.ri.typed.ValueDecoderFactory$QNameDecoder found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1) Duplicate class org.codehaus.stax2.ri.typed.ValueEncoderFactory found in modules poi-3.17.jar (com.github.SUPERCILEX.poi-android:poi:3.17) and stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1)
回答1:
exculde this stax2-api-4.1.jar in your dependency and rebuild the project
Edit 1- I know i am a bit late with the edit, but solved it just now, just move your System.properties in a static block where you initialize your app. This will solve your problem.
public class YourActivity extends AppCompatActivity {
//scope......
static {
System.setProperty(
"org.apache.poi.javax.xml.stream.XMLInputFactory",
"com.fasterxml.aalto.stax.InputFactoryImpl"
);
System.setProperty(
"org.apache.poi.javax.xml.stream.XMLOutputFactory",
"com.fasterxml.aalto.stax.OutputFactoryImpl"
);
System.setProperty(
"org.apache.poi.javax.xml.stream.XMLEventFactory",
"com.fasterxml.aalto.stax.EventFactoryImpl"
);
}
来源:https://stackoverflow.com/questions/56088744/provider-com-bea-xml-stream-eventfactory-not-found-when-loading-xlsx-file