Back to Home

Read XLSX on Android 5 (ART) with Apache POI XSSF

android · apache poi · openxml

Read XLSX on Android 5 (ART) with Apache POI XSSF

    android eating poiSome time ago I wrote how with the help of pagan dances and other paranormal activities I made Apache POI XSSF work on Android 4. Everything becomes much easier with Android Build Tools (21+) and Android 5 (ART).

    Now it’s enough to build a project with multi-dex support and everything will work * on devices with ART. I believe this is due to Ahead-of-time (AOT) compilation on the device and multi-dex, now, as such, is needed only as an intermediate stage.

    * Unfortunately, not everything is so smooth. The project dependencies have xmlbeans-2.6.0.jar, which is packed with an error and contains duplicate classes. This leads to a build failure on the packageAllDebugClassesForMultiDex task, with the following error:
    Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
    > java.util.zip.ZipException: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class
    

    XMLBeans will have to be repacked.
    This defect is already a hundred years old at dinner as issues.apache.org/jira/browse/XMLBEANS-499 is registered , but it is still there.

    For complete work, we need the following JAR files:
    poi-3.12-20150511.jar
    poi-ooxml-3.12-20150511.jar
    poi-ooxml-schemas-3.12-20150511.jar
    stax-1.2.0.jar
    stax-api-1.0.1 .jar
    xmlbeans-2.6.0.jar - because of this file, you won’t be able to easily add POIs depending on the project.

    All of the above files are available in POI downloads archive.apache.org/dist/poi/release/bin

    For everything to work, you need to:
    • add com.android.support:multidex depending on the project.
    • Enable multiDex in defaultConfig
    • for the task com.android.build.gradle.tasks.Dex add the parameter --core-library to avoid errors due to classes with namespace javax.


    If to use gradle, then we will receive a config about the following contents:
    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"
        defaultConfig {
            // ... другие стандартные настройки ...
            minSdkVersion 21
            targetSdkVersion 21        
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        //исключим текстовые файлы:
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
        }
        project.tasks.withType(com.android.build.gradle.tasks.Dex) {
            additionalParameters=['--core-library'] //javax namespace fix
        }
    }
    dependencies {
        // ... остальные зависимости проекта ...
        compile 'com.android.support:multidex:1.0.1'
    }
    


    All! Now we have, presumably, a fully functional POI on Android, which, again, presumably, can read all other openxml formats, since we did not have to trim the contents of the jars, as was done last time. Please love, experiment and share the results.
    If you wish, you can also be puzzled by making javax-> aavax hack, but this time I found this superfluous.

    If you really want the dependencies to stretch themselves, then you can write a routine for multidex.JarMergingTask, which would repack jar xmlbeans, and I even wrote such a routine. However, after that, the build began to crash on preDex due to javax in stax-api. I was not able to find a quick way to add --core-library to preDex and that was the end of my patience. It seems most reasonable to me to just add all the necessary jars, along with the repackaged xmlbeans to the project libs.

    In conclusion:
    I made two jaras that contain everything you need and do not contain duplicates in xmlbeans, files can be downloaded from this repository: use
    github.com/andruhon/android5xlsx on health, but at your own risk.

    Read Next