最全从零开始分析InstantRun源码,四面美团开发岗

完结

Redis基于内存,常用作于缓存的一种技术,并且Redis存储的方式是以key-value的形式。Redis是如今互联网技术架构中,使用最广泛的缓存,在工作中常常会使用到。Redis也是中高级后端工程师技术面试中,面试官最喜欢问的问题之一,因此作为Java开发者,Redis是我们必须要掌握的。

Redis 是 NoSQL 数据库领域的佼佼者,如果你需要了解 Redis 是如何实现高并发、海量数据存储的,那么这份腾讯专家手敲《Redis源码日志笔记》将会是你的最佳选择。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

git clone https://android.googlesource.com/platform/tools/base

在3.5版本的Android Studio之后,Google使用了新的apply change架构代替了instant run,所以最新的代码中看不到,需要切换到studio-3.2.1这个tag,最新的apply change使用时有诸多限制

  • 您使用调试构建变体来构建应用的 APK。
  • 您将应用部署到搭载 Android 8.0(API 级别 26)或更高版本的目标设备或模拟器上。

#####需要重启应用(不是重启Activity)才能实现的代码更改

某些代码和资源更改必须在重启应用之后才能应用,其中包括以下更改:

  • 添加或删除方法或字段
  • 更改方法签名
  • 更改方法或类的修饰符
  • 更改类继承行为
  • 更改枚举中的值
  • 添加或移除资源
  • 更改应用清单
  • 更改原生库(SO 文件)

所以我感觉这玩意以后可以用来在修改代码逻辑的时候使用,使用的范围非常有限。

知乎上也有人提问Android Studio3.5提供的Apply Changes是什么原理?

这里引用weishu大佬的回答,“猜测是使用JVMTI实现的,JVMTI 的全称是 JVM Tool Interface。它是 Java 虚拟机(ART)实现的一部分,包含了虚拟机中线程 / 内存 / 类 / 方法 / 变量 / 事件 / 定时器处理等等 20 多类功能。比如:内存控制和对象获取、线程和锁、调试功能。
对这个「Apply Changes」来说,比较重要的应该是 ClassTransform 和 ClassRedefine;它允许虚拟机在运行时动态修改类(Redefine只在9.0上实现了)。比如说 Activity 这个 class,你可以通过此接口在字节码层面往里面直接添加方法/修改方法,然后虚拟机会为你重新加载这个类,之后这个被改过的类就是原来那个货真价值的 Activity 类。所以,这个技术跟 Instant Run/Robust 编译期字节码编织 / ClassLoader 替换 / AndFix 方法替换那种动态修改完全不是一个层面的东西,这是 运行时动态字节码编织

另一个需要下载的库中有Android Studio相关的源代码,其中可以看到AS是如何配合instant-run工作的,需要切换到studio-3.2.1这个tag

git clone https://android.googlesource.com/platform/tools/adt/idea

我们可以在build.gradle中添加一行代码,查看启动gradle的命令和全部参数

println getGradle().getStartParameter()

我在3.4.2的Android Studio中看到 projectProperties={android.optional.compilation=INSTANT_DEV,这里就表示开启instant-run支持了

StartParameter{taskRequests=[DefaultTaskExecutionRequest{args=[:app:assembleDebug],projectPath=‘null’}], excludedTaskNames=[], currentDir=F:\GitAndroid\RxDemo, searchUpwards=true, projectProperties={android.optional.compilation=INSTANT_DEV, android.injected.build.density=xxhdpi, android.injected.coldswap.mode=MULTIAPK, android.injected.build.api=28, android.injected.invoked.from.ide=true, android.injected.build.abi=arm64-v8a,armeabi-v7a,armeabi, android.injected.restrict.variant.name=debug, android.injected.restrict.variant.project=:app}, systemPropertiesArgs={}, gradleUserHomeDir=C:\Users\Jackie.gradle, gradleHome=C:\Users\Jackie.gradle\wrapper\dists\gradle-4.4-all\9br9xq1tocpiv8o6njlyu5op1\gradle-4.4, logLevel=LIFECYCLE, showStacktrace=INTERNAL_EXCEPTIONS, buildFile=null, initScripts=[], dryRun=false, rerunTasks=false, recompileScripts=false, offline=false, refreshDependencies=false, parallelProjectExecution=false, configureOnDemand=false, maxWorkerCount=8, buildCacheEnabled=false, interactive=false}:app:buildInfoDebugLoader

在3.6.0的Android Studio中就看不到了

StartParameter{taskRequests=[DefaultTaskExecutionRequest{args=[:app:assembleDebug],projectPath=‘null’}], excludedTaskNames=[], currentDir=/Users/jackie/Desktop/WorkPlace/AndroidWorkPlace/MyApplication2, searchUpwards=true, projectProperties={android.injected.build.density=xhdpi, android.injected.build.api=29, android.injected.invoked.from.ide=true, android.injected.build.abi=x86}, systemPropertiesArgs={idea.active=true, idea.version=3.6}, gradleUserHomeDir=/Users/jackie/.gradle, gradleHome=/Users/jackie/.gradle/wrapper/dists/gradle-5.6.4-all/ankdp27end7byghfw1q2sw75f/gradle-5.6.4, logLevel=LIFECYCLE, showStacktrace=INTERNAL_EXCEPTIONS, buildFile=null, initScripts=[], dryRun=false, rerunTasks=false, recompileScripts=false, offline=false, refreshDependencies=false, parallelProjectExecution=false, configureOnDemand=false, maxWorkerCount=12, buildCacheEnabled=false, interactive=false, writeDependencyLocks=false}app: ‘annotationProcessor’ dependencies won’t be recognized as kapt annotation processors. Please change the configuration name to ‘kapt’ for these artifacts: ‘com.alibaba:arouter-compiler:1.2.2’.

到了android.gradle插件的执行逻辑里,会被转成如下枚举定义,分别表示不同的编译类型:

//源码路径 gradle3.0.0版本
//Users/jackie/Desktop/WorkPlace/InstantRun/base/build-system/builder-、model/src/main/java/com/android/builder/model/OptionalCompilationStep.java

/**

  • enum describing possible optional compilation steps. This can be used to turn on java byte code
  • manipulation in order to support instant reloading, or profiling, or anything related to
  • transforming java compiler .class files before they are processed into .dex files.
    */
    public enum OptionalCompilationStep {

/**

  • presence will turn on the InstantRun feature.
    /
    INSTANT_DEV,
    /
    *
  • Force rebuild of cold swap artifacts.
  • Dex files and/or resources.ap_ for ColdswapMode.MULTIDEX and some split APKs for

  • ColdswapMode.MULTIAPK.
    /
    RESTART_ONLY,
    /
    *
  • Force rebuild of fresh install artifacts.
  • A full apk for ColdswapMode.MULTIDEX and all the split apks for ColdswapMode.MULTIAPK.

*/
FULL_APK,
}

####Gradle4.1的Instant Run

源码分析是基于Gradle4.1版本研究的Instant Run,但是这个版本的Instant Run功能已经削减很多了,下面还会介绍其他版本的Gradle

运行后反编译app-debug.apk会找到多个dex(一般是两个),一开始是通过dex2jar-2.0和jd-gui,但是有时候有些方法无法进行反编译而是依旧显示初始的字节码,很不方便阅读,后来使用了jadx-gui进行直接反编译apk,使用很方便,但依旧还是会有些方法还是显示字节码,所以我是两者交叉着看,但是有时候甚至两者都是字节码,只能上网上直接找别人的博客代码了。

因为我研究的版本是基于Gradle4.1的,仅仅剩下可怜的热插拔和处理资源补丁,而且我还找不到intant-run.zip了,所以我找不到项目中的代码了,不在dex文件中,原本这玩意解压apk之后就有了,所以暂时只能在build下的目录里面寻找了,后面再看看这些文件时如何弄到apk当中。

InstantRunContentProvider的onCreate方法中初始化Socket

public boolean onCreate() {
if (isMainProcess()) { //只支持主进程
Log.i(“InstantRun”, “starting instant run server: is main process”);
Server.create(getContext());
return true;
}
Log.i(“InstantRun”, “not starting instant run server: not main process”);
return true;
}

然后启动一个socket监听Android Studio推送的消息

private class SocketServerThread extends Thread {
private SocketServerThread() {}

public void run() {
while (true) {
try {
LocalServerSocket localServerSocket = Server.this.serverSocket;
if (localServerSocket == null)
return;
LocalSocket localSocket = localServerSocket.accept();
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Received connection from IDE: spawning connection thread”);
(new Server.SocketServerReplyThread(localSocket)).run();
if (wrongTokenCount > 50) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Stopping server: too many wrong token connections”);
Server.this.serverSocket.close();
return;
}
} catch (Throwable throwable) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Fatal error accepting connection on local socket”, throwable);
}
}
}
}

然后在SocketServerReplyThread的run方法值接受数据并处理

//处理补丁
private int handlePatches(List paramList, boolean paramBoolean, int paramInt) {
if (paramBoolean)
FileManager.startUpdate();
for (ApplicationPatch applicationPatch : paramList) {
String str = applicationPatch.getPath();
if (str.equals(“classes.dex.3”)) { //如果有classes.dex.3处理热插拔
paramInt = handleHotSwapPatch(paramInt, applicationPatch);
continue;
}
if (isResourcePath(str))
//处理资源补丁
paramInt = handleResourcePatch(paramInt, applicationPatch, str);
}
if (paramBoolean)
FileManager.finishUpdate(true);
return paramInt;
}

这里先来看看ApplicationPatch是什么

public static List read(DataInputStream paramDataInputStream) throws IOException {
int j = paramDataInputStream.readInt();
if (Log.logging != null && Log.logging.isLoggable(Level.FINE))
Log.logging.log(Level.FINE, “Receiving " + j + " changes”);
ArrayList arrayList = new ArrayList(j);
for (int i = 0; i < j; i++) {
String str = paramDataInputStream.readUTF();
byte[] arrayOfByte = new byte[paramDataInputStream.readInt()];
paramDataInputStream.readFully(arrayOfByte);
arrayList.add(new ApplicationPatch(str, arrayOfByte));
}
return arrayList;
}

可以看到ApplicationPatch是从Socket接收到的数据输入流中调用readFully来读取的,关于readFully的使用while循环判断byte数组是否已经读满所有数据,如果没有读满则继续读取补充直到读满为止,从而改善输入流出现空档,造成read方法直接跳出的问题。即通过缓冲来保证数量的完整,也算是常用的一种方法。所以以后若要读取特定长度的数据,使用readFully读取更加安全。

#####1.处理热插拔

下面来看看是如何处理热插拔的

//处理热插拔
private int handleHotSwapPatch(int paramInt, ApplicationPatch paramApplicationPatch) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Received incremental code patch”);
try {
//创建或获取“data/data/applicationid/files/instant-run/dex”文件路径
String str1 = FileManager.writeTempDexFile(paramApplicationPatch.getBytes());
if (str1 == null) {
Log.e(“InstantRun”, “No file to write the code to”);
return paramInt;
}
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, "Reading live code from " + str1);

String str2 = FileManager.getNativeLibraryFolder().getPath();
//反射构造AppPatchesLoaderImpl实例
Class<?> clazz = Class.forName(“com.android.tools.fd.runtime.AppPatchesLoaderImpl”, true, (ClassLoader)new DexClassLoader(str1, this.context.getCacheDir().getPath(), str2, getClass().getClassLoader()));
try {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, "Got the patcher class " + clazz);
PatchesLoader patchesLoader = (PatchesLoader)clazz.newInstance();
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, "Got the patcher instance " + patchesLoader);
//获取热修复所要替换的类的classname
String[] arrayOfString = (String[])clazz.getDeclaredMethod(“getPatchedClasses”, new Class[0]).invoke(patchesLoader, new Object[0]);
if (Log.isLoggable(“InstantRun”, 2)) {
Log.v(“InstantRun”, "Got the list of classes ");
int j = arrayOfString.length;
for (int i = 0; i < j; i++) {
String str = arrayOfString[i];
Log.v(“InstantRun”, "class " + str);
}
}
//执行AppPatchesLoaderImpl的load方法进行类修复
boolean bool = patchesLoader.load();
if (!bool)
paramInt = 3;
} catch (Exception exception) {}
} catch (Throwable throwable) {
Log.e(“InstantRun”, “Couldn’t apply code changes”, throwable);
paramInt = 3;
}
return paramInt;
}
//AppPatchesLoaderImpl实现抽象类AbstractPatchesLoaderImpl,重写getPatchedClasses方法,重写方法中有提供需要热更新的类
public abstract class AbstractPatchesLoaderImpl implements PatchesLoader {
public abstract String[] getPatchedClasses();

public boolean load() {
try {
//《《《《《《最关键的方法,一个个替换类中要替换的方法》》》》》》
for (String str : getPatchedClasses()) {
ClassLoader classLoader = getClass().getClassLoader();
Object object1 = classLoader.loadClass(str + “ o v e r r i d e " ) . n e w I n s t a n c e ( ) ; F i e l d f i e l d = c l a s s L o a d e r . l o a d C l a s s ( s t r ) . g e t D e c l a r e d F i e l d ( " override").newInstance(); Field field = classLoader.loadClass(str).getDeclaredField(" override").newInstance();Fieldfield=classLoader.loadClass(str).getDeclaredField("change”);
field.setAccessible(true);
Object object2 = field.get(null);
if (object2 != null) {
object2 = object2.getClass().getDeclaredField(“$obsolete”);
if (object2 != null)
object2.set(null, Boolean.valueOf(true));
}
field.set(null, object1);
if (Log.logging != null && Log.logging.isLoggable(Level.FINE))
Log.logging.log(Level.FINE, String.format(“patched %s”, new Object[] { str }));
}
} catch (Exception exception) {
if (Log.logging != null)
Log.logging.log(Level.SEVERE, String.format(“Exception while patching %s”, new Object[] { “foo.bar” }), exception);
return false;
}
return true;
}
}
//AppPatchesLoaderImpl继承AbstractPatchesLoaderImpl,真正的实现类,这里我们只要求change的MainActivity这个类里面的方法
public class AppPatchesLoaderImpl extends AbstractPatchesLoaderImpl {
public static final long BUILD_ID = 1597285889481L;

public AppPatchesLoaderImpl() {
}

public String[] getPatchedClasses() {
return new String[]{“com.example.jackie.instantrundemo.MainActivity”};
}
}

其中DexClassLoader的构造方法定义

//dexPath:被解压的apk路径,不能为空。
//optimizedDirectory:解压后的.dex文件的存储路径,不能为空。这个路径强烈建议使用应用程序的私有路径,不要放到sdcard上,否则代码容易被注入攻击。
//libraryPath:os库的存放路径,可以为空,若有os库,必须填写。
//parent:父类加载器,一般为context.getClassLoader(),使用当前上下文的类加载器。
DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent)

我们的MainActivity会被修改成这样

public class MainActivity extends AppCompatActivity {
public static final long serialVersionUID = 2158910920756968252L;

//重写空构造方法,方便于替换该方法的实现
public MainActivity() {
IncrementalChange var1 = KaTeX parse error: Expected '}', got 'EOF' at end of input: …t[])var1.accessdispatch("init a r g s . ( [ L c o m / e x a m p l e / j a c k i e / i n s t a n t r u n d e m o / M a i n A c t i v i t y ; [ L j a v a / l a n g / O b j e c t ; ) L j a v a / l a n g / O b j e c t ; " , n e w O b j e c t [ ] n u l l , n e w O b j e c t [ 0 ] ) ; O b j e c t [ ] v a r 2 = ( O b j e c t [ ] ) v a r 10001 [ 0 ] ; t h i s ( v a r 10001 , ( I n s t a n t R e l o a d E x c e p t i o n ) n u l l ) ; v a r 2 [ 0 ] = t h i s ; v a r 1. a c c e s s args.([Lcom/example/jackie/instantrundemo/MainActivity;[Ljava/lang/Object;)Ljava/lang/Object;", new Object[]{null, new Object[0]}); Object[] var2 = (Object[])var10001[0]; this(var10001, (InstantReloadException)null); var2[0] = this; var1.access args.([Lcom/example/jackie/instantrundemo/MainActivity;[Ljava/lang/Object;)Ljava/lang/Object;",newObject[]null,newObject[0]);Object[]var2=(Object[])var10001[0];this(var10001,(InstantReloadException)null);var2[0]=this;var1.accessdispatch(“init$body.(Lcom/example/jackie/instantrundemo/MainActivity;[Ljava/lang/Object;)V”, var2);
} else {
super();
}
}

public void onCreate(Bundle savedInstanceState) {
IncrementalChange var2 = KaTeX parse error: Expected '}', got 'EOF' at end of input: …) { var2.accessdispatch(“onCreate.(Landroid/os/Bundle;)V”, new Object[]{this, savedInstanceState});
} else {
super.onCreate(savedInstanceState);
this.setContentView(2130968603);
if(this.test(30) > 20333005) {
Log.d(“jackie”, “4444099994sf=dd=ddecf999=abc”);
} else {
Log.d(“jackie”, “==999999999999=”);
}

byte b = 0;
Toast.makeText(this, “hellodd4fdddd”, 1).show();
Log.d(“jackie”, “=d=666dd=ddddabc” + b);
}
}

public int test(int a) {
IncrementalChange var2 = KaTeX parse error: Expected '}', got 'EOF' at end of input: …ber)var2.accessdispatch(“test.(I)I”, new Object[]{this, new Integer(a)})).intValue();
} else {
byte age = 100;
int b = 300189 + age;

for(int i = 0; i < b + 9; ++i) {
a += b;
}

return 20 + a;
}
}
//增加类的构造方法,方便与修改类的任何构造方法
MainActivity(Object[] var1, InstantReloadException var2) {
String var3 = (String)var1[1];
switch(var3.hashCode()) {
case -2089128195:
super();
return;
case 173992496:
this();
return;
default:
throw new InstantReloadException(String.format(“String switch could not find ‘%s’ with hashcode %s in %s”, new Object[]{var3, Integer.valueOf(var3.hashCode()), “com/example/jackie/instantrundemo/MainActivity”}));
}
}
}

在MainActivity中做个小修改,点击小闪电执行Instant Run,可以看到build下面文件夹4000中会找一个MainActivity$override.class和AppPatchesLoaderImpl.class

要替换的MainActivity$override实现了IncrementalChange,从这里面进行方法的替换,所有的方法都会被替换,因为change值不为空

public class MainActivityKaTeX parse error: Expected '}', got 'EOF' at end of input: …ic MainActivityoverride() {
}

public static Object init$args(MainActivity[] var0, Object[] var1) {
Object[] var2 = new Object[]{new Object[]{var0, new Object[0]}, “android/support/v7/app/AppCompatActivity.()V”};
return var2;
}

public static void init$body(MainActivity KaTeX parse error: Expected '}', got 'EOF' at end of input: …etPrivateField(this, new Integer(100), MainActivity.class, “cmd”);
}

public static void onCreate(MainActivity KaTeX parse error: Expected '}', got 'EOF' at end of input: …Activity.accesssuper($this, “onCreate.(Landroid/os/Bundle;)V”, var2);
t h i s . s e t C o n t e n t V i e w ( 2130968603 ) ; i f ( this.setContentView(2130968603); if( this.setContentView(2130968603);if(this.test(30) > 20333005) {
Log.d(“jackie”, “44440999940sf=dd=ddecf999=abc”);
} else {
Log.d(“jackie”, “==999999999999=”);
}

byte b = 0;
//因为修改了全局变量的值,所以进行处理
AndroidInstantRuntime.setPrivateField( t h i s , n e w I n t e g e r ( ( ( N u m b e r ) A n d r o i d I n s t a n t R u n t i m e . g e t P r i v a t e F i e l d ( this, new Integer(((Number)AndroidInstantRuntime.getPrivateField( this,newInteger(((Number)AndroidInstantRuntime.getPrivateField(this, MainActivity.class, “cmd”)).intValue() + 100), MainActivity.class, “cmd”);
Toast.makeText($this, “hellodd4fdddd”, 1).show();
Log.d(“jackie”, “=d=666dd=ddddabc” + b);
}

public static int test(MainActivity KaTeX parse error: Expected '}', got 'EOF' at end of input: …etPrivateField(this, MainActivity.class, “cmd”)).intValue();
int b = 300189 + ageabc;

for(int i = 0; i < b + 9; ++i) {
a += b;
}

return 20 + a;
}

//替换相对应的方法,最后两个方法我估计是前面MainActivity类里面的copy
public Object accessKaTeX parse error: Expected '}', got 'EOF' at end of input: …88: return initargs((MainActivity[])var2[0], (Object[])var2[1]);
case 1043612718:
init$body((MainActivity)var2[0], (Object[])var2[1]);
return null;
default:
throw new InstantReloadException(String.format(“String switch could not find ‘%s’ with hashcode %s in %s”, new Object[]{var1, Integer.valueOf(var1.hashCode()), “com/example/jackie/instantrundemo/MainActivity”}));
}
}
}

#####2.处理资源补丁(温插拔)

下面来看看是如何处理资源补丁的,但是设计到资源的处理需要重启当前界面,我们先来看看重启App这种状况下的逻辑

//Server类的handle()
case 5:
if (authenticate(param1DataInputStream)) {
Activity activity1 = Restarter.getForegroundActivity(Server.this.context);
if (activity1 != null) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Restarting activity per user request”);
Restarter.restartActivityOnUiThread(activity1);
}
continue;
}
return;
case 1:
if (authenticate(param1DataInputStream)) {
List list = ApplicationPatch.read(param1DataInputStream);
if (list != null) {
bool = Server.hasResources(list);
i = param1DataInputStream.readInt();
//处理acitivity或者把资源写到文件中
i = Server.this.handlePatches(list, bool, i);
boolean bool1 = param1DataInputStream.readBoolean();
param1DataOutputStream.writeBoolean(true);
//重启Activity
Server.this.restart(i, bool, bool1);
}
continue;
}

我们先来看看handlePatches里面的handleResourcePatch是如何处理资源的

//Server.class
//处理资源补丁
private static int handleResourcePatch(int paramInt, ApplicationPatch paramApplicationPatch, String paramString) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Received resource changes (” + paramString + “)”);
FileManager.writeAaptResources(paramString, paramApplicationPatch.getBytes());
return Math.max(paramInt, 2);
}
//FileManager.class
//writeAaptResources
public static void writeAaptResources(String paramString, byte[] paramArrayOfbyte) {
File file1 = getResourceFile(getWriteFolder(false)); //获取资源文件
File file2 = file1.getParentFile();
if (!file2.isDirectory() && !file2.mkdirs()) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, "Cannot create local resource file directory " + file2);
return;
}
//把内容写到resources.ap_资源中
if (paramString.equals(“resources.ap_”)) {
writeRawBytes(file1, paramArrayOfbyte);
return;
}
//把raw资源写入
writeRawBytes(file1, paramArrayOfbyte);
}
//getWriteFolder
public static File getWriteFolder(boolean paramBoolean) {
String str;
if (leftIsActive()) {
str = “right”;
} else {
str = “left”;
}
File file = new File(getDataFolder(), str);
if (paramBoolean && file.exists()) {
delete(file);
if (!file.mkdirs())
Log.e(“InstantRun”, "Failed to create folder " + file);
}
return file;
}
//getResourceFile
private static File getResourceFile(File paramFile) { return new File(paramFile, “resources.ap_”); }

//writeRawBytes
public static boolean writeRawBytes(File paramFile, byte[] paramArrayOfbyte) {
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(paramFile));
try {
bufferedOutputStream.write(paramArrayOfbyte);
bufferedOutputStream.flush();
return true;
} finally {
bufferedOutputStream.close();
}
} catch (IOException iOException) {
Log.wtf(“InstantRun”, "Failed to write file, clean project and rebuild " + paramFile, iOException);
throw new RuntimeException(String.format("InstantRun could not write file %1$s, clean project and rebuild ", new Object[] { paramFile }));
}
}
public static String writeTempDexFile(byte[] paramArrayOfbyte) {
File file = getTempDexFile();
if (file != null) {
writeRawBytes(file, paramArrayOfbyte);
return file.getPath();
}
Log.e(“InstantRun”, “No file to write temp dex content to”);
return null;
}

下面来看看Server.this.restart(i, bool, bool1)是如何处理的,不必拘泥于细节是如何启动的,在里面找到一行关键代码

MonkeyPatcher.monkeyPatchExistingResources(this.context, str, list);

具体实现如下

public static void monkeyPatchExistingResources(Context context, String externalResourceFile, Collection activities) {
Collection<WeakReference> references;
if (externalResourceFile != null) {
//通过反射获取AssetManager
AssetManager newAssetManager = AssetManager.class.getConstructor(new Class[0]).newInstance(new Object[0]);
Method mAddAssetPath = AssetManager.class.getDeclaredMethod(“addAssetPath”, new Class[]{String.class});
mAddAssetPath.setAccessible(true);
//将当前的资源文件路径添加到AssetManager中
if (((Integer) mAddAssetPath.invoke(newAssetManager, new Object[]{externalResourceFile})).intValue() == 0) {
throw new IllegalStateException(“Could not create new AssetManager”);
}
Method mEnsureStringBlocks = AssetManager.class.getDeclaredMethod(“ensureStringBlocks”, new Class[0]);
mEnsureStringBlocks.setAccessible(true);
//进行资源初始化StringBlock对象
mEnsureStringBlocks.invoke(newAssetManager, new Object[0]);
if (activities != null) {
for (Activity activity : activities) {
Resources resources = activity.getResources();
try {
Field mAssets = Resources.class.getDeclaredField(“mAssets”);
mAssets.setAccessible(true);
mAssets.set(resources, newAssetManager);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
Resources.Theme theme = activity.getTheme();
try {
Field ma = Resources.Theme.class.getDeclaredField(“mAssets”);
ma.setAccessible(true);
ma.set(theme, newAssetManager);
} catch (NoSuchFieldException e2) {
Field themeField = Resources.Theme.class.getDeclaredField(“mThemeImpl”);
themeField.setAccessible(true);
Object impl = themeField.get(theme);
Field ma2 = impl.getClass().getDeclaredField(“mAssets”);
ma2.setAccessible(true);
ma2.set(impl, newAssetManager);
} catch (Throwable e3) {
Log.e(Logging.LOG_TAG, "Failed to update existing theme for activity " + activity, e3);
}
Field mt = ContextThemeWrapper.class.getDeclaredField(“mTheme”);
mt.setAccessible(true);
mt.set(activity, (Object) null);
Method mtm = ContextThemeWrapper.class.getDeclaredMethod(“initializeTheme”, new Class[0]);
mtm.setAccessible(true);
mtm.invoke(activity, new Object[0]);
if (Build.VERSION.SDK_INT < 24) {
Method mCreateTheme = AssetManager.class.getDeclaredMethod(“createTheme”, new Class[0]);
mCreateTheme.setAccessible(true);
Object internalTheme = mCreateTheme.invoke(newAssetManager, new Object[0]);
Field mTheme = Resources.Theme.class.getDeclaredField(“mTheme”);
mTheme.setAccessible(true);
mTheme.set(theme, internalTheme);
}
pruneResourceCaches(resources);
}
}
//获取当前JVM中的ResourcesManager的final ArrayMap<ResourcesKey, WeakReference > mActiveResources
if (Build.VERSION.SDK_INT >= 19) {
Class<?> resourcesManagerClass = Class.forName("android.app.ResourcesManager"); Method mGetInstance = resourcesManagerClass.getDeclaredMethod("getInstance", new Class[0]); mGetInstance.setAccessible(true); Object resourcesManager = mGetInstance.invoke((Object) null, new Object[0]); try { Field fMActiveResources = resourcesManagerClass.getDeclaredField("mActiveResources"); fMActiveResources.setAccessible(true); references = ((ArrayMap) fMActiveResources.get(resourcesManager)).values(); } catch (NoSuchFieldException e4) { Field mResourceReferences = resourcesManagerClass.getDeclaredField("mResourceReferences"); mResourceReferences.setAccessible(true); references = (Collection) mResourceReferences.get(resourcesManager); } } else { Class<?> activityThread = Class.forName(“android.app.ActivityThread”);
Field fMActiveResources2 = activityThread.getDeclaredField(“mActiveResources”);
fMActiveResources2.setAccessible(true);
references = ((HashMap) fMActiveResources2.get(getActivityThread(context, activityThread))).values();
}
//循环便利当前Resources,将其成员变量mAssets指向自定义的newAssetManager
for (WeakReference wr : references) {
Resources resources2 = (Resources) wr.get();
if (resources2 != null) {
try {
Field mAssets2 = Resources.class.getDeclaredField(“mAssets”);
mAssets2.setAccessible(true);
mAssets2.set(resources2, newAssetManager);
} catch (Throwable th) {
Field mResourcesImpl = Resources.class.getDeclaredField(“mResourcesImpl”);
mResourcesImpl.setAccessible(true);
Object resourceImpl = mResourcesImpl.get(resources2);
Field implAssets = resourceImpl.getClass().getDeclaredField(“mAssets”);
implAssets.setAccessible(true);
implAssets.set(resourceImpl, newAssetManager);
}
//更新资源
resources2.updateConfiguration(resources2.getConfiguration(), resources2.getDisplayMetrics());
}
}
}
}

在研究过程中,本来想基于退出gradle4.1然后在gradle2.2.3重新再搞一下,后面想想不把4.1研究透再去搞2.2.3总是心有不甘,网上也基本找不到gradle 4.1的研究的,其实在2.3.0之后就没有instant-run.zip包了,但是好像所有人都没有提到这点,难道他们都是基于2.2.3或者更早的?

####找不到的项目代码去哪里了

下面来看看我们的MainActivity,MyApplication等文件在去哪里了,找到之前的Server(SocketServerThread),明白我们是从AS端接受这些文件的,接受这些dex文件后,存储在app的cache文件中(getWriteFolder),并进行处理,

private class SocketServerThread extends Thread {
private SocketServerThread() {}

public void run() {
while (true) {
try {
LocalServerSocket localServerSocket = Server.this.serverSocket;
if (localServerSocket == null)
return;
//接受AS发过来的文件
LocalSocket localSocket = localServerSocket.accept();
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Received connection from IDE: spawning connection thread”);
(new Server.SocketServerReplyThread(localSocket)).run();
if (wrongTokenCount > 50) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Stopping server: too many wrong token connections”);
Server.this.serverSocket.close();
return;
}
} catch (Throwable throwable) {
if (Log.isLoggable(“InstantRun”, 2))
Log.v(“InstantRun”, “Fatal error accepting connection on local socket”, throwable);
}
}
}
}

下面可以使用我们一开始下载的源码,在instant-run下面的intant-run-client,InstantClient类中,将文件发送到设备中

private void transferBuildIdToDevice(@NonNull IDevice device, @NonNull String buildId) {
try {
String remoteIdFile = getDeviceIdFolder(mPackageName);
//noinspection SSBasedInspection This should work
File local = File.createTempFile(“build-id”, “txt”);
local.deleteOnExit();
Files.write(buildId, local, Charsets.UTF_8);
device.pushFile(local.getPath(), remoteIdFile);

最后

一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。

这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。

image

请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

moteIdFile);

最后

一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。

这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。

[外链图片转存中…(img-lbGKGAy9-1715593667299)]

请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 26
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值