3

Edit: Updated with dependencies

Edit 2: Updated to show the function causing the error

Dependencies:

implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
implementation group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '3.1.0'
implementation 'com.fasterxml:aalto-xml:1.2.2'
implementation("it.skrape:skrapeit:1.2.2")

In my proguard file I have the following:

-keep class org.apache.poi.** {*;}
-keep class org.apache.xmlbeans.** {*;}
-keep class com.fasterxml.** {*;}
-keep class com.microsoft.schemas.** {*;}
-keep class org.openxmlformats.** {*;}
-keep class org.openxmlformats.schemas.** {*;}
-keep class schemaorg_apache_xmlbeans.** {*;}

This works fine in the debug build, but causes a crash when axcessing one of the methods in the release build.

The full stack trace:

java.lang.ExceptionInInitializerError
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(Unknown Source:0)
    at j7.a.a(SourceFile:2)
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.<clinit>(SourceFile:1)
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook$Factory.newInstance(Unknown Source:0)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(SourceFile:1)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:5)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:1)
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.pointsToWorkBook(SourceFile:2)
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.dataPointsToExcelFile(SourceFile:1)
    at f9.k.invokeSuspend(SourceFile:17)
    at sa.a.resumeWith(SourceFile:3)
    at qd.o0.run(SourceFile:18)
    at xd.a$a.run(SourceFile:14)
Caused by: java.lang.IllegalStateException: Cannot load nodeToCursor: verify that xbean.jar is on the classpath
    at org.apache.xmlbeans.XmlBeans.buildMethod(SourceFile:4)
    at org.apache.xmlbeans.XmlBeans.buildNodeMethod(Unknown Source:18)
    at org.apache.xmlbeans.XmlBeans.buildNodeToCursorMethod(Unknown Source:4)
    at org.apache.xmlbeans.XmlBeans.<clinit>(SourceFile:11)
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(Unknown Source:0) 
    at j7.a.a(SourceFile:2) 
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.<clinit>(SourceFile:1) 
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook$Factory.newInstance(Unknown Source:0) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(SourceFile:1) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:5) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:1) 
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.pointsToWorkBook(SourceFile:2) 
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.dataPointsToExcelFile(SourceFile:1) 
    at f9.k.invokeSuspend(SourceFile:17) 
    at sa.a.resumeWith(SourceFile:3) 
    at qd.o0.run(SourceFile:18) 
    at xd.a$a.run(SourceFile:14) 
Caused by: java.lang.NoSuchMethodException: org.apache.xmlbeans.impl.store.Locale.nodeToCursor [interface org.w3c.dom.Node]
    at java.lang.Class.getMethod(Class.java:2103)
    at java.lang.Class.getMethod(Class.java:1724)
    at org.apache.xmlbeans.XmlBeans.buildMethod(SourceFile:1)
    at org.apache.xmlbeans.XmlBeans.buildNodeMethod(Unknown Source:18) 
    at org.apache.xmlbeans.XmlBeans.buildNodeToCursorMethod(Unknown Source:4) 
    at org.apache.xmlbeans.XmlBeans.<clinit>(SourceFile:11) 
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(Unknown Source:0) 
    at j7.a.a(SourceFile:2) 
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.<clinit>(SourceFile:1) 
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook$Factory.newInstance(Unknown Source:0) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(SourceFile:1) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:5) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(SourceFile:1) 
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.pointsToWorkBook(SourceFile:2) 
    at com.shervinkoushan.anyTracker.shared.utils.FileUtils.dataPointsToExcelFile(SourceFile:1) 
    at f9.k.invokeSuspend(SourceFile:17) 
    at sa.a.resumeWith(SourceFile:3) 
    at qd.o0.run(SourceFile:18) 

The function causing the error:

    fun pointsToWorkBook(
        dataPoints: List<DataPoint>, textPoints: List<TextPoint>, trackedElement: TrackedElement,
        isText: Boolean, context: Context
    ): Workbook {
        initPoi()
        val workbook = XSSFWorkbook()
        val sheet = workbook.createSheet(stringToValidFileName(trackedElement.title))

        val titleCell = sheet.createRow(0).createCell(0)
        titleCell.setCellValue(trackedElement.title)
        val font = workbook.createFont()
        font.bold = true
        val titleStyle = workbook.createCellStyle()
        titleStyle.setFont(font)
        titleCell.cellStyle = titleStyle

        val linkFont = workbook.createFont()
        linkFont.underline = Font.U_SINGLE
        linkFont.color = IndexedColors.BLUE.getIndex()
        val anyTrackerCell = sheet.createRow(2).createCell(0)
        val anyTrackerString = XSSFRichTextString("Tracking with AnyTracker")
        anyTrackerString.applyFont(14, 24, linkFont)
        anyTrackerCell.setCellValue(anyTrackerString)
        val link = workbook.creationHelper.createHyperlink(HyperlinkType.URL)
        link.address = "https://play.google.com/store/apps/details?id=com.shervinkoushan.anyTracker"
        anyTrackerCell.hyperlink = link

        val infoCell = sheet.createRow(3).createCell(0)
        val trackedInfo = StringUtils.getTrackedInfo(trackedElement, context)
        infoCell.setCellValue(trackedInfo)
        trackedElement.websiteBundle?.websiteUrl?.let { url ->
            val websiteLink = workbook.creationHelper.createHyperlink(HyperlinkType.URL)
            websiteLink.address = url
            infoCell.hyperlink = websiteLink
            val richString = XSSFRichTextString(StringUtils.getTrackedInfo(trackedElement, context))
            val startIndex = trackedInfo.indexOf(url)
            richString.applyFont(startIndex, startIndex + url.length, linkFont)
            infoCell.setCellValue(richString)
        }
        val header = sheet.createRow(5)
        header.createCell(1).setCellValue("Date")
        sheet.setColumnWidth(0, 23 * 260)
        sheet.setColumnWidth(1, 16 * 260)

        val dateFormat = workbook.creationHelper.createDataFormat().getFormat("dd.mm.yyyy hh:mm")
        val dateStyle = workbook.createCellStyle()
        dateStyle.setBorderLeft(BorderStyle.THIN)
        dateStyle.setBorderRight(BorderStyle.THIN)
        dateStyle.dataFormat = dateFormat
        dateStyle.setAlignment(HorizontalAlignment.LEFT)
        val bottomDateStyle = workbook.createCellStyle()
        bottomDateStyle.setBorderLeft(BorderStyle.THIN)
        bottomDateStyle.setBorderBottom(BorderStyle.THIN)
        bottomDateStyle.setBorderRight(BorderStyle.THIN)
        bottomDateStyle.dataFormat = dateFormat
        bottomDateStyle.setAlignment(HorizontalAlignment.LEFT)
        val valueStyle = workbook.createCellStyle()
        valueStyle.setBorderRight(BorderStyle.THIN)
        val bottomValueStyle = workbook.createCellStyle()
        bottomValueStyle.setBorderRight(BorderStyle.THIN)
        bottomValueStyle.setBorderBottom(BorderStyle.THIN)

        if (isText) {
            sheet.setColumnWidth(
                2, max("Value".length, textPoints.maxOfOrNull { it.text.length } ?: 0
                ) * 250
            )
            header.createCell(2).setCellValue("New text")
            textPoints.forEachIndexed { index, textPoint ->
                val row = sheet.createRow(index + 6)
                val dateCell = row.createCell(1)
                dateCell.setCellValue(Date.from(textPoint.date))
                val valueCell = row.createCell(2)
                valueCell.setCellValue(textPoint.text)
                if (index == textPoints.size - 1) {
                    dateCell.cellStyle = bottomDateStyle
                    valueCell.cellStyle = bottomValueStyle
                } else {
                    dateCell.cellStyle = dateStyle
                    valueCell.cellStyle = valueStyle
                }
            }
        } else {
            sheet.setColumnWidth(
                2, max("Value".length, dataPoints.maxOfOrNull { it.value.toString().length } ?: 0
                ) * 320
            )
            header.createCell(2).setCellValue("Value")
            dataPoints.forEachIndexed { index, dataPoint ->
                val row = sheet.createRow(index + 6)
                val dateCell = row.createCell(1)
                dateCell.setCellValue(Date.from(dataPoint.date))
                val valueCell = row.createCell(2)
                valueCell.setCellValue(dataPoint.value.toString())
                if (index == dataPoints.size - 1) {
                    dateCell.cellStyle = bottomDateStyle
                    valueCell.cellStyle = bottomValueStyle
                } else {
                    dateCell.cellStyle = dateStyle
                    valueCell.cellStyle = valueStyle
                }
            }
        }

        val headerStyle = workbook.createCellStyle()
        headerStyle.setBorderTop(BorderStyle.THIN)
        headerStyle.setBorderBottom(BorderStyle.THIN)
        headerStyle.setBorderLeft(BorderStyle.THIN)
        headerStyle.setBorderRight(BorderStyle.THIN)
        header.getCell(1).cellStyle = headerStyle
        header.getCell(2).cellStyle = headerStyle

        return workbook
    }

Is there anything I am missing in my proguard file? I have tried various code snippets from Stack Overflow with no success.

shko
  • 64
  • 2
  • 20
  • [Apache POI 3.17 is over 5 years old now](https://poi.apache.org/devel/history/changes-3x.html#3.17), what happens if you try with a vaguely recent release? – Gagravarr Nov 28 '22 at 09:11
  • Sadly, newer versions come with other problems. I tried to use 4.10 and it crashed even without Proguard activated. So I would prefer to leave my functioning code as is, but fix this issue of course – shko Nov 28 '22 at 17:17
  • Latest Apache POI release right now [is 5.2.3](https://poi.apache.org/changes.html#5.2.3) – Gagravarr Nov 28 '22 at 17:48
  • The problem with that release is that it requires minSDK 26, while my app has minSDK set to 23 – shko Nov 28 '22 at 18:53

1 Answers1

1

You could use -keep class !com.your.package.** { *; } to keep everything outside the files of your own package.

km11
  • 528
  • 3
  • 12
  • 2
    Yes, this works. But I obviously want something less general – shko Dec 02 '22 at 20:43
  • could you shed some light on where in code/source you get this error. example code will help if you can. – ddassa Dec 21 '22 at 11:46
  • @ddassa added the code causing the crash now. – shko Dec 21 '22 at 14:50
  • thank you for the code update. I have tried it could not re-create the issue. I had to add `implementation 'javax.xml.stream:stax-api:1.0'` dependancy. is that something you using in your build too? – ddassa Dec 22 '22 at 12:36
  • No, my related dependencies are listed above. Adding it, I got the following error: `Duplicate class javax.xml.XMLConstants found in modules jetified-htmlunit-android-2.63.0 (net.sourceforge.htmlunit:htmlunit-android:2.63.0) and jetified-stax-api-1.0 (javax.xml.stream:stax-api:1.0)` – shko Dec 22 '22 at 19:37
  • yes of course, i added `implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit-android', version: '2.63.0'` and it came up with same error. I didnt know you were using this library. Anyway a serious lint issue was thrown and had to exclude `common-logging`. But still cannot recreate the issue. – ddassa Dec 23 '22 at 10:15
  • I was not using the library directly, but I am using `it.skrape:skrapeit:1.2.2`, which I believe uses it. Will update the description with it now. Anyway, I do not get any lint errors. How did you exclude `common-logging`? @ddassa – shko Dec 23 '22 at 11:52
  • you can exclude duplicate libraries like this in your dependancy block; `configurations { all{ exclude group: 'commons-logging', module: 'commons-logging' } }` – ddassa Dec 23 '22 at 12:10
  • Thanks, but I still get the same error @ddassa – shko Dec 23 '22 at 12:46