📜 ⬆️ ⬇️

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

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

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

* 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 causes the build to fail 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 have to repack.
This defect is already a hundred years old at lunch as issues.apache.org/jira/browse/XMLBEANS-499 has been registered, but things are still there.

To complete the 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 it’s not possible to carelessly add POI depending on the project
')
All the above files are available in POI downloads archive.apache.org/dist/poi/release/bin

To make it all you need:


If you use gradle, you will get a config similar to the following:
 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' } 


Everything! 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 jars, as was done last time. Please love, experiment and share the results.
If you wish, you can also be puzzled to make a javax-> aavax hack, but this time I thought it was over.

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 this build began to fall on preDex because of javax in stax-api. I did not manage to find a quick way to add --core-library to preDex and this was the end of my patience. The most sensible it seems to me is to simply add all the necessary jars, along with the repacked xmlbeans in the project libs.

To conclude:
I made two jara containing all necessary and not containing duplicates in xmlbeans, files can be downloaded in this repository:
Use github.com/andruhon/android5xlsx for health reasons, but at your own risk.

Source: https://habr.com/ru/post/267381/


All Articles