13

First of all, don't suggest me official documentation or this. When we build an app from Android Studio with predefined textview that simply displays "hello world", it generates an APK with more than 1.2MB. But some apps on playstore is available under 500KB with lot of features. I already applied all features of Android Studio like proguard, minifyEnabled etc. but that does not help. So, how can i achieve that level of compression?

Rohit Singh
  • 411
  • 3
  • 15
  • 1
    Considering most phones are several GB of storage, are you really concerned about sizes on the order of KB? – OneCricketeer Feb 06 '18 at 08:47
  • 1
    by default it includes support libs into the project. Just create a blank project **without** activities, and it won't include the support lib. – Vladyslav Matviienko Feb 06 '18 at 08:56
  • @cricket_007 i am talking about pre-installation size of APK. It doesn't matter how our phones display that. – Rohit Singh Feb 06 '18 at 09:44
  • @VladMatvienko bro, i already mention that some apps on playstore have lot of features under 1mb. Then, compiling and building an APK is look like off-topic talk. – Rohit Singh Feb 06 '18 at 09:48
  • 3
    I suggest you reading [this article](https://fractalwrench.co.uk/posts/playing-apk-golf-how-low-can-an-android-app-go/). – Exerion Feb 06 '18 at 10:52

4 Answers4

8

Points to reduce APK size:
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

Sanjay Kumar
  • 1,135
  • 14
  • 27
7

When create new project from Android studio, the IDE automatically add some dependencies to your project.

dependencies {
    // ...
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // implementation 'com.android.support:appcompat-v7:26.1.0'
    // implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    // ...
}

If you bundle support library and constraint layout library, your release apk size will increase rapidly even you don't add any code.

You can get a very small apk if you remove support library and constraint layout library and just use all the system api.

After remove the support library, you should

  1. use Activity rather than AppCompatActivity, YourActivity extends Activity

  2. do not use any theme from support library in your AndroidManifest.xml

  3. use FrameLayout to replace ConstraintLayout in your layout

alijandro
  • 11,627
  • 2
  • 58
  • 74
6

The problem might be in the support library. Adding com.android.support:appcompat-v7:27.0.2 to an empty project increased the APK size from 80 KB to 1.3 MB. So if your minSDK is high enough and you don't need its helper classes, you can get rid of it.

You can use the APK Analyzer to see what actually takes the space.

makovkastar
  • 5,000
  • 2
  • 30
  • 50
-2

Thanks for posting alijandro, makovkastar and Sanjay Manjoka guys. After reading your answers and struggling with the internet I found that:

  1. Remove unnecessary auto-produced libraries from build.app(Module: app) like ConstraintLayout, appcompat, junit etc.
  2. Use resConfig "en" in build.gradle under defaultConfig block.
  3. Use shrinkResources true in build.gradle under buildTypes's release block.
  4. Use minifyEnabled true in build.gradle under buildTypes's release block.
  5. Use aaptOptions { cruncherEnabled = false } to compress images
  6. Use webP format for large images
  7. Avoid enumerations and use @IntDef annotation
  8. Goto Build>Analyze APK and press OK. It shows the status of our app like an image below. Analyze APK
Rohit Singh
  • 411
  • 3
  • 15
  • 1
    don't post answers that just repeat what others already said. Instead you should upvote the existing answers and editing them if you want to add something – Tim Feb 06 '18 at 11:30
  • @TimCastelijns thanks for the suggestion. Everyone's answer is not complete and I found something useful in that. – Rohit Singh Feb 06 '18 at 11:37