Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize APK size for Android #18253

Closed
megasoft78 opened this issue Apr 17, 2018 · 28 comments
Closed

Optimize APK size for Android #18253

megasoft78 opened this issue Apr 17, 2018 · 28 comments

Comments

@megasoft78
Copy link

Hi,
I've been playing a bit with compiling Godot to optimize apk size for simple game like mine:
https://play.google.com/store/apps/details?id=it.megasoft78.fallingword

My current result is 6.5 MB for the complete game apk and 5.3 MB for the template.
This template include the following modules:

Here's are the optimizations I did:
in platform/android/detect.py changed -O2 to -Os and add -s:

    env.Append(LINKFLAGS=['-Os', '-s'])
    env.Append(CPPFLAGS=['-Os', '-DNDEBUG', '-ffast-math',
                         '-funsafe-math-optimizations', '-fomit-frame-pointer'])

I run the compilation with the following options:
scons -j6 platform=android --config=force target=release tools=no deprecated=no minizip=no xml=no builtin_glew=no builtin_libmpcdec=no disable_3d=yes disable_advanced_gui=yes builtin_openssl=no module_openssl_enabled=no builtin_libogg=no builtin_libtheora=no builtin_libvorbis=no builtin_opus=no builtin_speex=no builtin_squish=no builtin_zlib=no module_chibi_enabled=no module_cscript_enabled=no module_dds_enabled=no module_etc1_enabled=no module_gridmap_enabled=no module_ik_enabled=no module_jpg_enabled=no module_mpc_enabled=no module_ogg_enabled=no module_opus_enabled=no module_pbm_enabled=no module_pvr_enabled=no module_speex_enabled=no module_squish_enabled=no module_theora_enabled=no module_vorbis_enabled=no

I disabled all the modules I don't need.
I also comment out in scene/register_scene_types.cpp all the calls to ObjectTypeDB::register_type that I don't need in my game.

My template is arm only because most of the x86 devices on Android support arm emulation and anyway are a very small percentage.

In my game I also used under Project Export Settings on tab Images WebP and on tab Samples IMA-ADPCM.

I was looking to remove physics because I don't use it but it wasn't easy and I give up. :)

The final libgodot_android.so is 8.5MB uncompressed and 3.2Mb compressed in apk.
My assets are 2.5MB uncompressed and 1.1MB compressed in apk.
The classes.dex is 5MB uncompressed and 1.9MB compressed in apk (most of the size is Google Play Services for Admob)

I'm quite happy with the final result and I hope this informations can be useful to other people. :)

If you have any idea to improve even futher just let me know.

@Shin-NiL
Copy link

Thanks for sharing your experience :)

@megasoft78
Copy link
Author

@Shin-NiL Thank you for your amazing modules! :)

@megasoft78
Copy link
Author

megasoft78 commented Apr 24, 2018

I was able to improve it a little bit more using Proguard.
I'm actually at 4.1 MB for the template and 5.3 MB for my final game.
I'm using the following Proguard:
https://pastebin.com/hM9vJsDV

I'm still testing it to be sure that everything is working but it seems to work.

Now libgodot_android.so is 7.2MB uncompressed and 2.8MB compressed. :)

@b3nnee
Copy link

b3nnee commented Aug 31, 2018

Hi @megasoft, could we get a link to your appbrain module. I am about to do the same thing. Felt if you've already done it, no need to reinvent the wheel.

However, if you can't provide it, I will just do it with their new mediation platform and stick it in GitHub for others. Cheers.

@megasoft78
Copy link
Author

megasoft78 commented Aug 31, 2018 via email

@megasoft78
Copy link
Author

Hi,
I attached the module but I think you need to tweak it to make it work. It was working on Godot 2.1.5. :)

Cheers
appbrain.zip

@b3nnee
Copy link

b3nnee commented Aug 31, 2018

Thanks @megasoft. Cheers

@girng
Copy link

girng commented Jan 6, 2019

wow, just did this my executable went from 40mb to 20... thank you!!

@eon-s
Copy link
Contributor

eon-s commented Jan 6, 2019

This could go on the documentation section about optimizations for size
http://docs.godotengine.org/es/latest/development/compiling/optimizing_for_size.html

@SAqsin
Copy link

SAqsin commented Jun 26, 2019

I get this error at image. https://imgur.com/a/mPIP6lM

How Fix that

@megasoft78
Copy link
Author

It looks like vorbis module is required in your case. Remove the builtin_libvorbis=no and module_vorbis_enabled=no from the command line. :)

@SAqsin
Copy link

SAqsin commented Jun 26, 2019

It looks like vorbis module is required in your case. Remove the builtin_libvorbis=no and module_vorbis_enabled=no from the command line. :)

Ok. i'll. try

@mani144
Copy link

mani144 commented Aug 11, 2019

this should pinned or put in the documentation.

Thank you

@Calinou
Copy link
Member

Calinou commented Aug 11, 2019

@mani144 I just opened a pull request to document APK architecture optimization (see above), which is an important step many people forget 🙂

As for other optimizations, I wonder if they could be integrated directly in Godot somehow. ProGuard in particular could be integrated into the current Gradle setup, including the one that's used for the custom Android build template.

@mani144
Copy link

mani144 commented Aug 14, 2019

we could suggest scons module details and what is optional for android and ios exporting

@TheArthurius
Copy link

hi @megasoft78 , can you pleases share with us your commented register_scene_types file?

@megasoft78
Copy link
Author

I attached my register_scene_types.cpp.

I hope it help. ;)

register_scene_types.zip

@TheArthurius
Copy link

TheArthurius commented Nov 15, 2019 via email

@megasoft78
Copy link
Author

Don't worry! I know how you feel. Keep going! You'll get there! ;)

@vignesh-lgtm
Copy link
Contributor

I am noob in godot can say where to run that command

@megasoft78
Copy link
Author

@Vignesh1-art Are you talking about scons command? In your Command Prompt / Terminal.

@kyoz
Copy link

kyoz commented Jun 25, 2021

Thank you very much for sharing. It helped a lot. But during building with scons, i'v got this error:

scons: Reading SConscript files ...
Checking for Android NDK...
Building for Android, platform android-24 (armv7) (with NEON)
Checking for C header file mntent.h... yes
scons: done reading SConscript files.
scons: Building targets ...
[Initial build] Linking Shared Library ==> bin/libgodot.android.opt.armv7.neon.so
scene/libscene.android.opt.armv7.neon.a(node_3d.android.opt.armv7.neon.o):node_3d.cpp:function Node3D::_update_visibility_parent(bool): error: undefined reference to 'VisualInstance3D::get_instance() const'
scene/libscene.android.opt.armv7.neon.a(node_3d.android.opt.armv7.neon.o):node_3d.cpp:function Node3D::_update_visibility_parent(bool): error: undefined reference to 'VisualInstance3D::get_instance() const'
scene/libscene.android.opt.armv7.neon.a(node_3d.android.opt.armv7.neon.o):node_3d.cpp:function Node3D::_update_visibility_parent(bool): error: undefined reference to 'typeinfo for VisualInstance3D'
scene/libscene.android.opt.armv7.neon.a(node_3d.android.opt.armv7.neon.o):node_3d.cpp:typeinfo for GeometryInstance3D: error: undefined reference to 'typeinfo for VisualInstance3D'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
scons: *** [bin/libgodot.android.opt.armv7.neon.so] Error 1
scons: building terminated because of errors.

I'm using a custom.py like below instead of passing all the argument in cmd

tools = "no"
disable_3d = "yes"
disable_advanced_gui = "yes"
deprecated = "no"
minizip = "no"
module_arkit_enabled = "no"
module_bmp_enabled = "no"
module_bullet_enabled = "no"
module_camera_enabled = "no"
module_csg_enabled = "no"
module_dds_enabled = "no"
module_enet_enabled = "no"
module_etc_enabled = "no"
module_gdnavigation_enabled = "no"
module_gridmap_enabled = "no"
module_hdr_enabled = "no"
module_jsonrpc_enabled = "no"
module_mbedtls_enabled = "no"
module_mobile_vr_enabled = "no"
module_ogg_enabled = "no"
module_minimp3_enabled = "no"
module_opensimplex_enabled = "no"
module_opus_enabled = "no"
module_regex_enabled = "no"
module_tga_enabled = "no"
module_theora_enabled = "no"
module_tinyexr_enabled = "no"
module_upnp_enabled = "no"
module_visual_script_enabled = "no"
module_vorbis_enabled = "no"
module_webm_enabled = "no"
module_webp_enabled = "no"

Any one face the same problem like me.

My Specs:
OS: Artix
Godot Source: Latest
Scons: 3.1.2

Thank you very much

@Calinou
Copy link
Member

Calinou commented Jun 25, 2021

Godot Source: Latest

"Latest" is not a version. Also, make sure to build from a stable tag like 3.3.2-stable instead of using the master branch as the master branch is not compatible with 3.x projects.

The export template versions must match the editor version exactly.

@kyoz
Copy link

kyoz commented Jun 25, 2021

@Calinou , Thanks, i forgot about that. I'll build from 3.3.2-stable 🐱

@Calinou
Copy link
Member

Calinou commented Sep 11, 2021

The documentation was improved and AAB support was added to automatically exclude unused architectures (on Google Play's end), closing.

@Calinou Calinou closed this as completed Sep 11, 2021
@Calinou Calinou added this to the 3.3 milestone Sep 11, 2021
@maihomahesh
Copy link

When I use the plugins (Use Custom Build), the custom template is not used. I replaced the default template's .aar files with newly generated .aar files. Apk size is reduced now but just by 2-3 mb. If I do not use custom build, the generated apk size would have been much smaller.

Do you have any idea how we can make custom build and custom templates work together?

@akien-mga
Copy link
Member

@maihomahesh You should use the bin/android_source.zip from your size optimized custom build.

The current workflow to do that is a bit awkward as of Godot 3.4, if you have a custom build which is not named 3.4.stable, or if you don't have the original export templates installed, the "Install Android source template" option should give you a dialog that lets you select your custom android_source.zip to install.

Otherwise, you can unzip it manually in res://android to match what the editor would do.

@maihomahesh
Copy link

@akien-mga Thank you for a quick response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests