Compressing the size of React Native apps - android

Can someone please specify me the procedures for compressing the app size built on react native.
I referred to many pages on google, hackernoon, and medium but they were too good for me to understand.
Please if someone knows basic and fewer glitch ideas on how to compress react native apps for both Android and iOS with procedures and where to keep the screen files and assets folder.
I tried by following the steps on medium, detached the iOS and android folders but it was an unsuccessful attempt.
Please if someone can help me in compressing the app size.

Yes you can easily reduce app size . Just go to app/build.gradle and pasted these lines or check these lines
def enableSeparateBuildPerCPUArchitecture = true
def enableProguardInReleaseBuilds = true
enjoy

you can reduce your react-native app size by this steps:
1- shrink your app icons and images size as possible as you can
2- go to android/app/build.gradle and make this changes:
project.ext.react = [
entryFile: "index.js",
enableHermes: true, // change false to true
]
...
def enableHermes = project.ext.react.get("enableHermes", true);
its for using hermes engine in bundling js server.
3-
def enableSeparateBuildPerCPUArchitecture = false // change false to true
the above line causes building seprate apks instead of universal apk. however you can have both seprates and universal by doing following changes :
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk true // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64" // seprate apks
}
}

Related

react-native size doesn't shrink

Trying to make a smaller APK, currently the size is 21 MB. Hoping to reduce it to 10MB.
I do this inside android/app/build.gradle, but it doesn't reduce, I just want ARM support for now:
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "arm64-v8a"
}
}
Try this, I hope this will helps you.
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 tad bit
Set def enableSeparateBuildPerCPUArchitecture = true . 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.
more information to https://medium.com/#aswinmohanme/how-i-reduced-the-size-of-my-react-native-app-by-86-27be72bba640

barteksc/AndroidPdfViewer massive APK Size

We included this AndroidPdfViewer library to support viewing of PDF reports in the app. It lead to massive increase in APK size from 4.7Mb to 20.1Mb .
Is there a way to reduce this size. Let me know where and what to tinker around to help or solve this.
I am familiar with proguard and have it configure for my app with reasonable success.
Why resulting apk is so big?
As stated in the documentation by barteksc/AndroidPdfViewer
Android PdfViewer depends on PdfiumAndroid, which is set of native
libraries (almost 16 MB) for many architectures. Apk must contain all
this libraries to run on every device available on market.
Fortunately, Google Play allows us to upload multiple apks, e.g. one
per every architecture. There is good article on automatically
splitting your application into multiple apks, available here.
Most important section is Improving multiple APKs creation and
versionCode handling with APK Splits, but whole article is worth
reading. You only need to do this in your application, no need for
forking PdfiumAndroid or so.
API: https://github.com/barteksc/AndroidPdfViewer
You have to generate Multiple APKs for different devices, by doing this you can reduce the size of apk up to 10 MB less than previous. Add below code to build.gradle (Module:App)
android{
.....
splits {
abi {
enable true
reset()
include 'x86_64', 'x86', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips'
universalApk false
}
}
.....
}
ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, mips: 4, 'x86': 5, 'x86_64': 6]
import com.android.build.OutputFile
// For each APK output variant, override versionCode with a combination of
// ABI APK value * 1000 + defaultConfig.versionCode
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) * 1000 + android.defaultConfig.versionCode
}
}
Whatch video here https://youtu.be/bP05Vpp49hs for detail explanation on how to do it.
If anyone is looking for a lightweight pdf viewer for your app, then try this one
Use this library
It helped me to reduce the app size from 27MB to 10MB because of the barteksc-androidpdfviewer requires more space
I also faced the same problem, but this is very easy as of now to reduce the apk sizes by using android app bundles.
Firstly in build.gradle(Module: app) add these lines inside android { } brackets.
bundle {
abi {
enableSplit = true
}
}
This will handle apk splits on the basis of architecture.
Now, also remember to add this line in proguard-rules.pro
-keep class com.shockwave.**
If you will not add this line in proguard-rules.pro then your app will crash in release version.
Now, after this go to Build and then select, Generate Signed Bundle/apk. From here, generate a signed bundle. Bundles are generated in very same way, as the apk's are generated.
Then, upload your bundle(.aab file) on Google Play Console and you will see that every device gets different apks depending upon their architecture.
This is the most easiest way to reduce the app size.Multiple apk concept is more complicated.
So,I suggest you to use this way.

Gradle dependency for specific architecture with ABI splits

Im facing this problem which seems im not able to solve. Here is scenario:
Im building apk which uses gradle dependency and this dependency is architecture specific so for apk for x86 i need different dependency and for arm different as well.
I solved it with product flavors:
productFlavors {
dev { ... }
develx86 { ... }
production { ... }
productionx86 { ... }
}
So then i defined dependency like this:
develCompile 'dependency_for_arm'
develx86Compile 'dependency_for_x86'
This works good. But recently i had to add to my application an usage of renderscript. I did it in this way:
renderscriptTargetApi 22
renderscriptSupportModeEnabled true
And after this when i uploaded apk on Google Play it says it's apk is suitable with arm, x86. I don't know how this is possible. As you can think it will crash on device with different CPU (if i generated apk for arm and user will execute it on x86 app will crash).
So i decited to use ABI splits:
splits {
abi {
enable true
reset()
include 'armeabi', 'x86'
universalApk false
}
}
//Ensures architecture specific APKs have a higher version code
//(otherwise an x86 build would end up using the arm build, which x86 devices can run)
ext.versionCodes = [armeabi:0, x86:1]
import com.android.build.OutputFile
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
int abiVersionCode = project.ext.versionCodes.get(output.getFilter(OutputFile.ABI)) ?: 0
output.versionCodeOverride = android.defaultConfig.versionCode + abiVersionCode
}
But now when i see generated apk files, my dependency which is flavor-specific is not included into apk and apk will crash when i open section which uses API from this dependency.
Do someone know how to solve this issue? Or someone know why Google Play says that apk is for both architectures when i included renderscript? (Without it it works properly but i need renderscript).
Thank you for your time. I will appreciate any help.
If you look inside your APK, under lib folder, you should see that renderscript support mode added libs for other architectures than the one you're supporting.
You can keep your earlier configuration with ABI-specific flavors.
But in order to ensure that no libs for other architectures are included, try adding abiFilters to your flavors:
productFlavors {
dev { ... ndk.abiFilters 'armeabi-v7a' }
develx86 { ... ndk.abiFilters 'x86' }
production { ... ndk.abiFilters 'armeabi-v7a' }
productionx86 { ... ndk.abiFilters 'x86' }
}
Sorry I cannot comment inline yet.
What's in the apk, especially in res/raw/ and lib/?
Also, are you using gradle-plugin 2.1.0? (since you are using renderscriptTargetApi 22), have you tried Build-Tools 23.0.3?

Android product flavors are not considered when using CPU ABI split in build.gradle

I want to make APK split based on CPU ABI according to http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits, however I want to split the APK only for a certain product flavor.
So my build.gradle file has the following product flavors plain and market. Actually I want the APK split to be performed when building market flavor.
android {
productFlavors {
plain {
}
market {
splits {
abi {
enable true
reset()
include 'armeabi', 'armeabi-v7a', 'x86', 'mips'
universalApk true
}
}
}
}
}
However, when I invoke gradle assemblePlainDebug and assembleMarketDebug, both of them produces the multiple APK. Is there something wrong with the configuration above?
I'm using com.android.tools.build:gradle:1.2.3.
I've been looking for a way to do this for a while and haven't found a solid solution. Something to do with the splits having to be run before resolving the buildTypes and productFlavors.
The Android Gradle - is use splits only for release possible? question had answer that I thought useful. It basically relies on a project property, passed in when building via the command line or continuous integration invironment, to set weather the split apk's option is enabled or not.
I used it like this:
splits {
abi {
enable project.hasProperty('splitApk')
reset()
include 'x86', 'armeabi-v7a', 'mips', 'armeabi'
universalApk true
}
}
and then depending on what falvour or build type you are building you can include:
./gradlew --project-prop splitApk assembleMarketDebug
This should then only enable the apk split when explicitly told too and should stay disabled for everything else.

How to reduce size of apk file?

I am working on an application which is containing .apk file of 13.56MB. It is containing 70 .png images. And there is image transparency so it have to be in .png format.
And i have to add more images. so do you have any solution to reduce the memory of an application.
Please used 9-patch images.
Nine patch images are especially useful when designing buttons. Custom drawn buttons can look distorted and pixelated when their borders are stretched in addition to the rest of the image.
Official Documentation and other resource for further help.
Use Following things
use tiny png to reduce size of images without loosing quality https://tinypng.com
Use tool AndroidUnusedResources to remove unneccessary resources
https://code.google.com/p/android-unused-resources/
Repackge jars or libraries using JarJar.jar tool. If you are using GooglePlayServices.jar then its too large
Batter way to reduce size of apk. Split apk and minifyEnabled=true.
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
splits {
abi {
enable true
reset()
include 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86'
universalApk false
}
}

Categories

Resources