Android Extensions: put new functionality under the flag

This commit is contained in:
Yan Zhulanow
2017-07-14 18:55:55 +03:00
parent 77eafb9716
commit 4851a83a83
21 changed files with 358 additions and 30 deletions
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.internal
open class AndroidExtensionsExtension {
open var isExperimental: Boolean = false
}
@@ -29,15 +29,17 @@ import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.AbstractCompile
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil.*
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.android.AndroidGradleWrapper
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.w3c.dom.Document
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
// Use apply plugin: 'kotlin-android-extensions' to enable Android Extensions in an Android project.
// Just a marker plugin.
class AndroidExtensionsSubpluginIndicator : Plugin<Project> {
override fun apply(project: Project) {
project.extensions.create("androidExtensions", AndroidExtensionsExtension::class.java)
val kotlinPluginWrapper = project.plugins.findPlugin(KotlinAndroidPluginWrapper::class.java) ?: run {
project.logger.error("'kotlin-android' plugin should be enabled before 'kotlin-android-extensions'")
return
@@ -83,13 +85,55 @@ class AndroidSubplugin : KotlinGradleSubplugin<KotlinCompile> {
variantData: Any?,
androidProjectHandler: Any?,
javaSourceSet: SourceSet?
): List<SubpluginOption> {
val androidExtension = project.extensions.getByName("android") as? BaseExtension ?: return emptyList()
val androidExtensionsExtension = project.extensions.getByType(AndroidExtensionsExtension::class.java)
if (androidExtensionsExtension.isExperimental) {
return applyExperimental(androidExtension, project, variantData, androidProjectHandler)
}
val sourceSets = androidExtension.sourceSets
val pluginOptions = arrayListOf<SubpluginOption>()
val mainSourceSet = sourceSets.getByName("main")
val manifestFile = mainSourceSet.manifest.srcFile
val applicationPackage = getApplicationPackageFromManifest(manifestFile) ?: run {
project.logger.warn(
"Application package name is not present in the manifest file (${manifestFile.absolutePath})")
""
}
pluginOptions += SubpluginOption("package", applicationPackage)
fun addVariant(sourceSet: AndroidSourceSet) {
pluginOptions += SubpluginOption("variant", sourceSet.name + ';' +
sourceSet.res.srcDirs.joinToString(";") { it.absolutePath })
}
addVariant(mainSourceSet)
val flavorSourceSets = AndroidGradleWrapper.getProductFlavorsSourceSets(androidExtension).filterNotNull()
for (sourceSet in flavorSourceSets) {
addVariant(sourceSet)
}
return pluginOptions
}
private fun applyExperimental(
androidExtension: BaseExtension,
project: Project,
variantData: Any?,
androidProjectHandler: Any?
): List<SubpluginOption> {
@Suppress("UNCHECKED_CAST")
androidProjectHandler as? AbstractAndroidProjectHandler<Any?> ?: return emptyList()
val androidExtension = project.extensions.getByName("android") as? BaseExtension ?: return emptyList()
val pluginOptions = arrayListOf<SubpluginOption>()
pluginOptions += SubpluginOption("experimental", "true")
val mainSourceSet = androidExtension.sourceSets.getByName("main")
pluginOptions += SubpluginOption("package", getApplicationPackage(project, mainSourceSet))