2

I've just added location awareness to my app, based on the simple "get last known location" functionality described here, which uses the fused location provider.

I've been trying to keep my app as lightweight as possible, so it's disappointing that, in the process of adding simple location awareness, my apk size has gone from 185K to 2.1M. The actual code itself certainly hasn't increased by that amount, so I assume it's pulling in a big library from somewhere.

Any idea how I can reduce my apk size again?

Thanks.

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
  • google play services library keeps growing and growing and it will certainly increase your app size. Enable proguard as @Mike suggests, but the more classes from Google Play Services you use, the bigger your app will be. – rupps Dec 27 '14 at 12:43

2 Answers2

3

Couple things can help:

  1. Use ProGuard to strip out unused code - most folks leave this disabled for development builds, to avoid the extra build time, and enable for release builds.

  2. Use the new granular dependency management introduced in Play Services 6.5. You can select which of the Play Services components you need and exclude the others. From the documentation:

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app.

A list of the individual APIs you can include may be found here.

Community
  • 1
  • 1
stkent
  • 19,772
  • 14
  • 85
  • 111
  • Great answer. I tried (2) first, replacing "compile 'com.google.android.gms:play-services:6.5.+'" with "compile 'com.google.android.gms:play-services-location:6.5.+'" in the dependencies part of my build.gradle file. That reduced the sdk down to 1.5M... good but not brilliant. Then I tried (1) as well, replacing "minifyEnabled false" with "minifyEnabled true" in the buildTypes>release part of build.gradle. That took the sdk size down to 880K. Still a lot larger than my original 185K, but a lot better than 2.1M. Thanks! I guess that's about as low as I can go? – drmrbrewer Dec 27 '14 at 16:34
  • I think so, yes. There is another open question here on SO which indicates that including Play Services and then *not* using any of the APIs still doesn't allow ProGuard to strip out the entire Play Services code. I am not sure why that is. – stkent Dec 27 '14 at 17:54
2

This Problem should be solved when you are using proGuard before you publish your APP. Which i, and android strongly recommend you. proGuard comes with the android SDK and obfuscates(to make it harder to read after reverse-engineering your app) and shrinks your code. That means it kills dead code which you don't use and you are using right now just a small out of Google play service. The more classes you use, the bigger your app will be. Proguard can logically only cut off unused code. Hope it helps

You can read more about how to use proguard in android here: http://developer.android.com/tools/help/proguard.html

And what he does generally here: http://proguard.sourceforge.net/

Mike
  • 857
  • 1
  • 7
  • 11
  • Thanks for this answer. It is spot on, and I feel bad that I can only mark one answer as "correct", but stkent's answer had two suggestions, both of which helped to reduce the size in their own right. – drmrbrewer Dec 27 '14 at 16:36
  • But i was nearly one hour faster than him..... And i think you will anyway use the proguard so the other suggestion is not really a big thing because it does what proguard does automatically.... they only both "worked" because you did the second one first. If you did directly use the proguard you would have logically come on the same size as you are now... so i feel bad too now ;) @drmrbrewer – Mike Dec 27 '14 at 17:29
  • Using granular dependencies may save you from having to enable ProGuard for debug builds - otherwise, it will be forced when you hit the 64k method limit - which will speed up your build process while developing, so using both techniques together does have an advantage. – stkent Dec 27 '14 at 18:04
  • @stkent you are right it could be important if you really reach the Limit. Which will only happen if you are using large external libaries. But anyway then i would also hold on to this: http://developer.android.com/tools/building/multidex.html – Mike Dec 27 '14 at 18:12