I am facing a problem with app size in android.
The scenario is,
I developed my android app in Android Studio 2.0 and the size of apk was 23 MB.
After that, I upgraded my IDE to android studio 2.2 and with little code modification the size of apk boosted to 51 MB.
I tried with prorogued and Lint but no advantage.
Can Someone help me to tackle the issue.
1) Replace all of Images,Icons with vector drawable
2) Turn on pro guard like following
goto build.gradleapp level
and put these lines
**shrinkResources true
minifyEnabled true**
3) Remove unused classes,drawable and methods and strings
and use LINT's private method analyser which reduces method count
JAVA's Hidden cost
4) In android studio 2.2 and above they have added apk analyser tool in Build menu. Use that to analyse APk
5) if app size goes beyond 100mb use feature called split apk.
there are two methods of spliting apk
ABI and Density Splits
Do this changes in your build.gradle(Module:app) file. It decreases the apk size almost (40-50)%.
android {
// Other settings
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
It reduces the size of classes.dex files and res folder.
It is recommended to that you should use webp file format instead of BMP, JPG, PNG for better compression.
For reference, you can use: https://developer.android.com/studio/write/convert-webp.html
For more details on apk compression you can refer:
https://developer.android.com/topic/performance/reduce-apk-size.html
https://medium.com/how-you-can-decrease-application-size
Points to reduce APK size:
Make sure to do it
1. Use vector drawable
2. Use xml drawable to create simple view
3. Rotate images using xml drawable to reuse (eg. in case of arrow buttons)
4. Create drawable through code
5. Use aaptOptions { cruncherEnabled = false } to compress images
6. Use webP format for large images
7. Avoid enumerations and use #IntDef annotation
8. Use shrinkResources true in gradle to remove unused resources
9. Use resConfig “en” in gradle to remove other localization
android {
// Other settings
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
shrinkResources true will not include the images from resources which your using in the final apk
hope this will help you!
Related
Gradle build failing with this error:
Error:C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature
Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
Error:Execution failed for task ':app:mergeDebugResources'.
Error: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
aapt2 compile --legacy -o C:\dev\workspace\android2\MatrixCalculator\app\build\intermediates\res\merged\debug C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png
Issues:
- ERROR: C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature
Some basic things i've tried to solve this issue:
Invalidate caches/restart
Deleting gradle folder
It's likely a JPG renamed to a PNG file, not an actual PNG file
The problem could be because of the wrong extension of images.
In my case, the file was a JPEG image but it was saved as PNG not converted to. In this situation change extension to the real one and convert to PNG then retry.
For instance, you have ic_logo.png but it's actually a JPG image.
You should rename it back to ic_logo.jpg and use something like Photoshop to convert the image to PNG format.
android {
buildTypes {
release {
crunchPngs false // or true
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
crunchPngs false // or true
lintOptions {
checkReleaseBuilds false
abortOnError false
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Steps
In build.gradle
aaptOptions {
cruncherEnabled = false
}
Delete content inside C:\Users\.gradle\caches (or ~/.gradle/caches for Mac and Linux)
Restart Android Studio
Failed to read PNG signature - Just put 1 line in your build.gradle
buildTypes {
release {
minifyEnabled true
shrinkResources true
crunchPngs false // Paste this line
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Creating the .apk of an application all the resources are analyzed, if some resource has an incorrect format or is corrup you will get the message:
.png failed to read PNG signature: file does not start with PNG
signature
Be sure to have the correct resources, you can check it by opening your file.
This occurs due to the incorrect extension given to the image. For example, the image is jpg and the extension given is png.
Solutions:
Add { cruncherEnabled = false } in build.gradle
Convert the image into png rather than just changing the extension.
I had the same issue, solved by converting the images to webP from Android studio
right-click on the image -> tap on Convert to WebP
the conversion will handle file formating and also reduce the size of the PNG
This is a result of wrong file type indeed and can be fixed by saving the files in correct format.
I ran into this issue with PhoneGap Build and wanted to leave my 2 cents for any other PhoneGap / Cordova user who might also run into this.
My problem was that I upgraded my old PhoneGap 6.x to 8.0 and it seems behaviour of the default splash has changed. It used to require jpg as a default splash, but on 8.0 it results in this error on production builds.
This worked before:
<splash src="www/splash.jpg" />
Now you have 2 options. Replace the default splash with png like this:
<splash src="www/res/screen/android/screen-xhdpi-portrait.png" />
Other option is to remove default splash entirely.
For a quick fix ,Make shrinkResources to false instead of true in app.gradle
Hope this will help.
if Not then also add this aaptOptions { cruncherEnabled = false }
i have same error for slow i do some step:
open abc_btn_switch_to_on_mtrl_00001.9.png (Image)file in paint
now select png and save as and re-save in drawable and overweight in with old images and its work
If you don't have any transparent pixels, then renaming the file to .jpg worked for me.
You might have used a JPEG file or some other image file format.
Use PNG images to solve the error.
Drawable directory can only have png type of images.
If you are using an online platform to generate App Icon(launcher_icon), then use PNG Format or First convert JPEG to PNG and then used APP ICON GENERATOR
In my case also there was a jpg file instead of png, so I changed my extension and solve my problem.
Open [Root_project]/app/build.gradle and add following lines.
release {
...
crunchPngs false // or true
lintOptions {
checkReleaseBuilds false
abortOnError false
}
...
}
if you are facing this issue in Flutter while buildings apk then add crunchPngs false
android {
buildTypes {
release {
...
crunchPngs false
}
}
into android/app/build.gradle
I'm not sure if it's a valid solution, but deleting only the file mentioned in the error message helped. While deleting the folder, containing it didn't.
I've had the same issue. To solve it just restart your Android Studio and build the gradle file again.
You can add code in node_modules/react-native/react.gradle. After doFirst
doLast {
def moveFunc = { resSuffix ->
File originalDir = file("$buildDir/generated/res/react/release/${resSuffix}");
if (originalDir.exists()) {
File destDir = file("$buildDir/../src/main/res/${resSuffix}");
ant.move(file: originalDir, tofile: destDir);
}
}
moveFunc.curry("drawable-ldpi").call()
moveFunc.curry("drawable-mdpi").call()
moveFunc.curry("drawable-hdpi").call()
moveFunc.curry("drawable-xhdpi").call()
moveFunc.curry("drawable-xxhdpi").call()
moveFunc.curry("drawable-xxxhdpi").call()
moveFunc.curry("raw").call()
}
check it if it not fix issue after remove drawable folder
github enter link description here
I was able to fix it permanently.
I created a new blank expo project, then I added a new app icon (correct png format), then I ejected from expo.
I then copied the mipmap-* folders from the blank project to my real project. It all worked perfectly.
I was getting the same error when signing APK.
This solution fixed the problem:
build.gradle(:app):
...
aaptOptions {
cruncherEnabled = false
}
...
You can check for more: https://developer.android.com/studio/build/optimize-your-build
You can open the images with windows win10 'Paint',
then you can save as .png and overrite it,
It will solve the problem
Gradle build failing with this error:
Error:C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature
Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
Error:Execution failed for task ':app:mergeDebugResources'.
Error: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
aapt2 compile --legacy -o C:\dev\workspace\android2\MatrixCalculator\app\build\intermediates\res\merged\debug C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png
Issues:
- ERROR: C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature
Some basic things i've tried to solve this issue:
Invalidate caches/restart
Deleting gradle folder
It's likely a JPG renamed to a PNG file, not an actual PNG file
The problem could be because of the wrong extension of images.
In my case, the file was a JPEG image but it was saved as PNG not converted to. In this situation change extension to the real one and convert to PNG then retry.
For instance, you have ic_logo.png but it's actually a JPG image.
You should rename it back to ic_logo.jpg and use something like Photoshop to convert the image to PNG format.
android {
buildTypes {
release {
crunchPngs false // or true
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
crunchPngs false // or true
lintOptions {
checkReleaseBuilds false
abortOnError false
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Steps
In build.gradle
aaptOptions {
cruncherEnabled = false
}
Delete content inside C:\Users\.gradle\caches (or ~/.gradle/caches for Mac and Linux)
Restart Android Studio
Failed to read PNG signature - Just put 1 line in your build.gradle
buildTypes {
release {
minifyEnabled true
shrinkResources true
crunchPngs false // Paste this line
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Creating the .apk of an application all the resources are analyzed, if some resource has an incorrect format or is corrup you will get the message:
.png failed to read PNG signature: file does not start with PNG
signature
Be sure to have the correct resources, you can check it by opening your file.
This occurs due to the incorrect extension given to the image. For example, the image is jpg and the extension given is png.
Solutions:
Add { cruncherEnabled = false } in build.gradle
Convert the image into png rather than just changing the extension.
I had the same issue, solved by converting the images to webP from Android studio
right-click on the image -> tap on Convert to WebP
the conversion will handle file formating and also reduce the size of the PNG
This is a result of wrong file type indeed and can be fixed by saving the files in correct format.
I ran into this issue with PhoneGap Build and wanted to leave my 2 cents for any other PhoneGap / Cordova user who might also run into this.
My problem was that I upgraded my old PhoneGap 6.x to 8.0 and it seems behaviour of the default splash has changed. It used to require jpg as a default splash, but on 8.0 it results in this error on production builds.
This worked before:
<splash src="www/splash.jpg" />
Now you have 2 options. Replace the default splash with png like this:
<splash src="www/res/screen/android/screen-xhdpi-portrait.png" />
Other option is to remove default splash entirely.
For a quick fix ,Make shrinkResources to false instead of true in app.gradle
Hope this will help.
if Not then also add this aaptOptions { cruncherEnabled = false }
i have same error for slow i do some step:
open abc_btn_switch_to_on_mtrl_00001.9.png (Image)file in paint
now select png and save as and re-save in drawable and overweight in with old images and its work
If you don't have any transparent pixels, then renaming the file to .jpg worked for me.
You might have used a JPEG file or some other image file format.
Use PNG images to solve the error.
Drawable directory can only have png type of images.
If you are using an online platform to generate App Icon(launcher_icon), then use PNG Format or First convert JPEG to PNG and then used APP ICON GENERATOR
In my case also there was a jpg file instead of png, so I changed my extension and solve my problem.
Open [Root_project]/app/build.gradle and add following lines.
release {
...
crunchPngs false // or true
lintOptions {
checkReleaseBuilds false
abortOnError false
}
...
}
if you are facing this issue in Flutter while buildings apk then add crunchPngs false
android {
buildTypes {
release {
...
crunchPngs false
}
}
into android/app/build.gradle
I'm not sure if it's a valid solution, but deleting only the file mentioned in the error message helped. While deleting the folder, containing it didn't.
I've had the same issue. To solve it just restart your Android Studio and build the gradle file again.
You can add code in node_modules/react-native/react.gradle. After doFirst
doLast {
def moveFunc = { resSuffix ->
File originalDir = file("$buildDir/generated/res/react/release/${resSuffix}");
if (originalDir.exists()) {
File destDir = file("$buildDir/../src/main/res/${resSuffix}");
ant.move(file: originalDir, tofile: destDir);
}
}
moveFunc.curry("drawable-ldpi").call()
moveFunc.curry("drawable-mdpi").call()
moveFunc.curry("drawable-hdpi").call()
moveFunc.curry("drawable-xhdpi").call()
moveFunc.curry("drawable-xxhdpi").call()
moveFunc.curry("drawable-xxxhdpi").call()
moveFunc.curry("raw").call()
}
check it if it not fix issue after remove drawable folder
github enter link description here
I was able to fix it permanently.
I created a new blank expo project, then I added a new app icon (correct png format), then I ejected from expo.
I then copied the mipmap-* folders from the blank project to my real project. It all worked perfectly.
I was getting the same error when signing APK.
This solution fixed the problem:
build.gradle(:app):
...
aaptOptions {
cruncherEnabled = false
}
...
You can check for more: https://developer.android.com/studio/build/optimize-your-build
You can open the images with windows win10 'Paint',
then you can save as .png and overrite it,
It will solve the problem
I am getting images id from drawable-hdpi at run time using getIdentifier method using following code
mContext.getResources().getIdentifier(mContext.getPackageName() + ":drawable/" + mCur.getString(mCur.getColumnIndex(Constant.COLUMN_IMAGE_DRAWABLE)), null, null)
i also tried following ways
mContext.getResources().getIdentifier(mCur.getString(mCur.getColumnIndex(Constant.COLUMN_IMAGE_DRAWABLE))+"", "drawable", mContext.getPackageName());
and set image resource using following code
imgPosotion.setImageResource(item.getDrawable());
both work when i turn on instant run from android studio but unfortunately none of them work when i am turn off instant run feature from android studio.
I am using Proguard at debug as well as release time using following code
release {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Observed scenario
Image are not showing. Code run perfect and showing images when run with instant run.
i make release build and then decompile it, i observed that images(.png extension) are there in res/drawable-hdpi folder but sizes are converted into 1*1 dimension(67byte) and when i tried to open just a dot are visible and when i tried to open jpg(converted to 0byte size) files they are not opened.
Expected scenario
Image must be visible
Action taken to solved
Make the image size small
convert images into from .png to jpg
I stuck to this problem so please help me. Appreciate, if anyone can help to troubleshoot.
nobody takes this question seriously but after do lots of type stuff i got the solution. Actually problem was with proguard, i don't known why but it works after set false to shrinkResources
release {
..
minifyEnabled true
shrinkResources false
}
It`s the bad practce, you should use official google strategy as described here https://developer.android.com/studio/build/shrink-code#keep-resources
release {
..
minifyEnabled true
shrinkResources false
}
worked fr me
My android application file size is huge, I've used Proguard and ImageOptim to reduce the size of images and code which isn't being used.
This is the buildtypes in my gradle:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
I will also delete the mockable-android-jar before submitting.
I have also seen that the intermediates folder is big in size, I also cleaned it by deleting the extra png images which were not being used by the application (i.e: g+ png's and so on.)
What can I do which may help me shrink down this apk.
Edit: Made the following edits, which resulted intermediates to become 12.5 MB
android {
defaultConfig {
...
resConfigs "en"
}
Removes resources which are not meant for the english language.
Extract your apk and check which folder is bigger in size.
Or you can use "Android Studio" Also to analyze your apk file :
Reduce your image files size and use vector drawables wherever possible.
Go through the following guides on apk size reduce :
https://medium.com/google-developers/smallerapk-part-1-anatomy-of-an-apk-da83c25e7003#.nlid8yvdb
https://medium.com/google-developers/smallerapk-part-2-minifying-code-554560d2ed40#.bayyfe7zd
https://medium.com/google-developers/smallerapk-part-3-removing-unused-resources-1511f9e3f761#.igywlhtem
https://medium.com/#wkalicinski/smallerapk-part-4-multi-apk-through-abi-and-density-splits-477083989006#.9fgm8mryi
https://medium.com/#wkalicinski/smallerapk-part-5-multi-apk-through-product-flavors-e069759f19cd#.ngg4dfe8i
https://medium.com/#wkalicinski/smallerapk-part-6-image-optimization-zopfli-webp-4c462955647d#.elxsq9tcc
https://medium.com/#wkalicinski/smallerapk-part-7-image-optimization-shape-and-vectordrawables-ed6be3dca3f#.m0p9rrl4t
https://medium.com/#wkalicinski/smallerapk-part-8-native-libraries-open-from-apk-fc22713861ff#.mcytfj9y3
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
}
}