直接上代码

package com.enjoy.patch;

import android.content.Context;
import android.os.Build;
import android.util.Log; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; public class EnjoyFix { private static final String TAG = "EnjoyFix"; private static File initHack(Context context) { File hackDir = context.getDir("hack", Context.MODE_PRIVATE);
File hackFile = new File(hackDir, "hack.jar");
if (!hackFile.exists()) {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(context.getAssets().open("hack" +
".jar"));
os = new BufferedOutputStream(new FileOutputStream(hackFile));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return hackFile; } public static void installPatch(Context context, File patch) {
File hackFile = initHack(context);
ClassLoader classLoader = context.getClassLoader();
List<File> files = new ArrayList<>();
if (patch.exists()) {
files.add(patch);
}
files.add(hackFile);
File dexOptDir = context.getCacheDir();
try {
Log.i("TAG","Build.VERSION.SDK_INT"+Build.VERSION.SDK_INT);
//23 6.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
V23.install(classLoader, files, dexOptDir);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
V19.install(classLoader, files, dexOptDir); //4.4以上
} else { // >= 14
V14.install(classLoader, files, dexOptDir);
}
} catch (Exception e) {
e.printStackTrace();
}
} private static final class V23 { private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
File optimizedDirectory)
throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, InvocationTargetException, NoSuchMethodException,
IOException {
//找到 pathList
Field pathListField = SharedReflectUtils.findField(loader, "pathList");
Object dexPathList = pathListField.get(loader); ArrayList<IOException> suppressedExceptions = new ArrayList<>();
// 从 pathList找到 makePathElements 方法并执行
// 得到补丁创建的 Element[]
Object[] objects = makePathElements(dexPathList,
new ArrayList<>(additionalClassPathEntries), optimizedDirectory,
suppressedExceptions); //将原本的 dexElements 与 makePathElements生成的数组合并
SharedReflectUtils.expandFieldArray(dexPathList, "dexElements", objects);
if (suppressedExceptions.size() > 0) {
for (IOException e : suppressedExceptions) {
Log.w(TAG, "Exception in makePathElement", e);
throw e;
} }
} /**
* 把dex转化为Element数组
*/
private static Object[] makePathElements(
Object dexPathList, ArrayList<File> files, File optimizedDirectory,
ArrayList<IOException> suppressedExceptions)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
//通过阅读android6、7、8、9源码,都存在makePathElements方法
Method makePathElements = SharedReflectUtils.findMethod(dexPathList, "makePathElements",
List.class, File.class,
List.class);
return (Object[]) makePathElements.invoke(dexPathList, files, optimizedDirectory,
suppressedExceptions);
}
} private static final class V19 { private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
File optimizedDirectory)
throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, InvocationTargetException, NoSuchMethodException,
IOException {
Field pathListField = SharedReflectUtils.findField(loader, "pathList");
Object dexPathList = pathListField.get(loader);
ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
SharedReflectUtils.expandFieldArray(dexPathList, "dexElements",
makeDexElements(dexPathList,
new ArrayList<File>(additionalClassPathEntries), optimizedDirectory,
suppressedExceptions));
if (suppressedExceptions.size() > 0) {
for (IOException e : suppressedExceptions) {
Log.w(TAG, "Exception in makeDexElement", e);
throw e;
}
}
} private static Object[] makeDexElements(
Object dexPathList, ArrayList<File> files, File optimizedDirectory,
ArrayList<IOException> suppressedExceptions)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Method makeDexElements = SharedReflectUtils.findMethod(dexPathList, "makeDexElements",
ArrayList.class, File.class,
ArrayList.class); return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory,
suppressedExceptions);
}
} /**
* 14, 15, 16, 17, 18.
*/
private static final class V14 { private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
File optimizedDirectory)
throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, InvocationTargetException, NoSuchMethodException { Field pathListField = SharedReflectUtils.findField(loader, "pathList");
Object dexPathList = pathListField.get(loader); SharedReflectUtils.expandFieldArray(dexPathList, "dexElements",
makeDexElements(dexPathList,
new ArrayList<File>(additionalClassPathEntries), optimizedDirectory));
} private static Object[] makeDexElements(
Object dexPathList, ArrayList<File> files, File optimizedDirectory)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Method makeDexElements =
SharedReflectUtils.findMethod(dexPathList, "makeDexElements", ArrayList.class,
File.class);
return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory);
}
} }
---------------------------------------------------------------------------------------------------------------------------------
package com.enjoy.patch;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays; /**
* 反射工具类
*/
public class SharedReflectUtils { /**
* 从 instance 到其父类 找 name 属性
*
* @param instance
* @param name
* @return
* @throws NoSuchFieldException
*/
public static Field findField(Object instance, String name) throws NoSuchFieldException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
//查找当前类的 属性(不包括父类)
Field field = clazz.getDeclaredField(name); if (!field.isAccessible()) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException e) {
// ignore and search next
}
}
throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
} /**
* 从 instance 到其父类 找 name 方法
*
* @param instance
* @param name
* @return
* @throws NoSuchFieldException
*/
public static Method findMethod(Object instance, String name, Class<?>... parameterTypes)
throws NoSuchMethodException {
for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
try {
Method method = clazz.getDeclaredMethod(name, parameterTypes); if (!method.isAccessible()) {
method.setAccessible(true);
} return method;
} catch (NoSuchMethodException e) {
// ignore and search next
}
}
throw new NoSuchMethodException("Method "
+ name
+ " with parameters "
+ Arrays.asList(parameterTypes)
+ " not found in " + instance.getClass());
} /**
* @param instance
* @param fieldName
* @param fixs 补丁的Element数组
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void expandFieldArray(Object instance, String fieldName, Object[] fixs)
throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
//拿到 classloader中的dexelements 数组
Field jlrField = findField(instance, fieldName);
//old Element[]
Object[] old = (Object[]) jlrField.get(instance); //合并后的数组
Object[] newElements = (Object[]) Array.newInstance(old.getClass().getComponentType(),
old.length + fixs.length); // 先拷贝新数组
System.arraycopy(fixs, 0, newElements, 0, fixs.length);
System.arraycopy(old, 0, newElements, fixs.length, old.length); //修改 classLoader中 pathList的 dexelements
jlrField.set(instance, newElements);
} }

Android为TV端助力之QQ空间热更新技术的更多相关文章

  1. Android为TV端助力 计算每个目录剩余空间丶总空间以及SD卡剩余空间

    ublic class MemorySpaceCheck { /** * 计算剩余空间 * @param path * @return */ public static String getAvail ...

  2. Android为TV端助力 转载:RecyclerView分页加载

    package com.android.ryane.pulltoloaddata_recyclerview; import android.os.Handler;import android.os.L ...

  3. Android为TV端助力(转载)

    作者地址http://www.jianshu.com/u/63915ef020e2 针对Android Tv的自定义RecyclerView 作者 wenju_song 关注 2016.12.09 1 ...

  4. Android为TV端助力 关于4.0之后不能直接获取SD卡外部存储路径的问题

    Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方 ...

  5. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(上)

    前言 Android中绘图离不开的就是Canvas了,Canvas是一个庞大的知识体系,有Java层的,也有jni层深入到Framework.Canvas有许多的知识内容,构建了一个武器库一般,所谓十 ...

  6. Android为TV端助力 布局、绘制、内存泄露、响应速度、listview和bitmap、线程优化以及一些优化的建议!

    1.布局优化 首先删除布局中无用的控件和层级,其次有选择地使用性能较低的viewgroup,比如布局中既可以使用RelativeLayout和LinearLayout,那我们就采用LinearLayo ...

  7. Android为TV端助力 不需要Socket的跨进程推送消息AIDL!

    上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...

  8. Android为TV端助力之Webview与JS双向交互

    package com.hhzt.iptv.adservice; import android.app.Activity;import android.graphics.Bitmap;import a ...

  9. Android为TV端助力之WebView开发踩坑一

    在Android清单配置文件里面 自定义application时,在4.4系统上面不能加上一个属性,见下图 否则界面将不会显示任何数据,在更高或者更低的系统上面没有测试!

随机推荐

  1. golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)

    原文连接: https://blog.csdn.net/wade3015/article/details/83351776 yaml配置文件的使用方法总结 首先介绍使用yaml配置文件,这里使用的是g ...

  2. 基于redis5的session共享:【redis 5.x集群应用研究】

    基于springsession构建一个session共享的模块. 这里,基于redis的集群(Redis-5.0.3版本),为了解决整个物联网平台的各个子系统之间共享session需求,且方便各个子系 ...

  3. Win64 驱动内核编程-33.枚举与删除对象回调

    转载:http://www.voidcn.com/article/p-wulgeluy-bao.html 枚举与删除对象回调 对象回调存储在对应对象结构体里,简单来说,就是存储在 ObjectType ...

  4. 【漫谈数据仓库】 如何优雅地设计数据分层 ODS DW DM层级

    转载http://bigdata.51cto.com/art/201710/554810.htm 一.文章主题 本文主要讲解数据仓库的一个重要环节:如何设计数据分层!其它关于数据仓库的内容可参考之前的 ...

  5. RTSP Spectification

    Refer: https://www.ietf.org/rfc/rfc2326.txt Network Working Group H. SchulzrinneRequest for Comments ...

  6. (转) 解决django项目部署到nginx+uwsgi服务器后 admin页面样式消失的问题

    原贴地址:https://blog.csdn.net/qq_42571805/article/details/80862455 摘要 uwsgi为主要服务器,nginx为反向代理服务器部署完成之后发现 ...

  7. UE4 常用数学

    转自:https://dawnarc.com/2016/07/mathlinear-algebra%E5%90%91%E9%87%8F%E7%A7%AF%E5%A4%96%E7%A7%AF%E5%8F ...

  8. nginx的rtmp搭建流媒体服务器实现直播流

    最近自己搞了一个用nginx的rtmp来搭建流媒体服务器,从而实现直播的过程,参考了网上很多资料,有些资料对于初学者来说比较难以理解,在此将我搭建的过程记录下来,分享给大家. 1.下载nginx-rt ...

  9. 协程介绍, Greenlet模块,Gevent模块,Genvent之同步与异步

    昨日内容回顾 I/O模型,面试会问到I/O操作,不占用CPU.它内部有一个专门的处理I/O模块.print和写log 属于I/O操作,它不占用CPU 线程GIL保证一个进程中的多个线程在同一时刻只有一 ...

  10. mysql批量更新数据,循环select记录然后更新某一字段

    -- 处理IEMI重复的历史数据,建档日期(只有年月日)倒序,档案ID倒序,根据IMEI查档案,查询的结果,相同IMEI下的第一条记录的IEMI不处理,其他的记录的imei 改为空. -- USE ` ...