0

I'm currently developing an Android application, which is required to read/write data from/to an Excel spreadsheet (.xls or .xlsx).

In order to integrate Android <--> Excel I am using this poi library, mainly because I was having some issues with correctly configuring the dependencies to the original Apache POI.

Note: On an action performed (button click), a workbook object is instantiated as such this.workbook = new XSSFWorkbook(super.file);

Following is the content of my build.gradle (module) file:

apply plugin: 'com.android.application
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.ricardomiranda.expenses"
        minSdkVersion 24
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // Workaround - "Program type already present"
    dexOptions {
        preDexLibraries = false
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:27.1.1'

    // Workaround - "com.bea.xml.stream.EventFactory not found"
    implementation('com.fasterxml:aalto-xml:1.0.0'){
        // Workaround - "Program type already present"
        exclude module: 'stax'
        exclude module: 'stax-api'
        exclude module: 'xpp3'
        exclude group: 'com.android.support'
        exclude module: 'support-v7'
        exclude module: 'fasterxml'
    }

    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'
    implementation files('libs/poishadow-all.jar')
}`

As one can observe, the previous code snippet has some workaround inclusions (properly highlighted by the comments) in order to try to solve the problems I'm facing, yet to no avail.

The issues:

Consider the previous build.gradle code snippet without the workarounds

  1. The build is successfully executed and the application is launched. On a button click, the aforementioned line is executed: this.workbook = new XSSFWorkbook(super.file); as well as some other classes and methods inherited from the poi library - this button is trying to write data into an Excel spreadhseet. However, the expected behaviour is not applied, as the execution is aborted with a "fatal exception" as such:

`

2019-01-31 18:35:14.377 20682-20682/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ricardomiranda.expenses, PID: 20682
    org.apache.poi.javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found
        at org.apache.poi.javax.xml.stream.FactoryFinder.newInstance(FactoryFinder.java:72)
        at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:178)
        at org.apache.poi.javax.xml.stream.FactoryFinder.find(FactoryFinder.java:92)
        at org.apache.poi.javax.xml.stream.XMLEventFactory.newInstance(XMLEventFactory.java:30)
        at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
        at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
        at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:130)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:295)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:201)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:323)
        at com.ricardomiranda.expenses.ExcelManagement.initializeWorkbook(ExcelManagement.java:141)
        at com.ricardomiranda.expenses.ExcelManagement.<init>(ExcelManagement.java:51)
        at com.ricardomiranda.expenses.AddExpense$1.onClick(AddExpense.java:44)
        at android.view.View.performClick(View.java:6291)
        at android.view.View$PerformClick.run(View.java:24931)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

In order to remediate the issue and based on this comment, I added the following dependency to my build implementation('com.fasterxml:aalto-xml:1.0.0') and set the required system properties (also described in the Getting started section of the library's README documentation).

  1. Relaunching the application after applying the former changes results in the next error, this time during the project's build: Error: Program type already present: com.fasterxml.aalto.ValidationException

As far as I can tell this seems to be a duplicated dependency problem, but I can't resolve gradlew app:dependencies (the output is way too extensive) and no configurations are outputted by analogous commands.

0 Answers0