Skip to content

Commit e530b12

Browse files
修复兼容问题
1 parent 015e394 commit e530b12

File tree

7 files changed

+188
-115
lines changed

7 files changed

+188
-115
lines changed

src/main/kotlin/org/lizhi/tiya/dependency/DependencyReplaceHelper.kt

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,95 +14,54 @@
1414
package org.lizhi.tiya.dependency
1515

1616
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
2021
import org.lizhi.tiya.log.FastBuilderLogger
2122
import org.lizhi.tiya.plugin.IPluginContext
2223
import org.lizhi.tiya.task.AARBuilderTask
23-
import java.io.File
2424

2525
/**
2626
* 依赖替换帮助类
2727
*/
2828
class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
2929

30-
private val configList = mutableSetOf<String>("api", "runtimeOnly", "implementation")
30+
/**
31+
* 处理过工程的
32+
*/
33+
var handleProjectSet = mutableSetOf<Project>()
3134

3235
/**
3336
* 从根工程开始向下替换依赖
3437
*/
3538
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+
// }
4252
}
4353

44-
4554
/**
46-
* 用于替换 api files('xxx.aar') 依赖为 api(name:"xxx",ext:"aar")
47-
* files依赖移到父亲后目录改变会触发重新编译打包导致merge错误
55+
* 递归替换依赖
4856
*/
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) {
10058

59+
// if (handleProjectSet.contains(currentProject)) {
60+
// return
61+
// } else {
62+
// handleProjectSet.add(currentProject)
63+
// }
10164

102-
/**
103-
* 递归替换依赖
104-
*/
105-
private fun replaceDependency(currentProject: Project, parent: Project? = null) {
10665
// 获取所有的模块工程集合
10766
val moduleProjectList = pluginContext.getModuleProjectList()
10867

@@ -111,6 +70,7 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
11170

11271
// 替换所有待处理的module工程依赖
11372
for (configuration in currentProject.configurations) {
73+
11474
if (!DependencyUtils.configIsMatchEnd(configuration)) {
11575
continue
11676
}
@@ -129,23 +89,23 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
12989
// 把下层的依赖投递到上层, 由于下层的 module 变成 aar 后会丢失它所引入的依赖,因此需要将这些依赖回传给上层
13090
if (parent != null
13191
// 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) ))*/) {
13595
FastBuilderLogger.logLifecycle("缓存拷贝 ${currentProject.name} >>>> ${parent.name}")
13696
// 原始类型
13797
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, "")
13898
// Debug 前缀类型
13999
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, "debug")
140100
// release前缀类型
141-
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, "release")
101+
// DependencyUtils.copyDependencyWithPrefix(currentProject, parent, "release")
142102
// 变体前缀
143103
val flavorName = moduleProject?.moduleExtension?.flavorName
144104
if (flavorName != null && flavorName.isNotBlank() && flavorName.isNotEmpty()) {
145105
//api debugApi tiyaDebugApi
146106
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, flavorName)
147107
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, flavorName + "Debug")
148-
DependencyUtils.copyDependencyWithPrefix(currentProject, parent, flavorName + "Release")
108+
// DependencyUtils.copyDependencyWithPrefix(currentProject, parent, flavorName + "Release")
149109
}
150110
}
151111
}
@@ -198,7 +158,7 @@ class DependencyReplaceHelper(private val pluginContext: IPluginContext) {
198158
} else {
199159
FastBuilderLogger.logLifecycle("${currentProject.name} 依赖 ${dependencyModuleProject.obtainName()} 没有命中缓存")
200160
// aar缓存无效,重新声明要构建的aar
201-
AARBuilderTask.prepare(pluginContext, dependencyModuleProject)
161+
// AARBuilderTask.prepare(pluginContext, dependencyModuleProject)
202162
}
203163
}
204164

src/main/kotlin/org/lizhi/tiya/dependency/DependencyUtils.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import org.gradle.api.Project
1717
import org.gradle.api.artifacts.Configuration
1818
import org.gradle.api.artifacts.Dependency
1919
import org.gradle.api.artifacts.ModuleDependency
20+
import org.gradle.api.artifacts.ProjectDependency
2021
import org.gradle.api.internal.DefaultDomainObjectSet
2122
import org.gradle.internal.ImmutableActionSet
2223
import org.joor.Reflect
2324
import org.lizhi.tiya.log.FastBuilderLogger
2425
import org.lizhi.tiya.plugin.IPluginContext
26+
import org.lizhi.tiya.project.ModuleProject
2527

2628
/**
2729
* 依赖处理工具类
@@ -60,7 +62,8 @@ object DependencyUtils {
6062
}
6163
}
6264

63-
val configEndNameList = mutableSetOf("api", "runtimeOnly", "implementation","compileOnly")
65+
66+
val configEndNameList = mutableSetOf("api", "runtimeOnly", "implementation", "compileOnly")
6467

6568
/**
6669
* 将currentProject的依赖copy到parentProject
@@ -84,13 +87,10 @@ object DependencyUtils {
8487

8588

8689
fun copyDependency(currentProject: Project, parentProject: Project, configName: String) {
87-
val srcConfig = currentProject.configurations.getByName(configName)
88-
val dstConfig = parentProject.configurations.getByName(configName)
89-
val parentContains = parentProject.configurations.names.contains(configName)
90-
if (parentContains) {
91-
// 必须要保证依赖配置的名字在父工程存在,比如子工程用了一个叫xxxApi的configName,父工程也需要存在
92-
copyDependency(srcConfig, dstConfig)
93-
}
90+
val srcConfig = currentProject.configurations.maybeCreate(configName)
91+
val dstConfig = parentProject.configurations.maybeCreate(configName)
92+
// 必须要保证依赖配置的名字在父工程存在,比如子工程用了一个叫xxxApi的configName,父工程也需要存在
93+
copyDependency(srcConfig, dstConfig)
9494
}
9595

9696
fun copyDependency(src: Configuration, dst: Configuration) {

0 commit comments

Comments
 (0)