Skip to content

Commit

Permalink
Merge pull request #18 from BryanSharp/easy_jar_modify-0621
Browse files Browse the repository at this point in the history
1.2.7 release
modify jar more easily
  • Loading branch information
BryanSharp authored Jun 21, 2017
2 parents 72fbcad + f658345 commit 615b3fe
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: java
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Beaver,即河狸,是一种日日忙碌于在自己栖息河流上修建和

该插件已经上传到Jcenter,可直接引用最新版本如下:

classpath 'com.bryansharp:hibeaver:1.2.6'
classpath 'com.bryansharp:hibeaver:1.2.7'

在1.2.7及以上的版本中,hibeaverModifyFiles任务不再依赖Android的gradle插件,也就是说只有你有gradle和Java运行环境,建一个build.gradle就可以指定Jar/Aar文件进行修改了。详见testJarModify目录下的示例。

[Link to Jcenter](https://bintray.com/bsp0911932/maven/HiBeaver)

Expand Down Expand Up @@ -100,7 +101,7 @@ Basically, HiBeaver is an Android plugin for modifying your java byte code durin

This plugin has been uploaded to jcenter. You can use this by adding the following code to your buildScripts:

classpath 'com.bryansharp:HiBeaver:1.2.6'
classpath 'com.bryansharp:HiBeaver:1.2.7'

[Link to Jcenter](https://bintray.com/bsp0911932/maven/HiBeaver)

Expand Down
12 changes: 8 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repositories {
mavenCentral()
}
group = 'com.bryansharp'
version = '1.2.6'
version = '1.2.7'
uploadArchives {
// version = version + '-SNAPSHOT'//if you are testing the demo I provide locally, you can uncomment this.
repositories {
Expand Down Expand Up @@ -75,10 +75,14 @@ artifacts {
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
boolean isHasFile = false
if (project.rootProject.findProject('local.properties') != null){
isHasFile = true
properties.load(project.rootProject.file('local.properties').newDataInputStream())
}
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
user = isHasFile ? properties.getProperty("bintray.user") : System.getenv("bintray.user")
key = isHasFile ? properties.getProperty("bintray.apikey") : System.getenv("bintray.apikey")
configurations = ['archives']
pkg {
repo = "maven"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ class HiBeaverPluginImpl implements Plugin<Project> {
println ":applied HiBeaver"
project.extensions.create('hiBeaver', HiBeaverParams)
Util.setProject(project)
registerTransform(project)
try {
if(Class.forName("com.android.build.gradle.BaseExtension")){
BaseExtension android = project.extensions.getByType(BaseExtension)
if (android instanceof LibraryExtension) {
DataHelper.ext.projectType = DataHelper.TYPE_LIB;
} else if (android instanceof AppExtension) {
DataHelper.ext.projectType = DataHelper.TYPE_APP;
} else {
DataHelper.ext.projectType = -1
}
if (DataHelper.ext.projectType != -1) {
registerTransform(android)
}
}
} catch (Exception e) {
DataHelper.ext.projectType = -1
}
initDir(project);
project.afterEvaluate {
Log.setQuiet(project.hiBeaver.keepQuiet);
Expand All @@ -35,21 +51,15 @@ class HiBeaverPluginImpl implements Plugin<Project> {
}
}

def static registerTransform(Project project) {
// def isApp = project.plugins.hasPlugin("com.android.application")
BaseExtension android = project.extensions.getByType(BaseExtension)
if (android instanceof LibraryExtension){
DataHelper.ext.projectType = DataHelper.TYPE_LIB;
} else if(android instanceof AppExtension){
DataHelper.ext.projectType = DataHelper.TYPE_APP;
} else {
DataHelper.ext.projectType = -1
}
def static registerTransform(BaseExtension android) {
InjectTransform transform = new InjectTransform()
android.registerTransform(transform)
}

static void initDir(Project project) {
if (!project.buildDir.exists()) {
project.buildDir.mkdirs()
}
File hiBeaverDir = new File(project.buildDir, "HiBeaver")
if (!hiBeaverDir.exists()) {
hiBeaverDir.mkdir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ public class ModifyFiles {
ZipEntry zipEntry = new ZipEntry(name);

outputAarStream.putNextEntry(zipEntry);
Log.info("name is ${name}")
if (name.endsWith(".jar")) {
File innerJar = unzipEntryToTemp(element, zipFile);
def outJar = modifyJar(innerJar, map, tempDir, true);
outputAarStream.write(IOUtils.toByteArray(new FileInputStream(outJar)))
} else {
def stream = zipFile.getInputStream(element)
byte[] array = IOUtils.toByteArray(stream)
Log.info("length is ${array.length}")
if (array != null) {
outputAarStream.write(array)
}
Expand Down
Binary file added testJarModify/MiPush_SDK_Client_3_2_2.jar
Binary file not shown.
84 changes: 84 additions & 0 deletions testJarModify/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
buildscript {
repositories {
jcenter()
maven {
url uri('../snapshotRepo')
}
}
dependencies {
classpath 'com.bryansharp:hibeaver:1.2.7-SNAPSHOT'//this is the local version
}
}
apply plugin: 'hiBeaver'
import com.bryansharp.gradle.hibeaver.utils.MethodLogAdapter
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

//or you can import like bellow:
//import org.objectweb.asm.*
hiBeaver {
//this will determine the name of this hibeaver transform, no practical use.
hiBeaverModifyName = 'myHibeaverTest'
//turn this on to make it print help content, default value is true
showHelp = true
//this flag will decide whether the log of the modifying process be printed or not, default value is false
keepQuiet = false
//this is a kit feature of the plugin, set it true to see the time consume of this build
watchTimeConsume = false

//this is the most important part
modifyMatchMaps = [
'com.xiaomi.mipush.sdk.u' : [
// you can use javap -s command to get the description of one method
['methodName': 'a', 'methodDesc': '(Lorg/apache/thrift/a;Lcom/xiaomi/xmpush/thrift/a;ZLcom/xiaomi/xmpush/thrift/r;)V', 'adapter': {
ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions ->
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
MethodVisitor adapter = new MethodLogAdapter(methodVisitor) {
@Override
void visitCode() {
super.visitCode();
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, MethodLogAdapter.className2Path("bruce.com.testhibeaver.MainActivity"), "hookXM", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
return adapter;
}]
],
'*Activity|*Receiver|!android*': [
['methodName': 'on**', 'methodDesc': null, 'adapter': {
ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions ->
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
MethodVisitor adapter = new MethodLogAdapter(methodVisitor) {
@Override
void visitCode() {
super.visitCode();
methodVisitor.visitLdcInsn(desc);
methodVisitor.visitLdcInsn(name);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "bruce/com/testhibeaver/MainActivity", "hookXM", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
return adapter;
}]
]
,
'r:.*D[a-zA-Z]*Client' : [
['methodName': 'on**', 'methodDesc': null, 'adapter': {
ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions ->
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
MethodVisitor adapter = new MethodLogAdapter(methodVisitor) {
@Override
void visitCode() {
super.visitCode();
methodVisitor.visitLdcInsn(desc);
methodVisitor.visitLdcInsn(name);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "bruce/com/testhibeaver/MainActivity", "hookXM", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
return adapter;
}]
]
]
modifyTasks = ["${rootDir.absolutePath}/MiPush_SDK_Client_3_2_2.jar": modifyMatchMaps]
}

0 comments on commit 615b3fe

Please sign in to comment.