Android中每个dex文件的方法限制为64K

Vla*_*mir 8 android android-gradle-plugin

我遇到了这个问题java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536,我决定从dex文件中排除一些方法.我的gradle.build:

compile ('com.google.android.gms:play-services:+') {
        exclude group: "com.google.android.gms.analytics"
        exclude group: "com.google.android.gms.games"
        exclude group: "com.google.android.gms.plus"
        exclude group: "com.google.android.gms.drive"
        exclude group: "com.google.android.gms.ads"
    }
Run Code Online (Sandbox Code Playgroud)

我认为这段代码是错误的,因为有错误method ID not in [0, 0xffff]....如何排除Google Play服务中不必要的部分?我只使用地图和GCM.

更新.

谢谢reVerse.这是非常有用的代码.有一个用于获取方法计数的脚本(也可以看到现有包的名称)https://gist.github.com/JakeWharton/6002797(source ./dex.sh; dex-method-count-by-package test.apk)

在使用reVerse的答案代码片段之前

Count of methods / Package
...
22484   com.google.android.gms
2   com.google.android.gms.actions
578 com.google.android.gms.ads
152 com.google.android.gms.ads.doubleclick
25  com.google.android.gms.ads.identifier
86  com.google.android.gms.ads.internal
86  com.google.android.gms.ads.internal.rawhtmlad
86  com.google.android.gms.ads.internal.rawhtmlad.client
88  com.google.android.gms.ads.mediation
4   com.google.android.gms.ads.mediation.admob
73  com.google.android.gms.ads.mediation.customevent
26  com.google.android.gms.ads.purchase
118 com.google.android.gms.ads.search
...
858 com.google.android.gms.games.internal.api
43  com.google.android.gms.games.internal.constants
8   com.google.android.gms.games.internal.data
31  com.google.android.gms.games.internal.events
9   com.google.android.gms.games.internal.experience
215 com.google.android.gms.games.internal.game
56  com.google.android.gms.games.internal.multiplayer
23  com.google.android.gms.games.internal.notification
80  com.google.android.gms.games.internal.player
86  com.google.android.gms.games.internal.request
...
Run Code Online (Sandbox Code Playgroud)

使用reVerse答案的代码片段后,包裹:广告,游戏等被删除.

reV*_*rse 9

更新 - Google Play Services 6.5(12-08-14)

在6.5版本中,Google最终拆分了Google Play服务.因此,从现在开始,可以有选择地将API编译到您的可执行文件中.

示例(仅使用AdMob和Android Wear API)

compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.google.android.gms:play-services-ads:6.5.+'
Run Code Online (Sandbox Code Playgroud)

对于所有其他个人Google Play服务API,请访问d.android.com上的此页面.

注意:使用+一般气馁.截至目前,目前正确的版本将是6.5.87.有关更多信息,请参阅官方Blog-Post(单击).


前一段时间还有的是对文章Medium.com称为"[DEX]天空的极限吗?不,65K方法是"(绝对值得一读),它描述了一种剥离谷歌播放服务与shell脚本,你可以在这里找到(google-play-services-strip-script).
虽然这是一个选项,但也有一个gradle任务为您执行此操作:

def toCamelCase(String string) {
    String result = ""
    string.findAll("[^\\W]+") { String word ->
        result += word.capitalize()
    }
    return result
}

afterEvaluate { project ->
    Configuration runtimeConfiguration = project.configurations.getByName('compile')
    ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
// Forces resolve of configuration
    ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion

    String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
    File playServiceRootFolder = project.tasks.find { it.name.equals(prepareTaskName) }.explodedDir

    Task stripPlayServices = project.tasks.create(name: 'stripPlayServices', group: "Strip") {
        inputs.files new File(playServiceRootFolder, "classes.jar")
        outputs.dir playServiceRootFolder
        description 'Strip useless packages from Google Play Services library to avoid reaching dex limit'

        doLast {
            copy {
                from(file(new File(playServiceRootFolder, "classes.jar")))
                into(file(playServiceRootFolder))
                rename { fileName ->
                    fileName = "classes_orig.jar"
                }
            }
            tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
                destinationDir = playServiceRootFolder
                archiveName = "classes.jar"
                from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
  ----->                // Specify what should be removed
                }
            }.execute()
            delete {
                delete (file(new File(playServiceRootFolder, "classes_orig.jar")))
            }
        }
    }

    project.tasks.findAll { it.name.startsWith('prepare') && it.name.endsWith('Dependencies') }.each { Task task ->
        task.dependsOn stripPlayServices
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这取自Gradle任务,用于删除Google Play服务库@GitHubGist上未使用的包

你的相关部分是箭头所在的位置task.create(...).您需要指定应删除哪些部分.所以在你的情况下,只需在那里写下这样的东西:

exclude "com/google/ads/**"
exclude "com/google/android/gms/analytics/**"
exclude "com/google/android/gms/games/**"
exclude "com/google/android/gms/panorama/**"
exclude "com/google/android/gms/plus/**"
exclude "com/google/android/gms/drive/**"
exclude "com/google/android/gms/ads/**"
exclude "com/google/android/gms/wallet/**"
exclude "com/google/android/gms/wearable/**"
Run Code Online (Sandbox Code Playgroud)

这将删除除Maps-和GCM-Part之外的所有内容.

注意:要使用它,只需将gradle-task的内容复制到build.gradleapp模块文件的底部即可.