打正式包的時候,一般不允許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)
}
}
}