暂时分离support模块

This commit is contained in:
yummylau
2019-08-27 21:07:44 +08:00
parent 0d2da9081a
commit 9447b4c19f
24 changed files with 512 additions and 12 deletions

View File

@@ -65,13 +65,7 @@ android modular plugin.
而在实现完全被隔离的情况下,如何实现解决这个问题呢 Transform + annotation + asm 技术。通过自定义插件注册自定义的 transform 完成 Class 文件中 annotation 的扫描可收集目的信息(注解内容,注解类等), 用 asm 封装好的字节码指令完成对目标类字节内容的修改达到注入代码的目的。
待外放...
todo list
未来规划: 支持sdk及impl maven仓库存储
1. 自定义scope逻辑统一配置(done)
2. 模块内互相引用 done
3. 仓库管理
5. transform 优化 gradle增量前7秒多 多线程+增量编译 快了1.5倍 3s
6. kotlin 模块测试 done

View File

@@ -1,3 +1,4 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"

View File

@@ -9,6 +9,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.effective.plugins:component-plugin'
classpath 'com.effective.plugins:component-support-plugin'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.quinn.hunter:hunter-transform:0.9.0'
// NOTE: Do not place your application dependencies here; they belong

View File

@@ -1,9 +1,8 @@
apply plugin: 'groovy'
//apply maven 发布插件
apply plugin: 'maven'
group = 'com.effective.plugins'
archivesBaseName = 'module'
archivesBaseName = 'component'
version = '1.0.0'
//设置本地发布路径

Binary file not shown.

View File

@@ -1,6 +1,6 @@
package com.plugin.component.task
import com.plugin.component.PluginRuntime
import com.plugin.component.extension.PublicationManager
import com.plugin.component.extension.option.PublicationOption
import org.gradle.api.DefaultTask
@@ -17,12 +17,11 @@ class CompileSdkTask extends DefaultTask {
@TaskAction
void compileSource() {
def project = getProject()
def releaseJar = JarUtil.packJavaSourceJar(project, publication, MisPlugin.androidJarPath, MisPlugin.sModuleExtension.compileOptions, false)
def releaseJar = JarUtil.packJavaSourceJar(project, publication, PluginRuntime.sAndroidJarPath, PluginRuntime.sModuleExtension.compileOptions, false)
if (releaseJar == null) {
throw new RuntimeException("nothing to push.")
}
JarUtil.packJavaDocSourceJar(publication)
PublicationManager publicationManager = PublicationManager.getInstance()
if (publication.versionNew != null) {
publication.version = publication.versionNew

View File

@@ -0,0 +1,41 @@
apply plugin: 'groovy'
apply plugin: 'maven'
group = 'com.effective.plugins'
archivesBaseName = 'component-support'
version = '1.0.0'
uploadArchives {
repositories {
mavenDeployer {
repository(url: uri('../repo'))
}
}
}
dependencies {
implementation gradleApi()
implementation localGroovy()
implementation 'com.android.tools.build:gradle:3.3.0'
implementation 'org.ow2.asm:asm:7.1'
implementation 'org.ow2.asm:asm-commons:7.1'
implementation 'org.ow2.asm:asm-util:7.1'
implementation 'com.quinn.hunter:hunter-transform:0.9.3'
}
sourceSets {
main {
groovy {
srcDirs = ['src/main/groovy']
}
}
}
repositories {
jcenter()
google()
mavenCentral()
}

172
component-support-plugin/gradlew vendored Normal file
View File

@@ -0,0 +1,172 @@
#!/usr/bin/env sh
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"

84
component-support-plugin/gradlew.bat vendored Normal file
View File

@@ -0,0 +1,84 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -0,0 +1,2 @@
rootProject.name = 'component-support-plugin'

View File

@@ -0,0 +1,24 @@
package com.plugin.component
import org.gradle.api.Plugin
import org.gradle.api.Project
/**
* ./gradlew --no-daemon ComponentPlugin -Dorg.gradle.debug=true
* ./gradlew --no-daemon [clean, :app:generateDebugSources, :library:generateDebugSources, :module_lib:generateDebugSources] -Dorg.gradle.debug=true
* ./gradlew --no-daemon :app:assemble -Dorg.gradle.debug=true
* 组件插件入口
* created by yummylau 2019/08/09
*/
class ComponentPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
boolean isRoot = project == project.rootProject
if (isRoot) {
handleRootProject(project)
} else {
handleProject(project)
}
}
}

View File

@@ -0,0 +1,14 @@
package com.plugin.component.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 方法耗时
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface MethodCost {
}

View File

@@ -0,0 +1,17 @@
package com.plugin.component.extension
/**
* 插件配置
* created by yummylau 2019/08/09
*/
class ComponentSupportExtension {
public boolean openMethodCost = true
/**
* 编译sdk
* @param version
*/
void openMethodCost(boolean openMethodCost) {
this.openMethodCost = openMethodCost
}
}

View File

@@ -0,0 +1,96 @@
package com.plugin.component.transform
import com.plugin.component.anno.AutoInjectComponent
import com.plugin.component.anno.AutoInjectImpl
import com.plugin.component.anno.MethodCost
import com.plugin.component.transform.info.ScanComponentInfo
import com.plugin.component.transform.info.ScanRuntime
import com.plugin.component.transform.info.ScanSdkInfo
import org.objectweb.asm.*
import org.objectweb.asm.commons.AdviceAdapter
class ScanCodeAdapter extends ClassVisitor {
private String className
ScanCodeAdapter(ClassVisitor classVisitor) {
super(Opcodes.ASM7, classVisitor)
}
@Override
void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
className = name
super.visit(version, access, name, signature, superName, interfaces)
}
@Override
AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
AnnotationVisitor annotationVisitor = super.visitAnnotation(descriptor, visible)
ScanComponentInfo scanComponentInfo = null
ScanSdkInfo scanSdkInfo = null
return new AnnotationVisitor(Opcodes.ASM7, annotationVisitor) {
@Override
AnnotationVisitor visitArray(String arrayName) {
return new AnnotationVisitor(Opcodes.ASM7, super.visitArray(arrayName)) {
@Override
void visit(String name, Object value) {
if (Type.getDescriptor(AutoInjectComponent.class) == descriptor) {
if (arrayName != null && arrayName == "impl") {
if (value != null) {
if (scanComponentInfo == null) {
scanComponentInfo = new ScanComponentInfo(className)
}
scanComponentInfo.impl.add((String) value)
}
}
}
if (Type.getDescriptor(AutoInjectImpl.class) == descriptor) {
if (arrayName != null && arrayName == "sdk") {
if (value != null) {
if (scanSdkInfo == null) {
scanSdkInfo = new ScanSdkInfo(className)
}
scanSdkInfo.sdk.add((String) value)
}
}
}
super.visit(name, value)
}
}
}
@Override
void visitEnd() {
if (scanComponentInfo != null) {
ScanRuntime.addComponentInfo(scanComponentInfo)
scanComponentInfo = null
}
if (scanSdkInfo != null) {
ScanRuntime.addSdkInfo(scanSdkInfo)
scanSdkInfo = null
}
super.visitEnd()
}
}
}
@Override
MethodVisitor visitMethod(int access, String name, String methodDescriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, methodDescriptor, signature, exceptions)
mv = new AdviceAdapter(Opcodes.ASM7, mv, access, name, methodDescriptor) {
@Override
AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
if (Type.getDescriptor(MethodCost.class) == descriptor) {
ScanRuntime.addCostMethod(className, name, methodDescriptor)
}
return super.visitAnnotation(descriptor, visible)
}
}
return mv
}
}

View File

@@ -0,0 +1,38 @@
package com.plugin.component.transform
import com.android.build.api.transform.Context
import com.android.build.api.transform.TransformException
import com.android.build.api.transform.TransformInput
import com.android.build.api.transform.TransformOutputProvider
import com.plugin.component.Logger
import com.plugin.component.transform.info.ScanRuntime
import com.quinn.hunter.transform.HunterTransform
import com.quinn.hunter.transform.RunVariant
import org.gradle.api.Project
class ScanCodeTransform extends HunterTransform {
ScanCodeTransform(Project project) {
super(project)
this.bytecodeWeaver = new ScanCodeWeaver()
}
@Override
void transform(Context context, Collection<TransformInput> inputs, Collection<TransformInput> referencedInputs, TransformOutputProvider outputProvider, boolean isIncremental) throws IOException, TransformException, InterruptedException {
long startTime = System.currentTimeMillis()
super.transform(context, inputs, referencedInputs, outputProvider, isIncremental)
ScanRuntime.logScanInfo()
ScanRuntime.buildComponentSdkInfo()
Logger.buildOutput("scan code cost : " + (System.currentTimeMillis() - startTime) + "ms")
}
@Override
protected RunVariant getRunVariant() {
return super.getRunVariant()
}
@Override
protected boolean inDuplcatedClassSafeMode() {
return super.inDuplcatedClassSafeMode()
}
}

View File

@@ -0,0 +1,13 @@
package com.plugin.component.transform
import com.quinn.hunter.transform.asm.BaseWeaver
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
class ScanCodeWeaver extends BaseWeaver {
@Override
protected ClassVisitor wrapClassWriter(ClassWriter classWriter) {
return new InjectCodeAdapter(classWriter)
}
}

View File

@@ -0,0 +1 @@
implementation-class=com.plugin.component.ComponentPlugin

View File

@@ -1,3 +1,4 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"

View File

@@ -1,3 +1,4 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion 29

View File

@@ -1,3 +1,4 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

View File

@@ -2,3 +2,4 @@ include ':app', ':library', ':core', ':librarykotlin'
//调试插件使用
includeBuild './component-plugin'
includeBuild './component-support-plugin'