Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java windows上的Android jar/dex方法计数_Java_Android_Windows - Fatal编程技术网

Java windows上的Android jar/dex方法计数

Java windows上的Android jar/dex方法计数,java,android,windows,Java,Android,Windows,我已经看到了一些在Linux和MacOS上计算方法的链接,但是我没有看到任何适用于Windows的链接。如何计算.dex或.jar文件中的方法数?在搜索解决方案失败后,我编写了两个简单的批处理/shell脚本来实现这一点 第一个是methodcount.bat,它检查文件是.dex还是.jar,如果是.jar文件,它用dx将其处理为dex文件,然后调用第二个是printhex.ps1,它实际检查dex文件中的方法数-它读取从88开始的2个字节(小端)并将它们转换为十进制数 要使用此选项,您需要在

我已经看到了一些在Linux和MacOS上计算方法的链接,但是我没有看到任何适用于Windows的链接。如何计算.dex或.jar文件中的方法数?

在搜索解决方案失败后,我编写了两个简单的批处理/shell脚本来实现这一点

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

要使用此选项,您需要在路径中的某个位置安装dx(它位于android SDK build tools/xx.x.x文件夹中)并安装PowerShell(它应该已经安装在Windows 7/8上)

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

以下是脚本,但您也可以在gist上找到它们:

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

param(
[参数(位置=0,强制=TRUE)]
[字符串]$Path
)
if(-not(测试路径-literalpath$path)){
写入错误“未找到路径“$Path”。-类别ObjectNotFound
出口
}
$item=get item-literalpath$Path-force
if(-not($?-和($item-是[System.IO.FileInfo])){
写入错误“%$Path”不是文件系统中的文件。”-类别InvalidType
出口
}
如果($item.Length-gt[UInt32]::MaxValue){
写入错误“$Path”太大。“-类别开头错误
出口
}
$stream=[System.IO.File]::OpenRead($item.FullName)
$buffer=新对象字节[]2
$stream.Position=88
$bytesread=$stream.Read($buffer,0,2)
$output=$buffer[0..1]
#(“{1:X2}{0:X2}”)-f$输出
$outputdec=$buffer[1]*256+$buffer[0]
“方法数为”+$outputdec
$stream.Close()

我知道这个问题很老了,但是有一个Gradle插件可以在Windows上运行,它会在每次构建时在APK中报告方法引用计数:。

很好,非常感谢,通过这种方式,我可以在多个JAR中检查我的Android应用程序方法的批处理方法计数。我建议在这样的批处理循环中调用
printhex.ps1
而不调用
-noexit
选项:
@echo“START”>mc_res.txt\n@for/F“tokens=*”%%t in(mc.txt)do(\n call methodcount.bat%%t>>mc res txt\n)\n@remmc.txt包含jar文件名的列表\n'表示换行符
此Gradle插件易于使用,并在APK中报告方法总数。遗憾的是,它没有报告进入APK的JAR文件中的计数。所以这有效地回答了OP的DEX的一半,但不是JAR的一半。这是真的,但离题了。谈论一个jar的方法计数是没有意义的——当多个jar作为dx的输入时,方法引用被合并,有时在索引期间被删除,因此类文件和dex方法引用之间没有一对一的映射。re:
谈论jar中的方法计数是没有意义的
一个没有意义的点。我提到这个解决方案并没有解决JAR文件,只是因为OP询问了JAR文件。