Android熱修復技術——QQ空間補丁方案解析(3)

如前文所述,要想實現熱更新的目的,就必須在dex分包完成之後操作字節碼文件。比較常用的字節碼操作工具有ASM和javaassist。相比之下ASM提供一系列字節碼指令,效率更高但是要求使用者對字節碼操作有一定了解。而javaassist雖然效率差一些但是使用門檻較低,本文選擇使用javaassist。關於javaassist可以參考Java 編程的動態性, 第四部分: 用 Javassist 進行類轉換

正常App開發過程中,編譯,打包過程都是Android Studio自動完成。如無特殊需求無需人爲干預,但是要實現插樁就必須在Android Studio的自動化打包流程中加入插樁的過程。

1. Gradle,Task,Transform,Plugin

Android Studio採用Gradle作爲構建工具,所有有必要了解一下Gradle構建的基本概念和流程。如果不熟悉可以參考一下下列文章:
- Gradle學習系列之一——Gradle快速入門
- 深入理解Android之Gradle

Gradle的構建工程實質上是通過一系列的Task完成的,所以在構建apk的過程中就存在一個打包dex的任務。Gradle 1.5以上版本提供了一個新的API:Transform,官方文檔對於Transform的描述是:

The goal of this API is to simplify injecting custom class manipulations without having to deal with tasks, and to offer more flexibility on what is manipulated. The internal code processing (jacoco, progard, multi-dex) have all moved to this new mechanism already in 1.5.0-beta1.

    1. The Dex class is gone. You cannot access it anymore through the variant API (the getter is still there for now but will throw an exception)
    1. Transform can only be registered globally which applies them to all the variants. We’ll improve this shortly.
    1. There’s no way to control ordering of the transforms.

Transform任務一經註冊就會被插入到任務執行隊列中,並且其恰好在dex打包task之前。所以要想實現插樁就必須創建一個Transform類的Task。

1.1 Task

Gradle的執行腳本就是由一系列的Task完成的。Task有一個重要的概念:input的output。每一個task需要有輸入input,然後對input進行處理完成後在輸出output。

1.2 Plugin

Gradle的另外一個重要概念就是Plugin。整個Gradle的構建體系都是有一個一個的plugin構成的,實際Gradle只是一個框架,提供了基本task和指定標準。而具體每一個task的執行邏輯都定義在一個個的plugin中。詳細的概念可以參考:Writing Custom Plugins
在Android開發中我們經常使用到的plugin有:”com.android.application”,”com.android.library”,”java”等等。
每一個Plugin包含了一系列的task,所以執行gradle腳本的過程也就是執行目標腳本所apply的plugin所包含的task。

1.3 創建一個包含Transform任務的Plugin

    1. 新建一個module,選擇library module,module名字必須叫BuildSrc
    1. 刪除module下的所有文件,除了build.gradle,清空build.gradle中的內容
    1. 然後新建以下目錄 src-main-groovy
    1. 修改build.gradle如下,同步
apply plugin: 'groovy'

repositories {
    jcenter()
}

dependencies {
    compile gradleApi()
    compile 'com.android.tools.build:gradle:1.5.0'
    compile 'org.javassist:javassist:3.20.0-GA'//javaassist依賴
}
    1. 像普通module一樣新建package和類,不過這裏的類是以groovy結尾,新建類的時候選擇file,並且以.groovy作爲後綴
    1. 自定義Plugin:
package com.hotpatch.plugin

import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.gradle.api.Project

public class PreDexTransform extends Transform {

    Project project;

    public PreDexTransform(Project project1) {
        this.project = project1;

        def libPath = project.project(":hack").buildDir.absolutePath.concat("/intermediates/classes/debug")
        println libPath
        Inject.appendClassPath(libPath)
        Inject.appendClassPath("/Users/liyazhou/Library/Android/sdk/platforms/android-24/android.jar")
    }
    @Override
    String getName() {
        return "preDex"
    }

    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS
    }

    @Override
    Set<QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        return false
    }

    @Override
    void transform(Context context, Collection<TransformInput> inputs, Collection<TransformInput> referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException {

        // 遍歷transfrom的inputs
        // inputs有兩種類型,一種是目錄,一種是jar,需要分別遍歷。
        inputs.each {TransformInput input ->
            input.directoryInputs.each {DirectoryInput directoryInput->

                //TODO 注入代碼
                Inject.injectDir(directoryInput.file.absolutePath)

                def dest = outputProvider.getContentLocation(directoryInput.name,
                        directoryInput.contentTypes, directoryInput.scopes, Format.DIRECTORY)
                // 將input的目錄複製到output指定目錄
                FileUtils.copyDirectory(directoryInput.file, dest)
            }

            input.jarInputs.each {JarInput jarInput->


                //TODO 注入代碼
                String jarPath = jarInput.file.absolutePath;
                String projectName = project.rootProject.name;
                if(jarPath.endsWith("classes.jar")
                        && jarPath.contains("exploded-aar/"+projectName)
                        // hotpatch module是用來加載dex,無需注入代碼
                        && !jarPath.contains("exploded-aar/"+projectName+"/hotpatch")) {
                    Inject.injectJar(jarPath)
                }

                // 重命名輸出文件(同目錄copyFile會衝突)
                def jarName = jarInput.name
                def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
                if(jarName.endsWith(".jar")) {
                    jarName = jarName.substring(0,jarName.length()-4)
                }
                def dest = outputProvider.getContentLocation(jarName+md5Name, jarInput.contentTypes, jarInput.scopes, Format.JAR)
                FileUtils.copyFile(jarInput.file, dest)
            }
        }
    }
}
  • 8.Inject.groovy, JarZipUtil.groovy
package com.hotpatch.plugin

import javassist.ClassPool
import javassist.CtClass
import org.apache.commons.io.FileUtils

public class Inject {

    private static ClassPool pool = ClassPool.getDefault()

    /**
     * 添加classPath到ClassPool
     * @param libPath
     */
    public static void appendClassPath(String libPath) {
        pool.appendClassPath(libPath)
    }

    /**
     * 遍歷該目錄下的所有class,對所有class進行代碼注入。
     * 其中以下class是不需要注入代碼的:
     * --- 1. R文件相關
     * --- 2. 配置文件相關(BuildConfig)
     * --- 3. Application
     * @param path 目錄的路徑
     */
    public static void injectDir(String path) {
        pool.appendClassPath(path)
        File dir = new File(path)
        if(dir.isDirectory()) {
            dir.eachFileRecurse { File file ->

                String filePath = file.absolutePath
                if (filePath.endsWith(".class")
                        && !filePath.contains('R$')
                        && !filePath.contains('R.class')
                        && !filePath.contains("BuildConfig.class")
                        // 這裏是application的名字,可自行配置
                        && !filePath.contains("HotPatchApplication.class")) {
                    // 應用程序包名,可自行配置
                    int index = filePath.indexOf("com/hotpatch/plugin")
                    if (index != -1) {
                        int end = filePath.length() - 6 // .class = 6
                        String className = filePath.substring(index, end).replace('\\', '.').replace('/','.')
                        injectClass(className, path)
                    }
                }
            }
        }
    }

    /**
     * 這裏需要將jar包先解壓,注入代碼後再重新生成jar包
     * @path jar包的絕對路徑
     */
    public static void injectJar(String path) {
        if (path.endsWith(".jar")) {
            File jarFile = new File(path)


            // jar包解壓後的保存路徑
            String jarZipDir = jarFile.getParent() +"/"+jarFile.getName().replace('.jar','')

            // 解壓jar包, 返回jar包中所有class的完整類名的集合(帶.class後綴)
            List classNameList = JarZipUtil.unzipJar(path, jarZipDir)

            // 刪除原來的jar包
            jarFile.delete()

            // 注入代碼
            pool.appendClassPath(jarZipDir)
            for(String className : classNameList) {
                if (className.endsWith(".class")
                        && !className.contains('R$')
                        && !className.contains('R.class')
                        && !className.contains("BuildConfig.class")) {
                    className = className.substring(0, className.length()-6)
                    injectClass(className, jarZipDir)
                }
            }

            // 從新打包jar
            JarZipUtil.zipJar(jarZipDir, path)

            // 刪除目錄
            FileUtils.deleteDirectory(new File(jarZipDir))
        }
    }

    private static void injectClass(String className, String path) {
        CtClass c = pool.getCtClass(className)
        if (c.isFrozen()) {
            c.defrost()
        }
        def constructor = c.getConstructors()[0];
        constructor.insertAfter("System.out.println(com.hotpatch.hack.AntilazyLoad.class);")
        c.writeFile(path)
    }

}
package com.hotpatch.plugin

import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry

/**
 * Created by hp on 2016/4/13.
 */
public class JarZipUtil {

    /**
     * 將該jar包解壓到指定目錄
     * @param jarPath jar包的絕對路徑
     * @param destDirPath jar包解壓後的保存路徑
     * @return 返回該jar包中包含的所有class的完整類名類名集合,其中一條數據如:com.aitski.hotpatch.Xxxx.class
     */
    public static List unzipJar(String jarPath, String destDirPath) {

        List list = new ArrayList()
        if (jarPath.endsWith('.jar')) {

            JarFile jarFile = new JarFile(jarPath)
            Enumeration<JarEntry> jarEntrys = jarFile.entries()
            while (jarEntrys.hasMoreElements()) {
                JarEntry jarEntry = jarEntrys.nextElement()
                if (jarEntry.directory) {
                    continue
                }
                String entryName = jarEntry.getName()
                if (entryName.endsWith('.class')) {
                    String className = entryName.replace('\\', '.').replace('/', '.')
                    list.add(className)
                }
                String outFileName = destDirPath + "/" + entryName
                File outFile = new File(outFileName)
                outFile.getParentFile().mkdirs()
                InputStream inputStream = jarFile.getInputStream(jarEntry)
                FileOutputStream fileOutputStream = new FileOutputStream(outFile)
                fileOutputStream << inputStream
                fileOutputStream.close()
                inputStream.close()
            }
            jarFile.close()
        }
        return list
    }

    /**
     * 重新打包jar
     * @param packagePath 將這個目錄下的所有文件打包成jar
     * @param destPath 打包好的jar包的絕對路徑
     */
    public static void zipJar(String packagePath, String destPath) {

        File file = new File(packagePath)
        JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(destPath))
        file.eachFileRecurse { File f ->
            String entryName = f.getAbsolutePath().substring(packagePath.length() + 1)
            outputStream.putNextEntry(new ZipEntry(entryName))
            if(!f.directory) {
                InputStream inputStream = new FileInputStream(f)
                outputStream << inputStream
                inputStream.close()
            }
        }
        outputStream.close()
    }
}
    1. 在app module下build.gradle文件中添加新插件:apply plugin: com.hotpatch.plugin.Register

2. 創建hack.jar

創建一個單獨的module,命名爲com.hotpatch.plugin.AntilazyLoad:

package com.hotpatch.plugin
public class AntilazyLoad {
}

使用上一篇博客介紹的方法打包hack.jar。然後將hack.jar複製到app module下的assets目錄中。另外注意:app module不能依賴hack module。之所以要創建一個hack module,同時人爲地在dex打包過程中插入對其他hack.jar中類的依賴,就是要讓apk文件在安裝的時候不被打上CLASS_ISPREVERIFIED標記。
另外由於hack.jar位於assets中,所以必須要在加載patch_dex之前加載hack.jar。另外由於加載其他路徑的dex文件都是在Application.onCreate()方法中執行的,此時還沒有加載hack.jar,所以這就是爲什麼在上一章節插樁的時候不能在Application中插樁的原因。

插樁的過程介紹完了,整個熱修復的過程也就差不多了,讀者可以參考完整的代碼進行demo試用:Hotpatch Demo

發佈了81 篇原創文章 · 獲贊 19 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章