Android jar / dex方法依赖于Windows

时间:2014-09-14 16:23:33

标签: java android windows

我已经看到了一些用于计算Linux和MacOS上的方法的链接,但我还没有看到任何针对Windows的内容。你如何计算.dex或.jar文件中的许多方法?

2 个答案:

答案 0 :(得分:13)

在搜索解决方案失败后,我编写了两个简单的批处理/ shell脚本来执行此操作。

第一个,methodcount.bat,检查文件是.dex还是.jar,如果它是.jar文件,它用dx处理它到dex文件然后调用第二个,printhex.ps1,那个实际上检查dex文件中的方法数 - 它读取从88开始的2个字节(小端)并将它们转换为十进制数。

要使用此功能,您可以在路径中的某个位置安装dx(在android SDK build-tools / xx.xx文件夹中)并安装PowerShell(它应该已经安装在Windows 7/8上)。 / em>的

用法非常简单: methodcount.bat filename.dex | filename.jar

以下是脚本,但您也可以在gist上找到它们:https://gist.github.com/mrsasha/9f24e129ced1b1db791b

<强> methodcount.bat

@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

<强> printhex.ps1

<#
.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() 

答案 1 :(得分:1)

我看到这个问题已经过时了,但有一个Gradle插件可以在Windows上运行,它将在每个版本的APK中报告方法引用计数:https://github.com/KeepSafe/dexcount-gradle-plugin