查看apk方法及apk分包处理

查看apk方法数量

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

dex-method-count.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

运行

dex-method-count.bat classes.dex

其中classes.dex为apk解压中的文件,将上述两个文件与classes.dex放到同一个目录。

apk分包处理

导入multidex包,设置为支持多dex输出模式

build.gradle

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex' // enable multidex

        // optional
        // dx.additionalParameters += "--main-dex-list=$projectDir/<filename>".toString() // enable the main-dex-list
    }
}

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

在Application类添加

import android.support.multidex.MultiDex;

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}