3

I'm using ffmpeg library in my Project. Problem is App apk size is too big. Anyone know how to reduce apk size ? Any help, suggestion or links would be highly appreciated. Thank you.

tech_android
  • 719
  • 1
  • 7
  • 18
  • remove unused resources from an android project it will also reduce apk size. https://stackoverflow.com/questions/6373482/remove-all-unused-resources-from-an-android-project – Mehul Patel May 21 '18 at 07:09
  • If you are using ffmpeg for video streaming, then instead of using ffmpeg library you can directly use Exoplayer. – Anand Phadke May 21 '18 at 07:09
  • @mehupatel Thanks, But already do this. – tech_android May 21 '18 at 07:14
  • Yes we use this for Video compression. – tech_android May 21 '18 at 07:17
  • The ffmpeg binary is about 16mb. You cannot change this (except maybe compiling ffmpeg yourself with NDK and enabling every optimization flag for gcc). But you most likely won't get below 16mb. – Ch4t4r May 21 '18 at 07:19
  • @ch4t4r Yes know this. Any other alternative available to do this job. Video compress same like FFmpeg.Plz tell if u know – tech_android May 21 '18 at 07:22
  • use [MediaCodec](https://developer.android.com/reference/android/media/MediaCodec) if your min API level is >= 16 – pskink May 21 '18 at 07:23
  • If I recall correctly I've compiled a `ffmpeg` binary as small as 2.5 MB (with very limited functionality but what the client wanted). A more reasonable, much less restricted build for another was 7.3 MB. It depends on what what need to it to do. However, making these very small builds is not always trivial due to component interdependencies meaning you have to be familiar with it or experiment until you get it right. – llogan May 21 '18 at 19:07
  • 1
    Is this still on the table? if yes then try to compress all cpu architecture models in one archive file. I prefer `7z`. extract target cpu lib based on cpu model on app's directory and load ffmpeg from there. I'm using `ffmpeg/ffprobe(arm/x86)` --> **79.8 MB**. after doing above steps --> **13.2 MB**. If you need more help let me know. – S.R Dec 23 '18 at 15:51
  • Hello @S.R I'm facing the same problem. I'm developing an app that creates mpeg4 videos using this library: com.arthenica:mobile-ffmpeg-full:4.2.2.LTS but the final apk size, as you mentioned, is 71Mb that is obviously too much. Using com.arthenica:mobile-ffmpeg-min-gpl:4.2.2.LTS now I have a 50Mb because it only downloads a few codecs, but for me it's still too much and I don't know what else to do to reduce apk size. Cannot understand very well your explanations so, could you help me further please? – Diego Perez Mar 28 '20 at 16:11
  • @DiegoPerez https://github.com/bravobit/FFmpeg-Android/issues/80 – S.R Mar 30 '20 at 18:31
  • Thank you really very much @S.R. I'll give it a try! – Diego Perez Mar 31 '20 at 11:01
  • Hi again @S.R. I'm trying to implement ffmpeg compression / decompression with the link you've provided but I think I'll need further help. My problem is the next: My library (the one I put the url in previous comment) doesn't include ffmpeg and ffprobe executables as they are, but instead in each cpu folder inside libs you have the next files: libavcodec.so libavdevice.so libavfilter.so libavformat.so libavutil.so libc++_shared.so libcpufeatures.so libmobileffmpeg-abidetect.so libmobileffmpeg.so and so, so I guess I'll have to get ffmpeg and ffprobe for all cpus separately in web, right? – Diego Perez Mar 31 '20 at 14:02
  • But then I don't know how to do ffmpeg video creation in my app, because at the moment I use the sintax described in my library to launch ffmpeg, and my app has all those not lightweight so libraries, but if I remove my library from Gradle I don't know how I will launch ffmpeg when needed. Any further help or example? – Diego Perez Mar 31 '20 at 14:02
  • @DiegoPerez hello, today I am in your case. I am using this com.arthenica:mobile-ffmpeg-full:4.2.2 for audio compression but the app size becomes 120 MB. have you found any solution for this? – Gulab Sagevadiya Apr 13 '22 at 11:58
  • Hello @gulab patel. Check my answer below. – Diego Perez Apr 13 '22 at 23:14

1 Answers1

2

Hello @tech_android and @gulab patel. I've found a solution for this.

Android Gradle allows you to split your apk according processor ("armeabi-v7a", "arm64-v8a", "x86" and "x86_64"). This way you'll end up with 4 apks instead of one, and the size of each will be the fourth part, and in Google Play you upload all four apks and when a user tries to download your app Google Play will choose the correct apk depending on users device processor.

You have to set-up app split in gradle this way:

Below compilerOptions node (inside android json node):

splits {
    // Configures multiple APKs based on ABI.
    abi {
        // Enables building multiple APKs per ABI.
        enable true
        // By default all ABIs are included, so use reset() and include to specify that we only
        // want APKs for x86 and x86_64.
        // Resets the list of ABIs that Gradle should create APKs for to none.
        reset()
        // Specifies a list of ABIs that Gradle should create APKs for.
        include "armeabi-v7a"
        include "arm64-v8a"
        include "x86"
        include "x86_64"
        // Specifies that we do not want to also generate a universal APK that includes all ABIs.
        universalApk false
    }
}

And just before dependencies node:

// This next piece of code is used by apk Split
// map for the version code that gives each ABI a value. make sure to list all ABIs mentioned in splits block, an keep the order.
ext.versionCodes = ['armeabi-v7a': 1, 'arm64-v8a': 2, 'x86': 3, 'x86_64': 4]
Diego Perez
  • 2,188
  • 2
  • 30
  • 58
  • can i know after splitting apk size? – Gulab Sagevadiya Apr 14 '22 at 06:49
  • because we have a bundle so I don't have to split apk play console will manage this. but I just want to know the size for each abi version so I can get an idea about the approx size of the app – Gulab Sagevadiya Apr 14 '22 at 07:05
  • 1
    @gulab patel Im not at my home computer right now, but for you to have an idea if my apk size is about 80Mb, then after splitting each apk is about 20mb. – Diego Perez Apr 14 '22 at 09:25