[Java] Dieting program with preprocessor (How to deal with i-appli size)

4 minute read

The current mobile terminals are mainly smartphone iPhones and androids. Do you know the maximum size of each application? As of August 2020, iOS apps are 4GB and android apps are 100MB.

There is a big difference when you look at this… I wonder if android has a large-capacity game that downloads data later. I haven’t written the heap in detail, but since the terminal has a few GB of memory, it shouldn’t be too big a problem.

However, what I would like to talk about this time is the future phone (= Garaka) that was active as a mainstay before the birth of the smartphone, and the story of Docomo’s i-appli.

I would like to introduce that the initial development method of DoCoMo’s i-appli was the result of tearful efforts.

Docomo i-appli size

Time goes back to the age of Garaka. We recognize that it was Docomo’s i-appli that pulled the Garake application world, but what was the size limit at that time …

The following is the information of the application on the DoJa-1.0 profile that was issued in the first period.

Model App size (KB) Heap capacity (KB)
FOMA N2001 10 96
FOMA N2002 10 560
FOMA P2101V 30 1100
FOMA D2101V 10 128
FOMA P2002 10 560
FOMA SH2101V 30 1024
FOMA T2101V 30 1105

Official page https://www.nttdocomo.co.jp/service/developer/make/content/spec/iappli/#notice05

The minimum app size was 10KB and the heap capacity was 96KB. It seems that the native heap is separate and the memory on the terminal side can be used a little, but this capacity is in the pure Java part.

The app size is a Jar file that includes bytecode compiled from the code and resources such as images. That was kept at 10KB (10240 bytes).

It may be only a matter of consciousness, so I will check how much it is. However, it is difficult to build an i-appli development environment, so we will compare with the android app size as of August 2020.

  • Jota+(Text Editor) 7.7MB
  • Stopwatch timer 8.84MB

I chose an app that seems to have relatively few functions, but MB units are common. Since things and times are different, it is not possible to make a general comparison, but I can not imagine that it will fit within 10 KB.

Early app development code

I understand that the size of i-appli is extremely tight, but I wonder how it was made. The only element that can be cut is a program.

It’s certainly true that people say that they can’t do what they want because the program lacks the functionality, so there is a way to reduce the size without reducing one function. That is to reduce variable names, class names, and method names.

The code is written with a fairly appropriate feeling, but for example, it looks like this.

before change


public class MainFrame {
 
    private static final String TITLE = "Sample app";
    private static final String ERROR_MESSAGE = "An error has occurred";
 
    public void showFrame() {
        try {
            // do something
        } catch (Exception e) {
            showMessage(ERROR_MESSAGE);
        }
    }
 
    public void showMessage(String message) {
        // Message display processing
    }
}

after change


public class F {
 
    private static final String A = "Sample app";
    private static final String B = "Error occurred";
 
    public void s() {
        try {
            // do something
        } catch (Exception e) {
            m(B);
        }
    }
 
    public void m(String s) {
        // Message display processing
    }
}

If you reduce the number of characters in variable names and method names, the number of characters written in the compiled class file will decrease, so you can expect a size reduction. It’s an energy-saving program anyway.

If it is a program of this size, it will not be unreadable, but it is obvious that the more you build it, the worse the readability will be. How did you clear this problem?

Evil way called preprocessor + Java

As a result of our wisdom, our predecessors decided to use something that the Java language does not. It is a preprocessor often used in C language.

Specifically, write the following code.

``

 preprocessor version code
# define _MainFrame F
# define _TITLE A
# define _ERROR_MESSAGE B
# define _showFrame s
# define _showMessage m
  
public class _MainFrame {

    private static final String _TITLE = "Sample app";
    private static final String _ERROR_MESSAGE = "An error has occurred";

    public void _showFrame() {
        try {
            // do something
        } catch (Exception e) {
            _showMessage(_ERROR_MESSAGE);
        }
    }

    public void _showMessage(String s) {
        // Message display processing
    }
}

The syntax below means converting the character “_MainFrame” to the character “F”.

``


# define _MainFrame F

This part is not Java syntax, so it cannot be compiled by Java compiler. So let’s go through the preprocessor. Then the following code is completed.

public class F {
  
    private final String A = "Sample app";
    private final String B = "Error occurred";
  
    public void s() {
        try {
            // do something
        } catch (Exception e) {
            m(B);
        }
    }
  
    public void m(String s) {
        // Message display processing
    }
}

Now that you can compile, all you have to do is build normally.

In this way, by passing the code with macros in two steps, preprocessor and Java compiler, it is possible to reduce the size while improving readability. I think it was hard because I can’t use the IDE.

Finally

This article is based on the idea of using a preprocessor that I saw on another site nearly 10 years ago. The site doesn’t exist yet, but I wrote it because I wanted to keep the idea.