Skip to content

llew2011/BytecodeFixer

Repository files navigation

BytecodeFixer简介

BytecodeFixer 是基于Javassist开发的一款轻量级的字节码修复插件,它可以有效的修复第三方Jar包中出现的一些bug,例如:NullPointerException,NumberFormatException,IndexOutOfBoundsException等。它利用Gradle1.5.0版本后的Transform API 在项目打包时动态的对class文件进行修复。

**提示:**BytecodeFixer插件不会增加APP的方法数

BytecodeFixer使用

  • 引入插件 :在根项目的build.gradle添加如下配置:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        // 添加如下依赖
        classpath 'com.llew.bytecode.fix.gradle:BytecodeFixer:1.1.0'
    }
}
  • 使用插件 :在主项目的build.gradle末尾添加如下配置:
apply plugin: 'com.llew.bytecode.fix'
bytecodeFixConfig {
    enable = true
    logEnable = true
    keepFixedJarFile = true
    keepFixedClassFile = true
    dependencies = [
	    'jar1 absolutePath',
	    'jar2 absolutePath'
	    ]
    fixConfig = [
           'className##methodName#injectValue##injectLine'
           ]
}
  • 配置说明
  • enable true | false -- BytecodeFixer插件是否可用
  • logEnable true | false -- 是否允许打印日志
  • keepFixedJarFile true | false -- 是否保存修复过的Jar文件
  • keepFixedClassFile true | false -- 是否保存修复过的class文件
  • dependencies -- 依赖的第三方Jar文件或class文件的绝对路径
  • fixConfig注入配置,格式为A##B##C##D -- A:表示待修复的类名,例如:com.tencent.av.sdk.NetworkHelp -- B:表示待修复的方法名,例如:getAPInfo(android.content.Context) -- C:表示修复内容,例如:$1 = null;System.out.println("I have hooked this method by BytecodeFixer plugin !!!"); -- D:表示把修复内容插入在方法的哪一行,D > 0 表示插在具体的行数,D == 0表示插在方法的最开始处,D < 0表示替换方法的全部内容

注意: 在配置fixConfig的代码时,除了基本类型外,其他所有类型都需要写上类完整路径(packageName + className),否则编译器报错:[source error] no such **

  • 使用案例:
apply plugin: 'com.llew.bytecode.fix'
bytecodeFixConfig {
    logEnable = true
    keepFixedJarFile = true
    keepFixedClassFile = true
    dependencies = ['/Users/llew/Desktop/Android/Android_SDK_Eclipse/extras/android/support/v4/android-support-v4.jar']
    fixConfig = [
            'com.tencent.av.sdk.NetworkHelp##getAPInfo(android.content.Context)##$1 = null;System.out.println("I have hooked this method by BytecodeFixer plugin !!!");##0',
            'com.tencent.av.sdk.NetworkHelp##getMobileAPInfo(android.content.Context,int)##if(Boolean.TRUE.booleanValue()){$1 = null;System.out.println("i have hooked this method by BytecodeFixer !!!");}return new com.tencent.av.sdk.NetworkHelp.APInfo();##-1',
            'com.tencent.av.camera.CameraCaptureSettings##initSettings()##{}##0'
    ]
}

BytecodeFix博客

About

BytecodeFixer can fix third platform jar file that has some bugs by javassist in Android

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published