1

I want to use Apache POI to read and write excel files (both xlsx and xls) in Android. But I cannot resolve dependency issues that I currently have.

If I have only these two dependencies in my gradle file

compile files('libs/poi-3.12-20150511.jar')
compile files('libs/poi-ooxml-3.12-20150511.jar')

Then I get the following error:

   java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/xmlbeans/XmlOptions;
            at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:59)
            at com.gargzdai.spreadsheet.MainActivity.readData(MainActivity.java:131)
            at com.gargzdai.spreadsheet.MainActivity.prepareForReadingData(MainActivity.java:109)
            at com.gargzdai.spreadsheet.MainActivity.onCreate(MainActivity.java:68)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

This is the code I'm using to read excel files:

    if (isXlsxFile)
        myWorkbook = new XSSFWorkbook(OPCPackage.open(file));
    else
        myWorkbook = new HSSFWorkbook(fileStream);
    Sheet mySheet = myWorkbook.getSheetAt(0);
    Iterator rowIterator = mySheet.rowIterator();

It seems that I should add xmlbeans library. After I add this dependency to my gradle file:

compile files('libs/xmlbeans-2.6.0.jar')

After I add this dependency I get the following error during gradle build:

Error:Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_40\bin\java.exe'' finished with non-zero exit value 1

Any ideas on how should I approach this problem?

Mantas
  • 1,366
  • 3
  • 13
  • 18
  • One thing to look at is that Java 8 isn't supported by Android yet: https://developer.android.com/sdk/index.html#Requirements http://stackoverflow.com/questions/23318109/is-it-possible-to-use-java-8-for-android-development – Morrison Chang Jun 01 '15 at 17:14
  • Thanks for noticing this but it didn't change anything. – Mantas Jun 01 '15 at 17:48
  • is there different version of poi dependency for android ? i just want to know.I am using org.apache.poi poi-ooxml 3.9 for java projects and it works – Avyaan Jun 01 '15 at 18:13
  • Yes, there is. The newest version in 3.12 and you can get it from here: https://poi.apache.org/download.html – Mantas Jun 02 '15 at 12:07

4 Answers4

5

This worked for me: dependencies { implementation 'org.apache.poi:poi:3.12' implementation 'org.apache.poi:poi-ooxml:3.12' implementation 'com.fasterxml:aalto-xml:1.0.0'}

ad3bay0
  • 123
  • 1
  • 9
2

I faced the same problem. org.apache.poi:poi-ooxml is referencing at the end stax:stax-api, which contains core packages javax.xml.*. The compiler (or predexer or however it is called in android studio) aborts the compiling with a long warning. There are three ways to solve this:

  1. Use another library than poi, if there is any that also supports xlsx
  2. Recompile the dependencies. I started recompiling the stax-api. You need to change the package name of the classes wit a tool like jarjar, which is also mentioned in the error output of android studio. The problem: you will also have to recompile the xmlbeans library that references and uses the stax-api. At this point I gave up.
  3. Set the --core-library switch during compilation. This is not recommended as one can see in androids error message. Nevertheless, here is a discussion on how this would work. Doing this, you should also recompile stax-api and drop all classes that are defined in android.jar (javax.xml.XMLConstants, javax.xml.namespace.*)

Hope that helps!

Community
  • 1
  • 1
Clerenz
  • 821
  • 8
  • 23
0

Just add these two dependencies in your build.gradle module file

implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
implementation 'javax.xml.stream:stax-api:1.0'
Aman Pasricha
  • 61
  • 1
  • 3
0

You need to remember to sync the gradle file after saving it. That solved the issue for me. The sync button appeared at the top of the gradle file after a change was made.

enter image description here

Doh09
  • 2,324
  • 1
  • 16
  • 31