我们都知道Android应用的dex文件有64k方法数的限制,但具体怎么来查看我们现在的应用中到底有多少多少方法数了呢,常用的方法有两种:
1.<u> dexcount-gradle-plugin计算apk方法数</u>
2. <u>dex-method-count计算单个dex方法数</u>
方法1详细步骤访问dexcount-gradle-plugin
方法2则使用
<u>printhex.ps1文件</u>
<#
.SYNOPSIS
Outputs the number of methods in a dex file.
.PARAMETER Path
Specifies 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 -force
if ( -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()
<u>dex-method-count.bat文件</u>
@ECHO OFF
IF "%1"=="" GOTO MissingFileNameError
IF EXIST "%1" (GOTO ContinueProcessing) ELSE (GOTO FileDoesntExist)
:ContinueProcessing
set FileNameToProcess=%1
set FileNameForDx=%~n1.dex
IF "%~x1"==".dex" GOTO ProcessWithPowerShell
REM preprocess Jar with dx
IF "%~x1"==".jar" (
ECHO Processing Jar %FileNameToProcess% with DX!
CALL dx --dex --output=%FileNameForDx% %FileNameToProcess%
set FileNameToProcess=%FileNameForDx%
IF ERRORLEVEL 1 GOTO DxProcessingError
)
:ProcessWithPowerShell
ECHO Counting methods in DEX file %FileNameToProcess%
CALL powershell -noexit -executionpolicy bypass "& ".\printhex.ps1" %FileNameToProcess%
GOTO End
:MissingFileNameError
@ECHO Missing filename for processing
GOTO End
:DxProcessingError
@ECHO Error processing file %1% with dx!
GOTO End
:FileDoesntExist
@ECHO File %1% doesn't exist!
GOTO End
:End
将上面两个文件放到一个文件夹中,文件名和后缀都不变,然后把需要检测的dex文件也放在同一级目录,(dex文件通过打包的解压apk文件可以得到,如果压缩文件不识别apk文件,可以把apk文件改成zip或rar等文件再进行解压),使用命令行进入该目录,使用
dex-method-count classes.dex
就可以得到方法数。
可知上面apk的方法数是49763。