in generator/src/main/kotlin/com/android/gradle/replicator/generator/project/GradleProjectGenerator.kt [57:135]
override fun generateRootModule(project: ProjectInfo) {
dslWriter.newBuildFile(destinationFolder)
val rootPlugins = project.rootModule.plugins
val allPlugins = project.getAllPlugins()
val requiresBuildscript = allPlugins.any { !it.useNewDsl(project) }
if (requiresBuildscript) {
dslWriter.block("buildscript") {
block("repositories") {
google()
jcenter()
}
block("dependencies") {
// now prepare the classpaths for plugins *not* using the new DSL
val classpaths = allPlugins.asSequence()
.filter { !it.useNewDsl(project) }
.mapNotNull {
when (it) {
PluginType.ANDROID_APP, PluginType.ANDROID_LIB, PluginType.ANDROID_TEST, PluginType.ANDROID_DYNAMIC_FEATURE -> {
"com.android.tools.build:gradle:${project.agpVersion}"
}
else -> throw RuntimeException("Unexpected plugin requiring buildscript: ${it.id}")
}
}.toSet()
for (cp in classpaths) {
call("classpath", asString(cp))
}
}
}
}
// Because some AGP versions do not support the new DSL, we need a mix and match of both of DSL.
// First figure out the full list of plugins knowing whether it's applied to the root module or not.
// build a list of plugins to put in the root module.
val rootPluginInfos = allPlugins
.asSequence()
.map { RootPluginInfo(plugin = it, applied = rootPlugins.contains(it)) }
.filter { (it.plugin.useNewDsl(project) && it.plugin.requireVersions) || it.applied }
.sortedBy { it.plugin.priority }
.toList()
if (rootPluginInfos.isNotEmpty()) {
dslWriter.block("plugins") {
for (pluginInfo in rootPluginInfos) {
val version: String? = when (pluginInfo.plugin) {
PluginType.JAVA, PluginType.JAVA_LIBRARY, PluginType.APPLICATION -> null
PluginType.KOTLIN_ANDROID, PluginType.KOTLIN_JVM, PluginType.KAPT -> project.kotlinVersion
PluginType.ANDROID_APP, PluginType.ANDROID_LIB, PluginType.ANDROID_TEST, PluginType.ANDROID_DYNAMIC_FEATURE -> project.agpVersion
else -> throw RuntimeException("Unexpected plugin in root plugin infos.")
}
pluginInBlock(pluginInfo.plugin, version, pluginInfo.applied)
}
}
}
dslWriter.block("allprojects") {
block("repositories") {
google()
jcenter()
block("maven") {
url("https://jitpack.io")
}
}
}
// now the generic module info stuff
generateModuleInfo(destinationFolder, project.rootModule)
// create module metadata files
generateModuleMetadata(destinationFolder, project.rootModule)
generateModuleResourceMetadata(destinationFolder, project.rootModule)
// create generation constants file
generateResgenDefaultConstants(destinationFolder)
}