2

I have an extremely weird error, using apache poi and I think it has something to do with the ooxml-schemas--.jar file. I am basically trying to read an xlsx file into internal storage, and it's just not happening.

DETAILS: I'm using Apache Poi 3.9 to read from the res.raw folder in android it's an xlsx file that is coping itself to internal storage in android, using Outputstream. And it just in't happening. Here's the error log:

trouble writing output: Too many methods: 66024; max is 65536. By package:
    13 java.lang
     1 java.lang.reflect
     5 java.util
     1 javax.xml.namespace
    66 org.apache.xmlbeans
    19 org.apache.xmlbeans.impl.values
     1 org.apache.xmlbeans.impl.xb.xmlschema
  2500 org.openxmlformats.schemas.drawingml.x2006.chart
  1430 org.openxmlformats.schemas.drawingml.x2006.chart.impl
  8767 org.openxmlformats.schemas.drawingml.x2006.main
  5258 org.openxmlformats.schemas.drawingml.x2006.main.impl
    86 org.openxmlformats.schemas.drawingml.x2006.picture
    33 org.openxmlformats.schemas.drawingml.x2006.picture.impl
   745 org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing
   417 org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.impl
   230 org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing
   164 org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.impl
   298 org.openxmlformats.schemas.officeDocument.x2006.customProperties
   256 org.openxmlformats.schemas.officeDocument.x2006.customProperties.impl
   617 org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes
   596 org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.impl
   285 org.openxmlformats.schemas.officeDocument.x2006.extendedProperties
   196 org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.impl
    23 org.openxmlformats.schemas.officeDocument.x2006.math
    24 org.openxmlformats.schemas.officeDocument.x2006.relationships
     2 org.openxmlformats.schemas.officeDocument.x2006.relationships.impl
  2076 org.openxmlformats.schemas.presentationml.x2006.main
  1224 org.openxmlformats.schemas.presentationml.x2006.main.impl
     1 org.openxmlformats.schemas.schemaLibrary.x2006.main
  7271 org.openxmlformats.schemas.spreadsheetml.x2006.main
  4556 org.openxmlformats.schemas.spreadsheetml.x2006.main.impl
 11448 org.openxmlformats.schemas.wordprocessingml.x2006.main
  9217 org.openxmlformats.schemas.wordprocessingml.x2006.main.impl
     4 schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707
  1170 schemasMicrosoftComOfficeExcel
  1223 schemasMicrosoftComOfficeExcel.impl
   285 schemasMicrosoftComOfficeOffice
   124 schemasMicrosoftComOfficeOffice.impl
     2 schemasMicrosoftComOfficePowerpoint
     3 schemasMicrosoftComOfficeWord
  2858 schemasMicrosoftComVml
  2529 schemasMicrosoftComVml.impl
[2013-08-18 14:21:31 - gss4] Conversion to Dalvik format failed with error 2

What the hell is happening?

Here's the function I am using:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readAndWrite();

    }


    public void readAndWrite() {
          try
            {
              // Read from original Excel file. //Get from RawResources in Android
              InputStream inputStream = getResources().openRawResource(R.raw.ems_data);
              OutputStream fos = openFileOutput("ems_data.xlsx", Context.MODE_PRIVATE);
              Workbook workbook = WorkbookFactory.create(inputStream );

              // Get the first sheet.
              Sheet sheet = workbook.getSheetAt(0);

              // Set value of the first cell.
              Row row = sheet.getRow(0);
              Cell cell = row.getCell(0);
              cell.setCellValue("Xuan");

              // Write newly modified workbook to a file. //Copy to Internal Storage in Android
            //  FileOutputStream fileOut = new FileOutputStream("/sdcard/ems_data3new.xlsx");
              workbook.write(fos);
              fos.close();
            }
            catch(FileNotFoundException e)
            {
              System.out.println(e);
            }
            catch(IOException e)
            {
              System.out.println(e);
            }
            catch(InvalidFormatException e)
            {
              System.out.println(e);
            }

      }
Ahmed Zafar
  • 665
  • 2
  • 10
  • 24
  • I suggest you read the other questions about the same error message. – Dan Hulme Aug 18 '13 at 09:48
  • I have. None of them talk about on how to fix this. And [this](https://groups.google.com/forum/?fromgroups=#!topic/android-platform/sR6I2ldCxwU) is a talk by google engineers thats just a little too advanced for me. – Ahmed Zafar Aug 18 '13 at 09:49
  • Okay I can't find anything to fix this... – Ahmed Zafar Aug 18 '13 at 09:58
  • @AhmedZafar - It is clear from the output that you are NOT doing what you are saying you are doing. The output is clearly something to do with converting something to Davlik format. Please state clearly and precisely what you are actually doing, and how you are doing it ... – Stephen C Aug 18 '13 at 11:44
  • @StephenC- I've modified the question to fit the code I'm using. Yes I think the dalvik conversion messes it up since the function was working in java (evne though obviously it wasn't retrieving the file from raw or writing it to internal, it was doing that on the harddisk). I've written what I'm doing and it returns too many methods. I don't see why you're saying I'm not saying what I'm doing! – Ahmed Zafar Aug 18 '13 at 11:48
  • *"I don't see why you're saying I'm not saying what I'm doing!"* - Because you didn't say that you were doing a bytecode to Davlik conversion. That's what you were **doing** ... duh! What you told us is what the application tries to do ... which is irrelevant to the problem you are asking about. – Stephen C Aug 18 '13 at 11:50
  • Didn't know that was relevant... By bytecode I'm assuming you mean sort-of java. Here's something I found but I still need all the jars I'm using. I think it's the methods in the jars that's messing it up they're too many. [see here](http://stackoverflow.com/questions/15200286/android-error-conversion-to-dalvik-format-failed-with-error-2). Any way around this?? – Ahmed Zafar Aug 18 '13 at 11:53
  • @stephenc- I'm not doing a bytecode to dalvik conversion since I took the function and rewrote it in my main activity! So that isn't relevant. This function should work in android as well but is not. – Ahmed Zafar Aug 18 '13 at 11:59

1 Answers1

2

Your problem is caused by trying to put too much code into an Android app. According to this Q&A - Android: my application is too large and gives "Unable to execute dex: method ID not in [0, 0xffff]: 65536"? - the solution is to refactor your app so that some of the code is in the form of plugins.

Another approach is to try to eliminate dead code (i.e. library classes and methods that your app doesn't need). This Q&A - Android Error "Conversion to Dalvik format failed with error 2"? - suggests using Proguard to do this.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    My entire code right now is just that one function. The rest are just jar files in the libs folder. What am I supposed to do about them? – Ahmed Zafar Aug 18 '13 at 12:00
  • Read the linked Questions. – Stephen C Aug 18 '13 at 12:02
  • Thanks. I've read them and the only conclusion I've reached is to remove some libraries from my libs. But I can't do that since I need them. I can't 'split' the dex files either since I don't know how to... – Ahmed Zafar Aug 18 '13 at 12:04
  • 2
    You were right good Sir. Here's some good reading on the issue. Apache can handle HSSF(xls) fine on Android but too many methods result when handling XSSF (xlsx). Which is a pity. One potato for you ! :) [http://mail-archives.apache.org/mod_mbox/poi-dev/201110.mbox/%3CCA+JOeWNWinmNmEtHs5VK+KEc_6BzAG_=LfpdXqsDsnjJKR2X7Q@mail.gmail.com%3E](http://mail-archives.apache.org/mod_mbox/poi-dev/201110.mbox/%3CCA+JOeWNWinmNmEtHs5VK+KEc_6BzAG_=LfpdXqsDsnjJKR2X7Q@mail.gmail.com%3E) – Ahmed Zafar Aug 18 '13 at 15:31