gradle模糊移除module
时间: 2024-01-30 08:10:26 浏览: 101
以下是在Gradle中模糊移除module的步骤:
1.在根目录的build.gradle文件中添加以下代码:
```gradle
task cleanProject(type: Delete) {
delete rootProject.buildDir
}
def getProjectNames() {
def projectNames = []
rootProject.subprojects.each { project ->
projectNames.add(project.name)
}
return projectNames
}
def getProject(name) {
return rootProject.project(name)
}
def removeProject(name) {
def project = getProject(name)
rootProject.subprojects.remove(project)
project.parent = null
}
def removeProjects(names) {
names.each { name ->
removeProject(name)
}
}
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(clean)) {
removeProjects(getProjectNames() - ['app'])
}
}
```
2.在需要移除的module的build.gradle文件中添加以下代码:
```gradle
android {
// ...
}
task removeThisProject {
doLast {
def projectPath = project.path
def projectName = project.name
def rootProject = project.rootProject
rootProject.removeProject(projectName)
println "Removed project $projectPath"
}
}
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(':app:clean')) {
removeThisProject.execute()
}
}
```
3.运行以下命令来清理项目并移除指定的module:
```shell
./gradlew clean
```
请注意,这种方法只适用于移除子项目,而不适用于移除根项目。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)