Skip to content

Commit ac83b82

Browse files
author
xiaoqi
committed
update plugin
1 parent b2fda37 commit ac83b82

File tree

12 files changed

+222
-4
lines changed

12 files changed

+222
-4
lines changed

.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

3-
buildscript {
3+
buildscript {
44
ext.kotlin_version = '1.2.30'
55

66
repositories {
@@ -9,13 +9,18 @@ buildscript {
99
url 'https://maven.google.com/'
1010
name 'Google'
1111
}
12+
maven {
13+
url uri('publish')
14+
}
1215
}
1316
dependencies {
1417
classpath 'com.android.tools.build:gradle:3.0.1'
1518
classpath 'com.novoda:bintray-release:0.5.0'
1619
// NOTE: Do not place your application dependencies here; they belong
1720
// in the individual module build.gradle files
18-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
21+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
22+
classpath 'com.noober.plugin:savehelper-plugin:1.0.0'
23+
}
1924
}
2025

2126
allprojects {

sample/build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-kapt'
33
apply plugin: 'kotlin-android-extensions'
44
apply plugin: 'kotlin-android'
5+
apply plugin: 'myplugin'
6+
57

68
android {
79
compileSdkVersion 26
@@ -41,4 +43,3 @@ dependencies {
4143
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
4244
}
4345

44-

savehelper-plugin/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

savehelper-plugin/build.gradle

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apply plugin: 'groovy'
2+
apply plugin: 'maven'
3+
4+
dependencies {
5+
compile gradleApi()
6+
compile localGroovy()
7+
//groovy sdk
8+
compile 'com.android.tools.build:gradle:3.0.1'
9+
compile 'org.javassist:javassist:3.20.0-GA'
10+
}
11+
12+
repositories {
13+
jcenter()
14+
}
15+
group='com.noober.plugin'
16+
version='1.0.0'
17+
uploadArchives{
18+
repositories {
19+
mavenDeployer {
20+
repository(url: uri('../publish'))
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.noober.plugin;
2+
3+
import com.android.build.gradle.AppExtension;
4+
5+
import org.gradle.api.Action;
6+
import org.gradle.api.Plugin;
7+
import org.gradle.api.Project;
8+
import org.gradle.api.Task;
9+
import org.gradle.api.logging.Logger;
10+
11+
/**
12+
* Created by xiaoqi on 2018/8/8
13+
*/
14+
public class MyJavaPlugin implements Plugin<Project> {
15+
@Override
16+
public void apply(Project project) {
17+
project.evaluationDependsOn("clean");
18+
AppExtension android = project.getExtensions().findByType(AppExtension.class);
19+
android.registerTransform(new SaveTransform(project));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.noober.plugin
2+
3+
import com.android.build.gradle.AppExtension
4+
import org.gradle.api.Action
5+
import org.gradle.api.Plugin
6+
import org.gradle.api.Project
7+
import org.gradle.api.Task
8+
9+
class MyPlugin implements Plugin<Project> {
10+
11+
@Override
12+
void apply(Project project) {
13+
def android = project.getExtensions().findByType(AppExtension);
14+
android.registerTransform(new SaveTransform(project));
15+
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.noober.plugin
2+
3+
import javassist.ClassPool
4+
import javassist.CtClass
5+
import javassist.CtConstructor
6+
import org.gradle.api.Project
7+
8+
class SaveInject {
9+
private static ClassPool classPool = ClassPool.getDefault()
10+
private static String injectStr = "System.out.print(\"sssssssss\"); "
11+
12+
public static void inject(String path, String packageName, Project project){
13+
classPool.importPackage("android.util.*")
14+
classPool.appendClassPath(path)
15+
File dir = new File(path)
16+
if(dir.isDirectory()){
17+
dir.eachFileRecurse {File file ->
18+
String filePath = file.getAbsolutePath()
19+
if(filePath.endsWith('.class')
20+
&& !filePath.contains('R$')
21+
&& !filePath.contains('R.class')
22+
&& !filePath.contains('BuildConfig.class')){
23+
project.logger.error(filePath)
24+
int index = filePath.indexOf(packageName)
25+
if(index != -1){
26+
int end = filePath.length() - ".class".length()
27+
String className = filePath.substring(index, end).replace('/', '.')
28+
CtClass ctClass = classPool.getCtClass(className)
29+
if(ctClass.isFrozen()){
30+
ctClass.defrost()
31+
}
32+
CtConstructor[] constructor = ctClass.getDeclaredConstructors()
33+
34+
if(constructor == null || constructor.length == 0){
35+
CtConstructor ctConstructor = new CtConstructor(new CtClass[0], ctClass)
36+
ctConstructor.addLocalVariable(injectStr, ctClass)
37+
// ctClass.addConstructor(ctConstructor)
38+
}else {
39+
constructor[0].addLocalVariable(injectStr, ctClass)
40+
}
41+
ctClass.writeFile(path)
42+
ctClass.detach()
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.noober.plugin
2+
3+
import com.android.build.api.transform.Context
4+
import com.android.build.api.transform.DirectoryInput
5+
import com.android.build.api.transform.Format
6+
import com.android.build.api.transform.JarInput
7+
import com.android.build.api.transform.QualifiedContent
8+
import com.android.build.api.transform.Transform
9+
import com.android.build.api.transform.TransformException
10+
import com.android.build.api.transform.TransformInput
11+
import com.android.build.api.transform.TransformInvocation
12+
import com.android.build.api.transform.TransformOutputProvider
13+
import com.android.build.gradle.internal.pipeline.TransformManager
14+
import org.apache.commons.io.FileUtils
15+
import org.apache.commons.codec.digest.DigestUtils
16+
import org.gradle.api.Project
17+
18+
public class SaveTransform extends Transform{
19+
Project project
20+
21+
SaveTransform(Project project){
22+
this.project = project
23+
}
24+
25+
26+
@Override
27+
String getName() {
28+
return "MyTrans"
29+
}
30+
31+
@Override
32+
Set<QualifiedContent.ContentType> getInputTypes() {
33+
return TransformManager.CONTENT_CLASS
34+
}
35+
36+
@Override
37+
Set<? super QualifiedContent.Scope> getScopes() {
38+
return TransformManager.SCOPE_FULL_PROJECT
39+
}
40+
41+
@Override
42+
boolean isIncremental() {
43+
return false
44+
}
45+
46+
// @Override
47+
// void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
48+
//// super.transform(transformInvocation)
49+
// transformInvocation.inputs.each { TransformInput input ->
50+
// input.directoryInputs.each {DirectoryInput directoryInput ->
51+
// SaveInject.inject(directoryInput.file.absolutePath, "com\\recover\\autosavesample")
52+
//
53+
// def filePath = transformInvocation.outputProvider.getContentLocation(directoryInput.name,
54+
// directoryInput.scopes, Format.DIRECTORY)
55+
// FileUtil.copy(directoryInput.file, filePath)
56+
// }
57+
//
58+
// input.jarInputs.each {JarInput jarInput ->
59+
// def jarName = jarInput.name
60+
// def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
61+
// if(jarName.endsWith(".jar")) {
62+
// jarName = jarName.substring(0,jarName.length()-4)
63+
// }
64+
// //生成输出路径
65+
// def dest = outputProvider.getContentLocation(jarName+md5Name,
66+
// jarInput.contentTypes, jarInput.scopes, Format.JAR)
67+
// //将输入内容复制到输出
68+
// FileUtils.copyFile(jarInput.file, dest)
69+
// }
70+
// }
71+
// }
72+
73+
@Override
74+
void transform(Context context, Collection<TransformInput> inputs, Collection<TransformInput> referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException {
75+
inputs.each { TransformInput input ->
76+
input.directoryInputs.each {DirectoryInput directoryInput ->
77+
SaveInject.inject(directoryInput.file.absolutePath, "com/recover/autosavesample", project)
78+
79+
def filePath = outputProvider.getContentLocation(directoryInput.name,
80+
directoryInput.scopes, Format.DIRECTORY)
81+
FileUtils.copy(directoryInput.file, filePath)
82+
}
83+
84+
input.jarInputs.each {JarInput jarInput ->
85+
def jarName = jarInput.name
86+
def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath())
87+
if(jarName.endsWith(".jar")) {
88+
jarName = jarName.substring(0,jarName.length()-4)
89+
}
90+
//生成输出路径
91+
def dest = outputProvider.getContentLocation(jarName+md5Name,
92+
jarInput.contentTypes, jarInput.scopes, Format.JAR)
93+
//将输入内容复制到输出
94+
FileUtils.copyFile(jarInput.file, dest)
95+
}
96+
}
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.noober.plugin.MyPlugin

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':savehelper-api', ':savehelper-processor', ':savehelper', ':sample'
1+
include ':savehelper-api', ':savehelper-processor', ':savehelper', ':sample', ':savehelper-plugin'

0 commit comments

Comments
 (0)