检查Android Studio中有多少种方法

Tom*_*mCB 17 dex android-studio

看起来我正在调用65K的dex限制,如果我写一些新的方法我有一个dex错误,如果我删除一些旧的东西,它就消失了.

有没有办法检查Android Studio中目前使用的方法数量?

Chr*_*ght 8

Android Studio提供了一种内置方法。切换到“项目”视图,导航到/ app / build / outputs / apk / 路径以“ favour / type apk”。双击APK。这将在编辑器中启动apk,您可以在窗口的上半部分看到各种库及其关联的大小。在此窗口中向下滚动,直到找到classes.dex(或者,如果有多个,则为classes2.dex,等等)。单击此按钮,在窗口底部,您将看到按包列出的方法引用。如果您使用多种样式或构建类型,则还可以使用此视图来分析构建的Android清单。可以在android开发者网站上找到更多信息

在此处输入图片说明


Sco*_*rta 7

我可以找到一种脆弱的方式去做,这可能比完全没有办法好.将以下内容复制并粘贴到模块的build.gradle文件的底部,替换ANDROID_HOME为Android SDK安装的路径,并BUILD_TOOLS_VERSION使用块中buildToolsVersion规范中指定的相同版本android:

buildscript {
    dependencies {
        classpath files("/Users/sbarta/sdk/build-tools/21.0.2/lib/dx.jar")
    }
}

android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        variant.assemble.doLast {
            // Show the dex count after the assemble task is finished
            showDexCount(
                    [filename: output.outputFile],
            )
        }
    }
}

def showDexCount(Map... files) {
    def maxReferences = (int) Math.pow(2, 16);
    def buffer = 5000 // that's for safety, because you can't burn maxReferences

    println "\n\n***********************************************************************************"
    println "* DEX COUNTS                                                                      *"
    println "***********************************************************************************"
    files.each {
        def dex = new com.android.dex.Dex(it.filename)
        def count = dex.tableOfContents.methodIds.size
        if ((maxReferences - count - buffer) >= 0)
            println String.format('* %1$5d                 (there are still %2$5d references to burn...)             *',
                    count, maxReferences - count - buffer)
        else
            println String.format('* %1$5d  !!!WARNING!!!  Too many references, please decrease by %2$4d!             *',
                    count, -(maxReferences - count - buffer))
    }
    println "***********************************************************************************\n"
}
Run Code Online (Sandbox Code Playgroud)

This loads up the dex code itself to evaluate the dex files and count the number of methods; it adds its work to the end of the assemble task in the build script, so you'll see it in command line builds or if you actually run it from Android Studio (where it will show up in the Gradle console).

I tried to make it more resilient and use the ANDROID_HOME environment variable instead of requiring you to hardcode the path, but using environment variables when building from Android Studio is problematic (it works from the command line though). Similarly, I tried to have it pull in the build tools version from the other place in the build script where it's being referenced, and I also tried defining a global constant, but couldn't make the scoping and order of execution work. If someone can improve on this, please comment or edit the answer.

This is adapted from something written by Carlos Sobrinho; I can't find a Web-accessible reference to the original.

  • https://plus.google.com/+JahirFiquitivaJDev/posts/8VXJzyLUiYs我在这里找到了Jahir主题 (3认同)

Gak*_*ket 7

我使用两种不同的工具来查找方法的数量:

1)简单的工具,计算方法并显示不同包中的方法数量:https: //github.com/mihaip/dex-method-counts

它会向您显示如下的简单数据:

Read in 65490 method IDs.
<root>: 65490
    : 3
    android: 6837
        accessibilityservice: 6
        bluetooth: 2
        content: 248
            pm: 22
            res: 45
        ...
    com: 53881
        adjust: 283
            sdk: 283
        codebutler: 65
            android_websockets: 65
        ...
    Overall method count: 65490
Run Code Online (Sandbox Code Playgroud)

2)这是另一个更容易使用的工具,你必须为你的项目添加一个依赖项,它将显示每个构建的方法.您可以在不需要时简单地注释此依赖项:https: //github.com/KeepSafe/dexcount-gradle-plugin

methods  fields   package/class name
5037     1103     android.support.v4
29       1        android.support.v4.accessibilityservice
57       16       android.support.v4.animation
Run Code Online (Sandbox Code Playgroud)