Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to port Apache POI to Android

I'm looking for a lightweight version of poi-3.8.jar to use it in an Android (private) app. I don't seem to be able to fit the whole 1.7Mb jar in the APK for some reason (and it would be wrong to do so anyway) and since I'm only looking for the doc -> html and xls -> html functionality, I'm not quite sure I need the whole jar file.

I've spent a couple hours trying to figure out how to extract org.apache.poi.hwpf.converter.WordToHtmlExtracter.java in poi/hwpf/converter but it looks like it's using a lot of other stuff. Even if this doesn't really surprise me, I was thinking that maybe someone here would know which packages I can get rid of to make the jar smaller. I'll be glad to spend more time on it, unless someone here tells me it's a waste of time and that EVERYTHING in the sources is needed to convert doc to html files.

I don't need anything that displays anything, I just need the "simple" doc to html (and xls to html if possible) features. I don't need anything related to PDF, powerpoint, outlook or whatever.

I'll be glad to share whatever I find out

Cheers

like image 391
Johann Hilbold Avatar asked Dec 13 '11 17:12

Johann Hilbold


3 Answers

Well I was able to do most of what I was asking for here. That is importing the jar files. I had at least 2 kinds of problems: - not enough RAM on Eclipse which made dexing my classes crash most of the time (fixed by adjusting the Xmx and xms values in Eclipse.ini) - the 64k method limit for each DEX file made things complicated. I had to split all the required POI jars into several DEX files. (I did that by following the tutorial from the Android blog: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html )

The real answer to my question is: "yes you need everything in the jar". I made it work for the basic "non open xml" files. My app does the conversion to html quite well, and it's fast enough too.

On a side note, I was also trying to do the same thing with "open XML" files, and it's much more complicated. My little project doesn't do what it's supposed to do, I've got some weird exception when initializing the XMLBeans class. Here's my trace (sorry for the ugliness):

12-19 12:07:10.790: W/dalvikvm(13385): Exception
Ljava/lang/RuntimeException; thrown while initializing
Lorg/apache/xmlbeans/impl/regex/SchemaRegularExpression;
12-19 12:07:10.790: W/dalvikvm(13385): Exception
Ljava/lang/ExceptionInInitializerError; thrown while initializing
Lorg/apache/xmlbeans/impl/schema/BuiltinSchemaTypeSystem;
12-19 12:07:10.790: D/dalvikvm(13385): Method.invoke() on bad class
Lorg/apache/xmlbeans/impl/schema/BuiltinSchemaTypeSystem; failed
12-19 12:07:10.790: W/dalvikvm(13385): Exception
Ljava/lang/ExceptionInInitializerError; thrown while initializing
Lorg/apache/xmlbeans/XmlBeans;
12-19 12:07:10.790: W/System.err(13385):
java.lang.reflect.InvocationTargetException
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Method.invokeNative(Native Method)
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Method.invoke(Method.java:491)
12-19 12:07:10.790: W/System.err(13385):    at
t.fze.TestOfficeAndroidActivity.onCreate(TestOfficeAndroidActivity.java:55)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.ActivityThread.access$1500(ActivityThread.java:122)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
12-19 12:07:10.790: W/System.err(13385):    at
android.os.Handler.dispatchMessage(Handler.java:99)
12-19 12:07:10.790: W/System.err(13385):    at
android.os.Looper.loop(Looper.java:132)
12-19 12:07:10.790: W/System.err(13385):    at
android.app.ActivityThread.main(ActivityThread.java:4025)
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Method.invokeNative(Native Method)
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Method.invoke(Method.java:491)
12-19 12:07:10.790: W/System.err(13385):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-19 12:07:10.790: W/System.err(13385):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-19 12:07:10.790: W/System.err(13385):    at
dalvik.system.NativeStart.main(Native Method)
12-19 12:07:10.790: W/System.err(13385): Caused by:
org.apache.poi.POIXMLException:
java.lang.reflect.InvocationTargetException
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:414)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:174)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:63)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.ss.examples.html.ToHtml.create(ToHtml.java:139)
12-19 12:07:10.790: W/System.err(13385):    at
org.apache.poi.ss.examples.html.ToHtml.create(ToHtml.java:123)
12-19 12:07:10.790: W/System.err(13385):    ... 16 more
12-19 12:07:10.790: W/System.err(13385): Caused by:
java.lang.reflect.InvocationTargetException
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Constructor.constructNative(Native Method)
12-19 12:07:10.790: W/System.err(13385):    at
java.lang.reflect.Constructor.newInstance(Constructor.java:416)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
12-19 12:07:10.800: W/System.err(13385):    ... 22 more
12-19 12:07:10.800: W/System.err(13385): Caused by:
java.lang.ExceptionInInitializerError
12-19 12:07:10.800: W/System.err(13385):    at
org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(ThemeDocument.java:71)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:38)
12-19 12:07:10.800: W/System.err(13385):    ... 25 more
12-19 12:07:10.800: W/System.err(13385): Caused by:
java.lang.ExceptionInInitializerError
12-19 12:07:10.800: W/System.err(13385):    at
java.lang.reflect.Method.invokeNative(Native Method)
12-19 12:07:10.800: W/System.err(13385):    at
java.lang.reflect.Method.invoke(Method.java:491)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.XmlBeans.getNoType(XmlBeans.java:856)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:881)
12-19 12:07:10.800: W/System.err(13385):    ... 27 more
12-19 12:07:10.800: W/System.err(13385): Caused by:
java.lang.ExceptionInInitializerError
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem.fillInType(BuiltinSchemaTypeSystem.java:1025)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem.<clinit>(BuiltinSchemaTypeSystem.java:223)
12-19 12:07:10.800: W/System.err(13385):    ... 31 more
12-19 12:07:10.800: W/System.err(13385): Caused by:
java.lang.RuntimeException: Installation Problem???  Couldn't load
messages: Can't find resource for bundle
'org.apache.xmlbeans.impl.regex.message_fr_FR', key ''
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.RegexParser.setLocale(RegexParser.java:88)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.RegexParser.<init>(RegexParser.java:78)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.ParserForXMLSchema.<init>(ParserForXMLSchema.java:28)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:2996)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.RegularExpression.setPattern(RegularExpression.java:3009)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.RegularExpression.<init>(RegularExpression.java:2975)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<init>(SchemaRegularExpression.java:27)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<init>(SchemaRegularExpression.java:23)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.SchemaRegularExpression$1.<init>(SchemaRegularExpression.java:44)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.SchemaRegularExpression.buildKnownPatternMap(SchemaRegularExpression.java:43)
12-19 12:07:10.800: W/System.err(13385):    at
org.apache.xmlbeans.impl.regex.SchemaRegularExpression.<clinit>(SchemaRegularExpression.java:38)
12-19 12:07:10.800: W/System.err(13385):    ... 33 more
like image 92
Johann Hilbold Avatar answered Oct 02 '22 13:10

Johann Hilbold


You could as well use ProGuard shrinking. It can decrease the size of apk up to several times.

like image 44
bvk256 Avatar answered Oct 02 '22 15:10

bvk256


I've created a "port"(if I may say so) of XSSF recently: https://stackoverflow.com/a/25564538/2155217

It's enough for reading and writing XLSX files. Might not work properly if file contains some extra features such as Drawings or Charts.

like image 43
Andrew Kondratev Avatar answered Oct 02 '22 13:10

Andrew Kondratev