打正式包的时候,一般不允许sdk使用snapshot版本来封板,打封板包的时候如果检测到有snapshot依赖,直接退出打包。下面是依赖检测的脚本,免费贡献给大家参考。
gradle.projectsEvaluated{
//收集项目的直接依赖信息
def extension_dep_check = project.extensions.getByName("android")
extension_dep_check.applicationVariants.all { variant ->
println(variant.name) //debug、release
def task = project.tasks.create("checkSnapshot" + variant.name.capitalize())
task.doFirst{
Set<String> deps = new HashSet<>()
project.rootProject.buildscript.configurations.classpath.dependencies.each { dep ->
if (dep.version != null && dep.group != null && (dep.version.endsWith('-SNAPSHOT') || dep.version.endsWith('+'))) {
deps.add("${dep.group}:${dep.name}:${dep.version}" + '\r\n')
}
}
//这里检测的是Compile的依赖,要想检测runtime依赖,可以改成RuntimeClasspath
def configuration = project.configurations."${variant.name}CompileClasspath"
configuration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.each {
parse_snapshot_dep(it,deps)
}
//kapt依赖检测
def kapt_configuration = project.configurations."kapt${variant.name.capitalize()}"
kapt_configuration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.each {
parse_snapshot_dep(it,deps)
}
File dir = new File(project.projectDir.absolutePath + "/dependencies")
if(!dir.exists()){
dir.mkdir()
}
def inputFile = new File(dir.absolutePath, "dependencies-snapshot.txt")
if(inputFile.exists()){
inputFile.delete()
}
inputFile.createNewFile()
deps.each {
inputFile.append(it)
}
}
task.group = "check"
task.dependsOn variant.getPreBuildProvider().get()
}
}
def void parse_snapshot_dep(dependency, deps){
def identifier = dependency.module.id
if (identifier.version != "null" && identifier.version != "unspecified"
&& (identifier.version.endsWith('-SNAPSHOT') || identifier.version.endsWith('+'))) {
deps.add("${identifier.group}:${identifier.name}:${identifier.version}" + '\r\n')
}
if(dependency.children != null && dependency.children.size() > 0){
dependency.children.each{
parse_snapshot_dep(it, deps)
}
}
}
网友评论