14
14
package org.lizhi.tiya.dependency
15
15
16
16
import org.gradle.api.Project
17
- import org.gradle.api.artifacts.*
18
- import org.gradle.api.file.CopySpec
19
- import org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency
17
+ import org.gradle.api.artifacts.Configuration
18
+ import org.gradle.api.artifacts.Dependency
19
+ import org.gradle.api.artifacts.DependencySubstitutions
20
+ import org.gradle.api.artifacts.ProjectDependency
20
21
import org.lizhi.tiya.log.FastBuilderLogger
21
22
import org.lizhi.tiya.plugin.IPluginContext
22
23
import org.lizhi.tiya.task.AARBuilderTask
23
- import java.io.File
24
24
25
25
/* *
26
26
* 依赖替换帮助类
27
27
*/
28
28
class DependencyReplaceHelper (private val pluginContext : IPluginContext ) {
29
29
30
- private val configList = mutableSetOf<String >(" api" , " runtimeOnly" , " implementation" )
30
+ /* *
31
+ * 处理过工程的
32
+ */
33
+ var handleProjectSet = mutableSetOf<Project >()
31
34
32
35
/* *
33
36
* 从根工程开始向下替换依赖
34
37
*/
35
38
fun replaceDependency () {
36
- // val starTime = System.currentTimeMillis()
37
- // replaceSelfResolvingDependency()
38
- // val endTime = System.currentTimeMillis()
39
- // FastBuilderLogger.logLifecycle("替换files依赖耗时: ${endTime - starTime}")
40
-
41
- replaceDependency(pluginContext.getApplyProject())
39
+ // pluginContext.getApplyProject().rootProject.allprojects { project ->
40
+ replaceDependency(pluginContext.getApplyProject())
41
+ // }
42
+ // pluginContext.getApplyProject().rootProject.allprojects { project ->
43
+ // for (configuration in project.configurations) {
44
+ // for (dependency in configuration.dependencies) {
45
+ // if (dependency is ProjectDependency) {
46
+ // FastBuilderLogger.logLifecycle("BUG!!! >>>> ${project.name}:${configuration.name}:${dependency.name}")
47
+ //
48
+ // }
49
+ // }
50
+ // }
51
+ // }
42
52
}
43
53
44
-
45
54
/* *
46
- * 用于替换 api files('xxx.aar') 依赖为 api(name:"xxx",ext:"aar")
47
- * files依赖移到父亲后目录改变会触发重新编译打包导致merge错误
55
+ * 递归替换依赖
48
56
*/
49
- private fun replaceSelfResolvingDependency () {
50
- for (childProject in pluginContext.getApplyProject().rootProject.childProjects) {
51
- configFor@ for (configuration in childProject.value.configurations) {
52
- /* *
53
- * 此处是为了跳过不必要的config,只有后缀是api runtimeOnly implementation才处理
54
- */
55
- if (! DependencyUtils .configIsMatchEnd(configuration)) {
56
- continue @configFor
57
- }
58
-
59
- val mutableSet = mutableSetOf<Dependency >()
60
- mutableSet.addAll(configuration.dependencies) // 这里转成可变集合来操作
61
- for (dependency in mutableSet) {
62
- if (dependency is SelfResolvingDependency && dependency is FileCollectionDependency ) {
63
- val defaultSelfResolvingDependency = dependency as DefaultSelfResolvingDependency
64
- val fileCollection = defaultSelfResolvingDependency.files
65
- if (fileCollection.files.size != 1 ) {
66
- continue
67
- } else {
68
-
69
- val singleFile = fileCollection.singleFile
70
- if (! singleFile.name.endsWith(" aar" , true )) {
71
- continue
72
- }
73
- // 存在在执行拷贝操作
74
- val intoFile = File (pluginContext.getProjectExtension().thirdPartyAarsDir, singleFile.name)
75
- if (! intoFile.exists()) {
76
- FastBuilderLogger .logLifecycle(" 执行文件拷贝 $singleFile to $intoFile " )
77
-
78
- childProject.value.copy { copySpec: CopySpec ->
79
- copySpec.from(singleFile)
80
- copySpec.into(pluginContext.getProjectExtension().thirdPartyAarsDir)
81
- }
82
- }
83
- DependencyUtils .suppressionDeChange(configuration)
84
-
85
- configuration.dependencies.remove(dependency)
86
- configuration.dependencies.add(
87
- DependencyUtils .obtainDependency(
88
- pluginContext,
89
- singleFile.name.removeSuffix(" .aar" ),
90
- " aar"
91
- )
92
- )
93
- }
94
- }
95
- }
96
-
97
- }
98
- }
99
- }
57
+ fun replaceDependency (currentProject : Project , parent : Project ? = null) {
100
58
59
+ // if (handleProjectSet.contains(currentProject)) {
60
+ // return
61
+ // } else {
62
+ // handleProjectSet.add(currentProject)
63
+ // }
101
64
102
- /* *
103
- * 递归替换依赖
104
- */
105
- private fun replaceDependency (currentProject : Project , parent : Project ? = null) {
106
65
// 获取所有的模块工程集合
107
66
val moduleProjectList = pluginContext.getModuleProjectList()
108
67
@@ -111,6 +70,7 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
111
70
112
71
// 替换所有待处理的module工程依赖
113
72
for (configuration in currentProject.configurations) {
73
+
114
74
if (! DependencyUtils .configIsMatchEnd(configuration)) {
115
75
continue
116
76
}
@@ -129,23 +89,23 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
129
89
// 把下层的依赖投递到上层, 由于下层的 module 变成 aar 后会丢失它所引入的依赖,因此需要将这些依赖回传给上层
130
90
if (parent != null
131
91
// parent == pluginContext.getApplyProject() || (parent != null && moduleProject != null)
132
- /* && (moduleProject.cacheValid
133
- // fix: 上层module是aar依赖,下层module是源码依赖的情况
134
- || (parentCacheValid && !moduleProject.cacheValid) ))*/ ) {
92
+ /* && (moduleProject.cacheValid
93
+ // fix: 上层module是aar依赖,下层module是源码依赖的情况
94
+ || (parentCacheValid && !moduleProject.cacheValid) ))*/ ) {
135
95
FastBuilderLogger .logLifecycle(" 缓存拷贝 ${currentProject.name} >>>> ${parent.name} " )
136
96
// 原始类型
137
97
DependencyUtils .copyDependencyWithPrefix(currentProject, parent, " " )
138
98
// Debug 前缀类型
139
99
DependencyUtils .copyDependencyWithPrefix(currentProject, parent, " debug" )
140
100
// release前缀类型
141
- DependencyUtils .copyDependencyWithPrefix(currentProject, parent, " release" )
101
+ // DependencyUtils.copyDependencyWithPrefix(currentProject, parent, "release")
142
102
// 变体前缀
143
103
val flavorName = moduleProject?.moduleExtension?.flavorName
144
104
if (flavorName != null && flavorName.isNotBlank() && flavorName.isNotEmpty()) {
145
105
// api debugApi tiyaDebugApi
146
106
DependencyUtils .copyDependencyWithPrefix(currentProject, parent, flavorName)
147
107
DependencyUtils .copyDependencyWithPrefix(currentProject, parent, flavorName + " Debug" )
148
- DependencyUtils .copyDependencyWithPrefix(currentProject, parent, flavorName + " Release" )
108
+ // DependencyUtils.copyDependencyWithPrefix(currentProject, parent, flavorName + "Release")
149
109
}
150
110
}
151
111
}
@@ -198,7 +158,7 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
198
158
} else {
199
159
FastBuilderLogger .logLifecycle(" ${currentProject.name} 依赖 ${dependencyModuleProject.obtainName()} 没有命中缓存" )
200
160
// aar缓存无效,重新声明要构建的aar
201
- AARBuilderTask .prepare(pluginContext, dependencyModuleProject)
161
+ // AARBuilderTask.prepare(pluginContext, dependencyModuleProject)
202
162
}
203
163
}
204
164
0 commit comments