godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.35k stars 21.06k forks source link

Optimize APK size for Android #18253

Closed megasoft78 closed 3 years ago

megasoft78 commented 6 years ago

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 commented 6 years ago

Thanks for sharing your experience :)

megasoft78 commented 6 years ago

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

megasoft78 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

Il venerdì 31 agosto 2018, 10:34:00 CEST, Benjamin Yakubu notifications@github.com ha scritto:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

megasoft78 commented 6 years ago

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 commented 6 years ago

Thanks @megasoft. Cheers

girng commented 5 years ago

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

eon-s commented 5 years ago

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

SAqsin commented 5 years ago

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

How Fix that

megasoft78 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

this should pinned or put in the documentation.

Thank you

Calinou commented 5 years ago

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

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 commented 5 years ago

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

TheArthurius commented 4 years ago

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

megasoft78 commented 4 years ago

I attached my register_scene_types.cpp.

I hope it help. ;)

register_scene_types.zip

TheArthurius commented 4 years ago

you are amazing :) thank you very much for this fastest answer what I ever have got and for sharing your approach in compiling godot. It was hell for me last 3 days and your article at github was like light in this darkness :D now i have 16MB apk and maybe get smaller after succesfull change of register types file. thank you very much ;)

pi 15. 11. 2019 o 11:24 megasoft78 notifications@github.com napísal(a):

I attached my register_scene_types.cpp.

I hope it help. ;)

register_scene_types.zip https://github.com/godotengine/godot/files/3850999/register_scene_types.zip

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/godotengine/godot/issues/18253?email_source=notifications&email_token=AI3PCLGRDYVUXU7X6TDFXIDQTZ2HDA5CNFSM4E3BOXCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEFAASY#issuecomment-554303563, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI3PCLAX5RTLWQMRCGKGASDQTZ2HDANCNFSM4E3BOXCA .

megasoft78 commented 4 years ago

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

vignesh-j-shetty commented 4 years ago

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

megasoft78 commented 4 years ago

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

kyoz commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

Calinou commented 3 years ago

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

maihomahesh commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@akien-mga Thank you for a quick response.