android检测apk及dex方法数

来源:互联网 发布:苏州seo大牛 编辑:程序博客网 时间:2024/05/16 07:47


原文:http://www.jianshu.com/p/366b3ae72be6


随着项目越来越大,android应用不得不面对64k方法数限制的问题,说真的,这个问题很头疼,虽然网上已经有了多种解决方案,multidex,dynamicApk,代码混淆,业务逻辑用c实现然后用jni调用等等,dynamicApk需要工程模块化,高度解耦,修改量极大;代码混淆,已经做了,治标不治本;业务逻辑用c实现然后用jni调用,技术不到家是一种,各种依赖包的添加才是方法数爆棚的罪魁祸首,作用不大;multidex,虽然解了燃眉之急,但仍感到忧心忡忡,不是长久之计,还有各种坑等着你跳。

问题预防,检测方法数

虽然目前还没有一劳永逸的方案,只能走一步算一步,避免使用一些方法数较多的库,时时刻刻意识到这问题,让它慢一点到来,做我们能做的事,哦弥陀佛。

使用dexcount-gradle-plugin 计算apk方法数

用法
在app/build.gradle添加

buildscript {    repositories {        mavenCentral() // or jcenter()    }    dependencies {        classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.3.0'    }}apply plugin: 'com.getkeepsafe.dexcount'

简单输出

> ./gradlew assembleDebug...buildspam...:app:compileDebugSources:app:preDexDebug UP-TO-DATE:app:dexDebug:app:packageDebug:app:zipalignDebug:app:assembleDebugTotal methods in MyApp-debug-5.3.14.apk: 56538BUILD SUCCESSFULTotal time: 33.017 secs

更多查看dexcount-gradle-plugin

使用dex-method-count计算单个dex方法数

解压apk文件可以看到classes.dex文件,如果使用了multidex可能会看到多个classses.dex,classes2.dex等。
这里使用了网上找的一个脚本,很早之前看到的,找不到出处了,贴出来

printhex.ps1

<#.SYNOPSISOutputs the number of methods in a dex file..PARAMETER PathSpecifies the path to a file. Wildcards are not permitted.#>param(  [parameter(Position=0,Mandatory=$TRUE)]    [String] $Path)if ( -not (test-path -literalpath $Path) ) {  write-error "Path '$Path' not found." -category ObjectNotFound  exit}$item = get-item -literalpath $Path -forceif ( -not ($? -and ($item -is [System.IO.FileInfo])) ) {  write-error "'$Path' is not a file in the file system." -category InvalidType  exit}if ( $item.Length -gt [UInt32]::MaxValue ) {  write-error "'$Path' is too large." -category OpenError  exit}$stream = [System.IO.File]::OpenRead($item.FullName)$buffer = new-object Byte[] 2$stream.Position = 88$bytesread = $stream.Read($buffer, 0, 2)$output = $buffer[0..1] #("{1:X2} {0:X2}") -f $output$outputdec = $buffer[1]*256 + $buffer[0]"Number of methods is " + $outputdec$stream.Close()

dex-method-count.bat

@ECHO OFFIF "%1"=="" GOTO MissingFileNameErrorIF EXIST "%1" (GOTO ContinueProcessing) ELSE (GOTO FileDoesntExist):ContinueProcessingset FileNameToProcess=%1set FileNameForDx=%~n1.dexIF "%~x1"==".dex" GOTO ProcessWithPowerShellREM preprocess Jar with dxIF "%~x1"==".jar" (    ECHO Processing Jar %FileNameToProcess% with DX!    CALL dx --dex --output=%FileNameForDx% %FileNameToProcess%    set FileNameToProcess=%FileNameForDx%    IF ERRORLEVEL 1 GOTO DxProcessingError):ProcessWithPowerShellECHO Counting methods in DEX file %FileNameToProcess%CALL powershell -noexit -executionpolicy bypass "& ".\printhex.ps1" %FileNameToProcess%GOTO End:MissingFileNameError@ECHO Missing filename for processingGOTO End:DxProcessingError@ECHO Error processing file %1% with dx!GOTO End:FileDoesntExist@ECHO File %1% doesn't exist!GOTO End:End

用法:
将上面两个文件拷贝到电脑(windows),文件名字和后缀都不要变,且放在同一个目录下,需要检测的dex文件也放在同一级目录,就像这样


QQ截图20151116174811.png


打开命令行并切到当前目录下,运行命令

dex-method-count.bat classes.dex

运行结果:Number of methods is 64595


QQ截图20151116175052.png

计算开源库方法数的网站

http://www.methodscount.com/
感谢Razerdp提供



文/dongjunkun(简书作者)
原文链接:http://www.jianshu.com/p/366b3ae72be6

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。



0 0
原创粉丝点击