How to avoid 65k method limit while using Google P

2020-03-01 08:47发布

问题:

If you find yourself writing a big Android application that depends on many different libraries (which I would recommend instead of reinventing the wheel) it is very likely that you have already come across the 65k method limit of the Dalvik executable file classes.dex. Furthermore, if you depend on large libraries like the Google Play Services SDK which itself in already contained more than 20k methods in version 5.0 you are forced to use tricks like stripping packages or multidex support to avoid errors while packaging. With Android's new runtime ART which is publicly available since Android Lollipop multiple dex files are easier to handle, but currently developers are still forced to do method counting.

What is the simplest way to reduce your application`s method count while using Google Play Services?

回答1:

The biggest change for developers that came with the 6.5 release of the Google Play Services was probably the Granular Dependency Management. Google managed to split up it's library to allow developers to depend only on certain components which they really need for their apps.

Since version 6.5 developers are no longer forced to implement the complete Google Play Services library in their app, but can selectively depend on components like this:

compile 'com.google.android.gms:play-services-fitness:6.5.+'
compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.gogole.android.gms:play-services-maps:6.5.+'
...

If you want to compile the complete library into your app, you can still do so:

compile 'com.google.android.gms:play-services:6.5.+'

A complete list of available packages can be found on the Android Developers site.



回答2:

For someone who do not use Android Studio w/gradle or don't want to implement ProGuard to you project.

It's possible to avoid 65k while using google-play-services by keeping some packages that you really want to use by using jarjar. For example, in my project I want to implement only google map and google location I lean my google-play-services.jar like this.

  1. Download jarjar HERE

  2. Create new file call 'services.rules'

  3. edit it as follow

keep com.google.android.gms.maps.*

keep com.google.android.gms.location.*

  1. Copy your original google-play-services.jar / jarjar-1.4.jar / services.rules into the same folder

  2. start command prompt and type..

java -jar jarjar-1.4.jar process services.rules google-play-services.jar google-lean.jar

That's it you will get a new .jar that size was reduce a lot (method also)

use it instaed of google-play-services.jar and dex over 56k will gone.

Here is .jar that was lean already as mention above.



回答3:

I know this question is old but for those of you who face this issue while working on eclipse and cannot use the above answer

please follow the steps

if you don't want to download android studio to get the lib projects you can download lib files from here https://www.dropbox.com/s/1pf73czcn7pyqgi/com.google.android.gms.rar?dl=0 and skip to step 4

1 - first of all you still need android studio to download your dependencies you can download it from here

https://developer.android.com/sdk/index.html

2 - then in your build.gradle inside your app add the below lines

dependencies {
    compile 'com.google.android.gms:play-services-maps:7.5.0'
//map , gcm or any specific api for a hole list visit the below link
//https://developers.google.com/android/guides/setup
}

and then hit sync project with gradle file

after that you will get to lib projects

play-services-base play-services-maps

right click on them to get their path

4 - create project inside eclipse delete the generate files inside src folder , res folder and manifest

5- copy res and manifest from play-services-base to your project

6 - copy file inside play-services-base/jars to the libs folder of your project normally named classes.jar (please rename it to any other name so it won't conflict with other project)

7- add this jar to build paths then right click on project / properties / java build path / order and export tab check the added jar

8- right click on project / properties / android / check is lib

9- make the same steps for play-services-maps

10 - now you got to lib projects one is called googleBase and the other is called googleMaps (or any other name)

11 - add them to your project as libraries

now add the following lines to your manifest

<!-- Include required permissions for Google Maps API to run-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-feature
       android:glEsVersion="0x00020000"
       android:required="true"/> 


<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="" />

for a complete tutorial with images please refer to below link http://androidninja.quora.com/Prevent-65-K-Methods-Count-When-Using-Google-Lib-on-Android-with-eclipse-adt