Last active
June 2, 2020 18:19
-
-
Save PaoloMilano/f67be047d70d05087cac19d97baeaa92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package extensions | |
| import com.android.build.gradle.AppExtension | |
| import com.android.build.gradle.BaseExtension | |
| import com.android.build.gradle.FeatureExtension | |
| import com.android.build.gradle.LibraryExtension | |
| import com.android.build.gradle.api.BaseVariant | |
| import org.gradle.api.DomainObjectSet | |
| import org.gradle.api.GradleException | |
| import org.gradle.api.Project | |
| import java.io.File | |
| fun Project.android(): BaseExtension { | |
| val android = project.extensions.findByType(BaseExtension::class.java) | |
| if (android != null) { | |
| return android | |
| } else { | |
| throw GradleException("Project $name is not an Android project") | |
| } | |
| } | |
| fun BaseExtension.variants(): DomainObjectSet<out BaseVariant> { | |
| return when (this) { | |
| is AppExtension -> { | |
| applicationVariants | |
| } | |
| is FeatureExtension -> { | |
| featureVariants | |
| } | |
| is LibraryExtension -> { | |
| libraryVariants | |
| } | |
| else -> throw GradleException("Unsupported BaseExtension type!") | |
| } | |
| } | |
| // New extension to write pre-formatted xml within resource tags | |
| fun File.writeXlmWithTags(body: String) { | |
| ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + | |
| "<resources>" + | |
| "$body\n" + | |
| "</resources>") | |
| .also { resXml -> | |
| try { | |
| createNewFile() | |
| writeText(resXml) | |
| } catch (e: Exception) { | |
| throw GradleException(e.message) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment