This article is a summary of APK — Dex hotfix and Classpath

In running Java files directly in JVM and Dalvik, Java programs are explained how to run in JVM and Dalvik. In the Android part, Java files are first converted into DEX files, imported into Android phones, and compiled and run through Dalvik. Implement the Java file running in the mobile phone function. This article focuses on how to use this function to achieve a simple thermal repair effect. The basic principle of hot repair is to modify the dex order of dexPathList in the DexClassLoader to achieve the effect of hot repair.

1. Make a dex with bugs

First create a class with a printing bug called test.java as follows:

package xiaohan;
public class Test {
	public  void test (){
		System.out.println("This a BUG!"); }}Copy the code

Note here that since this class needs to be referenced in a class with main, you need to define a package otherwise the class will not be referenced properly.

Create a main class helloWorld.java with main and a reference to Test, imported as import.

import  xiaohan.Test;
public class HelloWorld {
	public static void main (String [] args){
		Test test= new Test(); test.test(); }}Copy the code

Since you need to load two Java files, you need to execute the following statement. Note the “after cp. Modifier of the.

javac -cp . HelloWorld.java Test.java
Copy the code

dx --dex --output=xiaohan.dex HelloWorld.class Test.class
Copy the code

The following error occurs:

E:\ProgramFiles\Android\build-tools\28.0.3>dx --dex --output=xiaohan.dex helloWorld.class test.class PARSE ERROR: class name (xiaohan/Test) does not match path (Test.class) ... while parsing Test.class 1 error; abortingCopy the code

The reason is that when you create the test. Java file, you specify the package name xiaohan, which is not in the same package as HelloWorld. Therefore, you need to create a folder xiaohan, copy the test. class into it, and execute the following statement:

dx --dex --output=xiaohan.dex HelloWorld.class xiaohan/Test.class
Copy the code

A xiaohan.dex file will be generated, copied to the phone, and run the following statement:

dalvikvm -cp xiaohan.dex HelloWorld
Copy the code

The results are as follows:

2. Create patch packages

Create a new test.java file and modify it as follows:

package xiaohan;
public class Test {
	public  void test (){
		System.out.println("Fix a BUG!"); }}Copy the code

Create a new class file. Note that you only need to create the test. class file, not the HelloWorld. , including HelloWorld because an error is reported at compile time.

javac -cp HelloWorld.java Test.java
Copy the code

dx --dex --output=new.dex  xiaohan/Test.class
Copy the code

The dex is to make the patch package, only contains the test.class file, does not contain the helloWorld. class file.

Push the generated new.dex file to the directory where helloworld. dex is stored on the mobile phone and execute the repaired statement, as shown below.

dalvikvm -cp new.dex:xiaohan.dex  HelloWorld
Copy the code

Note that the patch package needs to be placed before and then xiaohan.dex(dex file containing main) is added. Otherwise, if new. Dex is added first or not added, the repair effect cannot be achieved.

dalvikvm -cp xiaohan.dex:new.dex  HelloWorld
Copy the code

Android Learning – A step-by-step guide to implementing Android hotfixes