For my react native android app, size of codes I have written is only 12KB
But package generated is of size 7.9MB. How can i decrease that.
As per Documentation for react native, I have already enbabled proguard on file app_root/android/app/proguard-rules.pro but size of didn't decrease
...
android {
...
buildTypes {
release {
...
minifyEnabled true
}
}
}
...
Is there a solution available for this?
React Native app APKs include JSCore binaries for x86 and ARM. If you don't need x86 you could reduce the size to 3-4 MB.
1)In your app/build.gradle set
def enableSeparateBuildPerCPUArchitecture = true
2)remove x86 from abiFilters
this helped me in reducing size from 7.8 mb to 4.5MB
Android devices support two major device artitectures armebi and x86. By default RN builds the native librariers for both these artitectures into the same apk.
Open up android/app/build.gradle :
Set def enableProguardInReleaseBuilds = true,
this would enable Progaurd to compress the Java Bytecode. This reduces the app size by a lil bit
And,
Set def enableSeparateBuildPerCPUArchitecture = true .
And check in android/app/build/outputs/apk/ - you will receive two apk files for armebi and x86 with approx half size of original apk.
There are a couple of techniques in order to reduce your APK size:
Split your app by architecture, you can split your APK by a list of well-known architectures: armeabi-v7a, x86, arm64-v8a, x86_64
if you're distributing your application, using the play store it's a good idea to do it with the Android app bundle.
Enabling prodguard will result in a lower size of the APK.
Optimize your image assets, and if there's a chance try using SVG assets as much a possible.
There have been a change in the size of the resulted APK in React-Native 0.62 by enabling the new JavaScript engine which reduce the size of the native libs almost 40% Hermes Engine
These stats below are from Hermes engine and also mention the APK size reduction.
release {
signingConfig signingConfigs.debug
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
shrinkResources true
ndk {
abiFilters "x86", "armeabi-v7a" //this will help you to shrink your app size in release build
}
}
Related
I have a very simple react native app it doesn't use any heavy resources, just 2 images (146kb combined). Upon generating the .aab file using this https://reactnative.dev/docs/signed-apk-android the size of the .aab file is 27 Mb. Once I publish the release on the playstore and test it on my device there is no change in the size of the app it is still 27 Mb. I have tried the following in an effort to reduce the size of the app but to no avail.
How to reduce Android APK size in react-native?
How to decrease React-Native android App Size
https://medium.com/#aswinmohanme/how-i-reduced-the-size-of-my-react-native-app-by-86-27be72bba640
The below code is what I'm currently using as part of the build.gradle file. I'm not sure on where I'm going wrong or why the size of the .aab file is 27 Mb even after installing on my device (I understand the .aab files are bundled with multiple apk configs and the size is supposed to reduce anywhere between 7-10 Mb on installation).
Any help would be greatly appreciated thanks in advance!
def enableSeparateBuildPerCPUArchitecture = true
def enableProguardInReleaseBuilds = true
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
debuggable false
shrinkResources true
zipAlignEnabled true
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
}
}
It works only after you upload it on the app store so the .aab file on local installs is generally heavier from those that are directly donwloaded from the play store.
I'm planning to migrate from ABI split to App Bundle feature. Currently I'm using this code:
def versionCodesAbi = ['x86': 1, 'x86_64': 2, 'armeabi-v7a': 3, 'arm64-v8a': 4]
splits {
abi {
enable true
reset()
include "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
// "armeabi", "mips", "mips64" last three not needed and not supported currently
universalApk true
}
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def abi = versionCodesAbi.get(output.getFilter(OutputFile.ABI))
if (abi != null) {
output.versionCodeOverride =
abi * 1000 + variant.versionCode
}
}
}
which gives 4 APKs per ABI (+ universal one). The reason of using this code is to reduce app size, because of PanoWidget (uses NDK) and
renderscriptTargetApi 28
renderscriptSupportModeEnabled true
After removing splits configuration (+4001 to versionCode) and building Bundle I got .aab file, which converted to .apks (using bundletool) contains folder standalones/. Inside I have four "kinds" of APK, for x86, x86_64, armeabi-v7a and arm64-v8a ABIs. Everything looks fine for now.
Now I've noticed that apps code isn't using RenderScript at all, so I think it's redundant to use supportMode and targetApi. I've removed these two lines, tested on devices/emulator, everything works fine. So next I'm producing Bundle and now it doesn't have x86_64 APK version inside .apks archive... Should it be ommitted without RenderScript support? I'm still using VrPanoramaView and it probably have some specific NDK code for every ABI (don't see on GitHub)... Sadly I don't have x86 (32 or 64) device for testing and nom I'm afraid of releasing this Bundle... Am I missing smth, do I even need _64 version?
Edit:
Removing these two options in the build.gradle will remove the native libraries that were used by RenderScript: librsjni.so and libRSSupport.so. These two libraries will be removed for all ABIs.
Since after disabling RenderScript, you still have 3 ABIs, it looks like your app depends on other libraries which make use of native code, but don't provide the libraries for the x86_64 architecture, which is why the x86_64 directory disappears. This probably means that your app never worked properly on x86_64 before since the x86_64 directory would be loaded by the platform but some native libraries would be missing.
Eventually, you should identify which library brings these native libraries and see if they can also build the 64 bit version, but in the short term, nothing will break since the x86_64 devices also support x86 (32-bit) libraries.
Previous post:
If you have any *.bc files in your APK, the 64-bit libraries are removed from the APKs because those RenderScript files are 32-bit only and cannot be loaded in a 64-bit process.
If you migrate to a more recent version of RenderScript, the *.bc files won't be generated and the 64-bit native libraries will be present again in the APKs. Or if you don't need RenderScript at all, then remove those files completely.
My assets and drawable are only 2mb java and xml sources is only 1mb but after build project the apk size is 20mb!
I set shrinkResources true
and remove unused resources and generate app with proguard.
Is there a way to reduce the size of apk?
Android Studio has its own apk analyzer which is very useful for cases like yours.
Analyze your apk file and check which files are using this much space.
https://developer.android.com/studio/build/apk-analyzer.html
Also using ProGuard helps to reduce apk size.
Additionally, avoid using unnecessary libraries. For example,
if you need to use Google Analytics, import gradle only analytics library like this:
compile 'com.google.android.gms:play-services-analytics:10.2.4'
do not use like this:
compile 'com.google.android.gms:play-services:10.2.4'
second example uses too much space and redundant classes and files.
at your gradle, normally debug apk will be larger than release APK about 50%. If you care about the debug size, just do the same config like release on debug config
buildTypes {
release {
minifyEnabled true <-- minify your code
shrinkResources true <-- remove any unused resources
zipAlignEnabled true <-- optimization
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' <-- enable proguard is important to shrink byte code
}
}
Second, if you are using google services dependencies, please use individual dependencies.. refer here In android studio 2.2 and above they have added apk analyser tool in Build menu. Use that to analyse APK.
Not only for google services, others library also. some library put android design or appcompat in their library. so you need to exclude those module (if you already have in your dependencies)
According to the "Resource Shrinking" webpage of Andriod documentations (here), you can minimize the app's size via the build.gradle file, by using these lines:
android {
...
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
You can do following things
Remove non required libraries. even use required libraries (like map
or gcm individual instead of full play service library)
Use vector images instead of multiple png.
Use zipAlignEnabled command in build file
Check my blog Different ways to reduce apk size
Main Points are :
android.enableR8=true // enable New R8 Code Shrinker
minifyEnabled true && Add Proguard Rules
Examine Your APK Using Android Studio’s APK Analyzer
Enable Resource Shrinking
Convert Your PNGs, JPEGs, and BMPs Into WebP
Avoid enumerations and use #IntDef annotation
Use aaptOptions { cruncherEnabled = false } to compress images
Use Remove Unused Resources android studio built-in function to remove all the unused resources in the application
Use Code Cleanup android studio built-in function to remove
Note: Go enable it! (*just double and triple check everything works afterwards)
OS : Mac os
Gradle 3.0
APK generated by studio : 13 Mb
APK generated by command line using "gradle assembleDebug" : 21 Mb.
Its almost double the size for the same project with the same build.gradle file.
I have used
aaptOptions
{
cruncherEnabled = true
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Any idea if there are parameters being passed specifically by Studio which help reduce the size?
If you build APK in Android studio connecting the phone, then Android studio removes all the resources from your drawable folder which is not applicable to that phone. That's the reason APK generated by Android studio looks smaller.
Try this, connect different resolution phone, HDPI, MDPI, XXHDPI phones while building your APK. You can find different size of APKs generated
I am using arcgis in my app and it bundles native dependencies that are large in size. I don't want to include the x86 dependency if it means reducing the size of the apk. How do I tell gradle to automatically exclude the x86 native library.
I tried removing it manually during the build. but it shows up again after rebuild.
how do I tell gradle to automatically exclude the x86 native library
Use splits:
android {
// other good stuff here
splits {
abi {
enable true
reset()
include 'armeabi-v7a'
universalApk false
}
}
}
This tells Android to build only an ARMv7 version of your APK. You would need to adjust the include line to list what APKs you want.
However, you may be better served using splits to just build a separate x86 APK file (have include 'x86', 'armeabi-v7a') and ship both, so you better support x86 but still have smaller files.