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

时间:2014-09-01 14:22:13

标签: android android-gradle

我遇到了这个问题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"
    }

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

更新

感谢reVerse。这是非常有用的代码。有一个用于获取方法计数的脚本(也可以查看现有包的名称)https://gist.github.com/JakeWharton/6002797source ./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
...

在使用来自reVerse答案的代码片段后,包裹:广告,游戏等已被删除。

1 个答案:

答案 0 :(得分: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.+'

对于所有其他单独的Google Play服务API,请检查this page on d.android.com

注意:通常不鼓励使用+。截至目前,当前正确的版本为6.5.87。有关详细信息,请参阅official Blog-Post (click)


前段时间有一篇关于 Medium.com 的文章名为"[DEX] Sky’s the limit? No, 65K methods is"(绝对值得一读),它描述了一种剥离 Google Play服务,您可以找到here (google-play-services-strip-script)的shell脚本 虽然这是一个选项,但还有一个 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
    }
}

注意:这取自Gradle task to strip unused packages on Google Play Services library @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/**"

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

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