Skip to content

Commit

Permalink
1、完善cutInfo.json
Browse files Browse the repository at this point in the history
2、修复一个bug
3、优化debugMode的增量编译
  • Loading branch information
FlyJingFish committed May 15, 2024
1 parent ebc2f7a commit 137b44d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.flyjingfish.android_aop_plugin.beans

data class CutCollectMethodJson(val method:String,val classCount:Int,val classes:MutableList<String> = mutableListOf())
data class CutCollectMethodJson(val method:String,val collectType:String, val regex:String,val classCount:Int,val classes:MutableList<String> = mutableListOf())
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.flyjingfish.android_aop_plugin.beans

data class CutCollectMethodJsonCache(val collectType:String, val regex:String, val classes:MutableList<String> = mutableListOf())
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ enum class RootBooleanConfig(
) {
DEBUG_MODE("androidAop.debugMode", false),
REFLECT_INVOKE_METHOD("androidAop.reflectInvokeMethod", false),
ONLY_DEBUG("androidAop.debugMode.variantOnlyDebug", true);
ONLY_DEBUG("androidAop.debugMode.variantOnlyDebug", true),
INCREMENTAL("androidAop.debugMode.isIncremental", true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ abstract class BasePlugin :Plugin<Project> {
var reflectInvokeMethod = false
private var debugMode = false
private var onlyDebug = false
private var isIncremental = true
private fun init(project: Project){
val reflectInvokeMethodStr = project.properties[RootBooleanConfig.REFLECT_INVOKE_METHOD.propertyName]?:"${RootBooleanConfig.REFLECT_INVOKE_METHOD.defaultValue}"
val debugModeStr = project.properties[RootBooleanConfig.DEBUG_MODE.propertyName]?:"${RootBooleanConfig.DEBUG_MODE.defaultValue}"
val onlyModeStr = project.properties[RootBooleanConfig.ONLY_DEBUG.propertyName]?:"${RootBooleanConfig.ONLY_DEBUG.defaultValue}"
val isIncrementalStr = project.properties[RootBooleanConfig.INCREMENTAL.propertyName]?:"${RootBooleanConfig.INCREMENTAL.defaultValue}"
debugMode = debugModeStr.toString() == "true"
reflectInvokeMethod = reflectInvokeMethodStr.toString() == "true"
onlyDebug = onlyModeStr.toString() == "true"
isIncremental = isIncrementalStr.toString() == "true"
}

fun isIncremental():Boolean{
return isIncremental
}

override fun apply(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.android.build.gradle.BaseExtension
import com.android.build.gradle.DynamicFeaturePlugin
import com.android.build.gradle.LibraryExtension
import com.flyjingfish.android_aop_plugin.config.AndroidAopConfig
import com.flyjingfish.android_aop_plugin.scanner_visitor.WovenIntoCode
import com.flyjingfish.android_aop_plugin.tasks.CompileAndroidAopTask
import com.flyjingfish.android_aop_plugin.tasks.SyncConfigTask
import com.flyjingfish.android_aop_plugin.utils.AndroidConfig
Expand Down Expand Up @@ -65,10 +66,23 @@ class CompilePlugin(private val root:Boolean): BasePlugin() {
}
val variantName = variant.name
val buildTypeName = variant.buildType.name
if (javaCompile is JavaCompile){
if (!isIncremental() && javaCompile is JavaCompile && isDebugMode(buildTypeName,variantName)){
javaCompile.options.isIncremental = false
}
// println("CompilePlugin=variant=$variantName,output.name=${variant.buildType.name},isDebug=${isDebugMode(buildTypeName,variantName)}")
if (isApp && isIncremental()){
javaCompile.doFirst{
val enabled = try {
val firstConfig = project.extensions.getByType(AndroidAopConfig::class.java)
firstConfig.enabled
} catch (e: Exception) {
true
}
if (enabled && isDebugMode(buildTypeName,variantName)){
WovenIntoCode.deleteOtherCompileClass(project, variantName)
}
}
}
javaCompile.doLast{
val androidAopConfig : AndroidAopConfig = if (isApp){
val config = project.extensions.getByType(AndroidAopConfig::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.flyjingfish.android_aop_plugin.scanner_visitor

import com.flyjingfish.android_aop_plugin.beans.AopCollectClass
import com.flyjingfish.android_aop_plugin.beans.AopCollectCut
import com.flyjingfish.android_aop_plugin.beans.CutFileJson
import com.flyjingfish.android_aop_plugin.beans.MethodRecord
import com.flyjingfish.android_aop_plugin.utils.ClassFileUtils
import com.flyjingfish.android_aop_plugin.utils.ClassNameToConversions
Expand All @@ -21,6 +22,7 @@ import javassist.NotFoundException
import javassist.bytecode.AnnotationsAttribute
import javassist.bytecode.AttributeInfo
import javassist.bytecode.annotation.Annotation
import org.gradle.api.Project
import org.objectweb.asm.*
import org.objectweb.asm.ClassWriter.COMPUTE_FRAMES
import org.objectweb.asm.ClassWriter.COMPUTE_MAXS
Expand Down Expand Up @@ -649,4 +651,18 @@ object WovenIntoCode {
}

}

fun deleteOtherCompileClass(project:Project, variantName:String){
val json : CutFileJson? = InitConfig.optFromJsonString(
InitConfig.readAsString(Utils.aopCompileTempOtherJson(project,variantName)),
CutFileJson::class.java)
json?.let {
it.cacheFileJson.forEach {filePath ->
val file = File(filePath)
if (file.exists()){
file.delete()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class CompileAndroidAopTask(
}
}
if (isApp){
val cacheDeleteFiles = mutableListOf<String>()
val tmpOtherDir = File(Utils.aopCompileTempOtherDir(project,variantName))
WovenIntoCode.createInitClass(tmpOtherDir)
WovenIntoCode.createCollectClass(tmpOtherDir)
Expand All @@ -258,8 +259,10 @@ class CompileAndroidAopTask(
file.inputStream().use {
target.saveEntry(it)
}
cacheDeleteFiles.add(target.absolutePath)
}
}
InitConfig.exportCacheCutFile(File(Utils.aopCompileTempOtherJson(project,variantName)),cacheDeleteFiles)
if (!AndroidAopConfig.debug){
tmpOtherDir.deleteRecursively()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import java.util.jar.JarFile
object AopTaskUtils {
fun processFileForConfig(file : File, directory: File, directoryPath:String){
if (file.isFile) {
// logger.error("file.name="+file.absolutePath)
val className = file.absolutePath.replace("$directoryPath/","")
WovenInfoUtils.addClassName(className)
if (file.name.endsWith(Utils.AOP_CONFIG_END_NAME)) {
FileInputStream(file).use { inputs ->
val classReader = ClassReader(inputs.readAllBytes())
classReader.accept(
SearchAOPConfigVisitor(), ClassReader.EXPAND_FRAMES)
}
}else if (file.absolutePath.endsWith(Utils._CLASS)){
val className = file.absolutePath.replace("$directoryPath/","")
WovenInfoUtils.addClassName(className)
if (AndroidAopConfig.verifyLeafExtends && !className.startsWith("kotlinx/") && !className.startsWith("kotlin/")){
FileInputStream(file).use { inputs ->
val bytes = inputs.readAllBytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.flyjingfish.android_aop_plugin.utils

import com.android.build.gradle.internal.coverage.JacocoReportTask
import com.flyjingfish.android_aop_plugin.beans.AopCollectClass
import com.flyjingfish.android_aop_plugin.beans.CutCollectMethodJsonCache
import com.flyjingfish.android_aop_plugin.beans.CutClassesJson
import com.flyjingfish.android_aop_plugin.beans.CutClassesJsonMap
import com.flyjingfish.android_aop_plugin.beans.CutCollectJson
Expand Down Expand Up @@ -35,7 +36,7 @@ object InitConfig {
private val cutInfoMap = mutableMapOf<String, CutJsonMap?>()
private val replaceMethodInfoMap = mutableMapOf<String, ReplaceMethodInfo>()
private val modifyExtendsClassMap = mutableMapOf<String, ModifyExtendsClassJson>()
private val collectClassMap = mutableMapOf<String, MutableMap<String,MutableSet<String>>>()
private val collectClassMap = mutableMapOf<String, MutableMap<String, CutCollectMethodJsonCache>>()
var isInit: Boolean = false
private val gson: Gson = GsonBuilder().create()
fun <T> optFromJsonString(jsonString: String, clazz: Class<T>): T? {
Expand Down Expand Up @@ -204,14 +205,14 @@ object InitConfig {
}
collectClassMap.forEach {(classKey,classMap)->
val collectClassJson = CutCollectJson("收集切面",classKey)
classMap.forEach {(methodName,classes)->
val methodJson = CutCollectMethodJson(methodName,classes.size)
methodJson.classes.addAll(classes)
classMap.forEach {(methodName,methodCache)->
val methodJson = CutCollectMethodJson(methodName,methodCache.collectType,methodCache.regex,methodCache.classes.size)
methodJson.classes.addAll(methodCache.classes)
collectClassJson.collectMethod.add(methodJson)
}
cutJsons.add(collectClassJson)
}
val json = GsonBuilder().setPrettyPrinting().create().toJson(cutJsons)
val json = GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(cutJsons)

saveFile(cutInfoFile, json)

Expand Down Expand Up @@ -271,37 +272,14 @@ object InitConfig {
classMap = mutableMapOf()
collectClassMap[aopCollectClass.invokeClassName] = classMap
}
val paramKey = if (aopCollectClass.isClazz) "Class[? extends ${aopCollectClass.collectClassName}]" else aopCollectClass.collectClassName
val paramKey = if (aopCollectClass.isClazz) "Class<? extends ${aopCollectClass.collectClassName}>" else aopCollectClass.collectClassName
val methodKey = "${aopCollectClass.invokeMethod}($paramKey)"
var methodSet = classMap[methodKey]
if (methodSet == null){
methodSet = mutableSetOf()
methodSet = CutCollectMethodJsonCache(aopCollectClass.collectType,aopCollectClass.regex)
classMap[methodKey] = methodSet
}
methodSet.add(aopCollectClass.collectExtendsClassName)
methodSet.classes.add(aopCollectClass.collectExtendsClassName)

}
}



/**
{
"type": "收集切面",
"collectClass": "com.flyjingfish.test_lib.collect.InitCollect2",
"collectMethod": [
{
"method": "collect(sssss)",
"classCount": 1,
"class": [
"com.flyjingfish.test_lib.collect.test"
]
}
]
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ object Utils {
fun aopCompileTempOtherDir(project:Project, variantName:String):String{
return project.buildDir.absolutePath + "/tmp/android-aop/${variantName}/tempCompileOtherClass/"
}
fun aopCompileTempOtherJson(project:Project, variantName:String):String{
return project.buildDir.absolutePath + "/tmp/android-aop/${variantName}/tempCompileOtherClassJson/needDelClassInfo.json"
}
fun aopTransformTempDir(project:Project, variantName:String):String{
return project.buildDir.absolutePath+"/tmp/android-aop/tempInvokeClass/${variantName}/"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public static void collect(SubApplication2 sub){
collects.add(sub);
}

@AndroidAopCollectMethod
public static void collect2(SubApplication2 sub){
collects.add(sub);
}

@AndroidAopCollectMethod
public static void collect3(Class<? extends SubApplication2> sub){
collectClazz.add(sub);
Expand Down

0 comments on commit 137b44d

Please sign in to comment.