Skip to content

Commit

Permalink
增加并行编译参数 parallel
Browse files Browse the repository at this point in the history
推送v1.1.3
  • Loading branch information
iwhys committed Dec 6, 2018
1 parent 733def7 commit 87ddafe
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_version = '1.3.10'
repositories {
google()
jcenter()
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'kotlin'

buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_version = '1.3.10'
repositories {
google()
jcenter()
Expand All @@ -19,6 +19,7 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1"
implementation('com.android.tools.build:gradle:3.0.0') {
exclude group: 'org.jetbrains.kotlin'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import org.gradle.api.Project
*/
open class SdkEditorConfig {

/**
* 是否开启并行处理任务,false单线程处理,true使用协程多线程处理任务
*/
var parallel: Boolean = false

/**
* 收集信息时需要被额外检查的jar的名称,即包括Fix类的jar包
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.android.build.api.transform.TransformInvocation
import com.iwhys.classeditor.domain.ReplaceClass
import javassist.ClassPool
import javassist.CtClass
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.apache.commons.io.FileUtils
import org.gradle.api.Project
import java.io.File
Expand All @@ -27,6 +29,8 @@ class TransformHandler(project: Project, transformInvocation: TransformInvocatio

private val sdkEditorConfig = SdkEditorConfig[project]

private val isParallel = sdkEditorConfig.parallel

private val outputProvider = transformInvocation.outputProvider

private val dirInputs = mutableSetOf<DirectoryInput>()
Expand Down Expand Up @@ -66,7 +70,16 @@ class TransformHandler(project: Project, transformInvocation: TransformInvocatio
* 根据收集到的信息修复sdk中的bug类
*/
private fun fixSdk() {
log("Begin to fix the bug classes.")
if (isParallel) {
fixSdkParallel()
} else {
fixSdkSerial()
}
log("All bug classes have been fixed.")
}

private fun fixSdkSerial() {
log("Begin to fix the bug classes in serial.")
for (jarInput in jarInputs.values) {
if (!isTargetJar(jarInput.name)) {
log("Not the target jar package, output directly:${jarInput.name}")
Expand All @@ -76,7 +89,23 @@ class TransformHandler(project: Project, transformInvocation: TransformInvocatio
log("Found the target jar package:${jarInput.name}, prepare to fix.")
jarInput.handleClass { name !in replaceClasses }
}
log("All bug classes have been fixed.")
}

private fun fixSdkParallel() = runBlocking {
log("Begin to fix the bug classes in parallel.")
for (jarInput in jarInputs.values) {
if (!isTargetJar(jarInput.name)) {
log("Not the target jar package, output directly:${jarInput.name}")
launch {
safe { FileUtils.copyFile(jarInput.file, outputProvider.jarOutput(jarInput)) }
}
continue
}
log("Found the target jar package:${jarInput.name}, prepare to fix.")
launch {
jarInput.handleClass { name !in replaceClasses }
}
}
}

/**
Expand Down Expand Up @@ -104,13 +133,36 @@ class TransformHandler(project: Project, transformInvocation: TransformInvocatio
* 收集信息之后的dirInputs或者jarInputs会被直接输入,因为他们实际上应该都是开发者可控源文件的编译产物
*/
private fun gatherInfo() {
log("Begin to gather the classes information.")
if (isParallel) {
gatherInfoParallel()
} else {
gatherInfoSerial()
}
log("The classes information collection:$replaceClasses")
}

private fun gatherInfoSerial() {
log("Begin to gather the classes information in serial.")
dirInputs.forEach(infoFromDirInput)
val jarInputNames = jarInputs.keys
sdkEditorConfig.fixedJarNamesSet()?.mapNotNull {
findInfoJarInput(it, jarInputNames)
}?.forEach(infoFromJarInput)
log("The classes information collection:$replaceClasses")
}

private fun gatherInfoParallel() = runBlocking {
log("Begin to gather the classes information in parallel.")
for (dirInput in dirInputs) {
launch {
infoFromDirInput(dirInput)
}
}
val jarInputNames = jarInputs.keys
sdkEditorConfig.fixedJarNamesSet()?.mapNotNull {
findInfoJarInput(it, jarInputNames)
}?.forEach {
launch { infoFromJarInput(it) }
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {

sdkEditor {
fixedJarNames = ['hack_du']
parallel = false
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def projectDescription = "The plugin to edit sdk."

group = "com.iwhys.sdkeditor"
// 版本号,下次更新是只需要更改版本号即可
version = "1.1.2"
version = "1.1.3"

//生成源文件
task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down

0 comments on commit 87ddafe

Please sign in to comment.