How to reduce binary executable file size?

298 views
Skip to first unread message

Qi Chen

unread,
Aug 2, 2017, 9:33:20 AM8/2/17
to j2objc-discuss
Hi,

I have tried this "Getting Started" introduction and build "Hello" executable file.

By default j2objcc command, the file size is 9.3MB.
By Add "-s" (with or without -Os) to j2objcc command, the file size is 7.3MB.
By "stri" command, the file size is 6.4MB.

By "nm" command, I can see the whole JRE library inside this executable file, But the source file only implement a "Hello World".

So is there any configuration or method can help to reduce this huge size?

Thanks.

Tom Ball

unread,
Aug 2, 2017, 11:23:17 AM8/2/17
to j2objc-discuss
It isn't possible to get a binary created with j2objc down to the size of one created with native Objective C for two reasons:
  1. All Java apps require the Java Runtime Environment, which is huge (just the main jre/lib/rt.jar is 64MB). All JRE references in the j2objc app, direct and indirect, have to be linked into the iOS app due to iOS SDK license restrictions. A hello world app run with "java -verbose:class" lists 420 classes were loaded.
  2. iOS apps are similarly large, but most of the framework code is in dynamic libraries built into iOS. iOS doesn't allow third-parties like j2objc to distribute dynamic libraries for security reasons, so its JRE needs to be distributed as a static library, which gets linked into the app.
We created j2objc for apps that have a lot of client-side, platform-independent code, which can be shared by Android and/or web versions. For example, the Google Sheets app has a spreadsheet engine completely written and tested using platform-independent Java, which is then built into the Android app, and transpiled to the iOS app and web client (using the GWT compiler). That spreadsheet engine was originally written in Java, Objective C and JavaScript, but it proved very time-consuming to maintain and keep the three versions in sync. For big apps like this, the size of the JRE is worth the cost of rewriting that code.

We therefore don't recommend using j2objc for smaller apps, especially those that mainly provide a user interface to server apps (there's little client-side code worth sharing). Any competent Java or Objective C developer can learn the other language quickly (there are many similarities), so it's usually faster and more efficient to just rewrite a small app.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Ball

unread,
Aug 2, 2017, 12:22:07 PM8/2/17
to j2objc-discuss
FWIW, Xamarin apps are roughly the same size, for similar reasons. It uses .NET instead of Java, but both runtimes have to be big to provide all of their functionality.

kgal...@gmail.com

unread,
Aug 8, 2017, 4:10:28 PM8/8/17
to j2objc-discuss
I spent some time stressing on this and doing some tests. I'm going to assume you ultimately want to put this into the app store. For us, the metric was a little soft. Basically, two questions. 1) Is this reasonable? 2) How does it compare?

I only have numbers for us, but this app uses j2objc and swift (2.something), and lists at 52.6 megs in the app store listing:

https://itunes.apple.com/us/app/droidcon-nyc-2016/id1155197664?mt=8

When you look on the phone, it lists at 26.6 megs, and the phone's app store listing reflects that. For an iOS app, that's pretty good, and there was no effort to make that smaller at the time (it used -ObjC for example, I think). On the device, when looking at local storage usage, it's pretty low in the size listing when compared with other apps. For question #1, our answer is "yes". For a general iOS app with significant "logic", the J2ojbc overhead is reasonable.

I did some absolute minimum size tests several months ago. I posted to ITunes Connect with various configurations. I believe the absolute take-the-doors-off minimum with j2ojbc was 10-12 megs. Looking at it right now, it's listing at 16 megs, but itunes connect changed its interface. It used to show generic app listing size, and a per-device number. That latter set of numbers is gone (or I can't find it). Anyway, react native comes in at 5-6 megs, but you have to consider that the Android side for J2objc is 0 megs, so on the balance I'd say it comparable!

I have no idea on web sizes. We've only done mobile builds. Hope that's useful.

Tom Ball

unread,
Aug 8, 2017, 4:52:31 PM8/8/17
to j2objc-discuss
Thanks, Kevin, excellent analysis!

Qi Chen

unread,
Aug 9, 2017, 12:46:02 AM8/9/17
to j2objc-discuss, kgal...@gmail.com
Thank you, It's awesome.

在 2017年8月9日星期三 UTC+8上午4:10:28,kgal...@gmail.com写道:
Reply all
Reply to author
Forward
0 new messages