mit-cml/ai2-kawa

Remove unused pieces of code to reduce APK size

pavi2410 opened this issue · 11 comments

We can remove code like this https://github.com/mit-cml/ai2-kawa/tree/master/gnu/kawa/swingviews which don't work on Android to save some APK size.

@ewpatton Can you please weigh in?

Who's Eric?

No problem, sir. I wish him a happy vacation!

@pavi2410 One concern is that it might make it harder in the future if we were going to try and update to newer versions of Kawa as it will cause merge conflicts all over the place. Maybe there's a way we could have a list of classes we could strip out of the Kawa jar. On the whole, it looks like Kawa contributes 2.2 MB to the build, so it might not be that much in savings. I'd be surprised if you could halve the size.

Upon running ant appinventor to compile a jar, I noticed these configs:

build-settings:
     [echo] 
     [echo]         version=1.11
     [echo]         dist.kawa.jar=/home/runner/ai2-kawa/kawa-1.11.jar
     [echo]         build.dir=/home/runner/ai2-kawa
     [echo]         with-awt=true
     [echo]         with-swing=true
     [echo]         with-sax2=true
     [echo]         enable-jemacs=false
     [echo]         enable-xml=true
     [echo]         enable-servlet=false
     [echo]         enable-android=false

Shouldn't it include with-awt=false, with-swing=false and enable-android=true instead?

There is a target select-android that is called later in the build process that resets the configuration to one that is specific for Android. You could try passing -Dwith-awt=false -Dwith-swing=false to the ant process, but I don't think you'll see a huge difference in jar size.

You are right! I could not notice any difference in binary size but diff reports that there is some difference.

~/ai2-kawa$ ls -l *jar
-rw-r--r-- 1 runner runner 2390784 Mar 18 12:46 kawa-1.11.jar
-rw-r--r-- 1 runner runner 2390784 Mar 18 12:45 kawa-1.11-noawt.jar
~/ai2-kawa$ diff kaw*jar
Binary files kawa-1.11.jar and kawa-1.11-noawt.jar differ
~/ai2-kawa$ file kawa*jar
kawa-1.11.jar:       Java archive data (JAR)
kawa-1.11-noawt.jar: Java archive data (JAR)

Produced when building the companion directly through Android Studio

> Task :app:minifyReleaseWithR8
WARNING:: R8: Missing class: java.awt.event.KeyListener
WARNING:: R8: Missing class: javax.swing.JFrame
WARNING:: R8: Missing class: javax.swing.text.ViewFactory
WARNING:: R8: Missing class: javax.swing.undo.AbstractUndoableEdit
WARNING:: R8: Missing class: java.awt.event.ActionListener
WARNING:: R8: Missing class: java.awt.geom.PathIterator
WARNING:: R8: Missing class: javax.swing.event.DocumentListener
WARNING:: R8: Missing class: javax.swing.text.AbstractDocument$Content
WARNING:: R8: Missing class: javax.swing.Box
WARNING:: R8: Missing class: java.awt.Shape
WARNING:: R8: Missing class: javax.swing.JPanel
WARNING:: R8: Missing class: java.awt.event.FocusListener
WARNING:: R8: Missing class: net.sf.cglib.proxy.MethodInterceptor
WARNING:: R8: Missing class: javax.swing.text.View
WARNING:: R8: Missing class: javax.management.DynamicMBean
WARNING:: R8: Missing class: javax.swing.DefaultButtonModel
WARNING:: R8: Missing class: javax.swing.JTextPane
WARNING:: R8: Missing class: javax.swing.JLabel
WARNING:: R8: Missing class: javax.swing.JButton
WARNING:: R8: Missing class: javax.servlet.ServletContextListener
WARNING:: R8: Missing class: javax.swing.text.StyledEditorKit
WARNING:: R8: Missing class: javax.swing.text.ComponentView
WARNING:: R8: Missing class: java.awt.event.WindowAdapter
WARNING:: R8: Missing class: javax.swing.text.DefaultStyledDocument
WARNING:: R8: Missing class: javax.swing.text.Position

This is what I wanted to highlight that Kawa references Java desktop GUI APIs on Android, which could be removed from the bytecode to save APK size.

I got the answer for this too.

There isn't any noticeable difference in binary sizes for the JARs because the AWT library is a runtime dependency. As long as it isn't called (which is never on Android), we're good.