gradle kotlin
Building Gradle plugins isn’t new, and Kotlin isn’t new, and Android isn’t new. By the same token, building Gradle plugins in Kotlin isn’t new, and neither is building Gradle Plugins for Android apps. But building Gradle plugins, in Kotlin, for Android? Well, there’s really just not that much information out there on that.
构建Gradle插件不是新事物,Kotlin也不是新事物,Android也不是新事物。 同样,在Kotlin中构建Gradle插件并不是什么新鲜事,在Android应用程序中构建Gradle插件也不是新鲜事。 但是,在Kotlin中为Android构建Gradle插件吗? 好吧,实际上没有那么多的信息。
Before we get started, I’ll just mention that writing Android Gradle plugins is, in many ways, much easier if you write in Groovy. But what we wanted to do was easy, we wouldn’t be talking about it. So let’s just stick to the plan.
在我们开始之前,我仅提及在许多方面,如果您使用Groovy编写,则编写Android Gradle插件要容易得多。 但是我们想要做的事情很简单,我们不会在谈论它。 因此,让我们坚持计划。
Android部分 (The Android Part)
So there are lots of resources out there on building plugins in Kotlin, and many are great, so if you’re just starting from scratch, maybe go and google that before continuing, because we’re going to skip right to the good stuff: How do you get access to the Android junk in inside your Kotlin Gradle plugin? Glad you asked.
因此,有很多资源可以在Kotlin上构建插件,并且很多资源都很棒,因此,如果您只是从头开始,也许在继续之前先搜索一下Google,因为我们将直接跳到好东西: 您如何在Kotlin Gradle插件中访问Android垃圾? 很高兴你问。
First of all, what do we mean by “Android junk?” Well, when your gradle build script runs to build your Android app, the android application plugin is going to add a bunch of tasks to your build, and you’re going to configure those tasks in the android closure that the plugin uses for configuration. Your android closure in your build.gradle file might look something like this:
首先,“ Android垃圾”是什么意思? 好吧,当您的gradle构建脚本运行以构建Android应用程序时,android应用程序插件将向您的构建中添加一堆任务,并且您将在该插件用于配置的android闭包中配置这些任务。 您的build.gradle文件中的android关闭可能看起来像这样:
android {
compileSdkVersion 29
buildToolsVersion '29.0.2'
defaultConfig {
applicationId "com.my.androidapp"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName 1.0.0
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
debuggable true
}
}
}
And if you’re creating your own plugin, you might want to know some of the stuff in that closure, or other stuff the AGP is doing. For groovy, this is all pretty easy and straightforward, because groovy doesn’t require you to explicitly import anything. But with Kotlin, you can’t get away with that. So, the first thing you need to do, is add a dependency in your Gradle plugin’s build.gradle file on the Android Gradle Plugin. So, in your plugin’s build.gradle script, you’ll need:
而且,如果您要创建自己的插件,则可能想了解该闭包中的某些内容或AGP正在执行的其他内容。 对于groovy来说,这都是非常容易和直接的,因为groovy不需要您显式导入任何内容。 但是有了Kotlin,您将无法摆脱。 因此,您需要做的第一件事就是在Android Gradle插件上的Gradle插件的build.gradle文件中添加一个依赖项。 因此,在插件的build.gradle脚本中,您需要:
dependencies {
implementation gradleApi()
implementation 'com.squareup:javapoet:1.10.0'
implementation 'com.squareup:kotlinpoet:1.0.0-RC1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compileOnly("com.android.tools.build:gradle:3.6.3")
}
The key here is the last line. We’re using compileOnly, since, at runtime, this dependency will be provided by the build.script of the Android application that applies your plugin, in its build script. All we need is for your plugin to be aware of the AGP, so we can use the APIs it provides.
关键是最后一行。 我们正在使用compileOnly ,因为在运行时,依赖项将由应用程序插件的Android应用程序的build.script在其构建脚本中提供。 我们所需要的只是让您的插件了解AGP,因此我们可以使用其提供的API。
Kotlin部分 (The Kotlin Part)
Now that the dependency is included, our plugin can finally get access to that sweet Android junk:
现在已经包含了依赖项,我们的插件终于可以访问该可爱的Android垃圾了:
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.StringTokenizer
import com.android.build.gradle.AppExtension
import org.gradle.api.tasks.SourceTask
class ResourceProviderPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create<ResourceProviderPluginExtension>(RP_PLUGIN_NAME, ResourceProviderPluginExtension::class.java)
project.afterEvaluate {
val appExtension = project.extensions.findByType(AppExtension::class.java)
appExtension?.applicationVariants?.all { variant ->
if (extension.packageName == null) {
extension.packageName = variant.applicationId
}
val processResourcesTask = project.tasks.getByName("process${variant.name.capitalize()}Resources")
val rpTask = it.task("generate${variant.name.capitalize()}ResourceProvider") {
it.doLast {
generateResourceProviderForVariant(project, extension, variant.name.decapitalize())
}
}.dependsOn(processResourcesTask)
val variantNamePathComponent = variant.name.decapitalize()
val outputDir = "${project.buildDir}/generated/source/resourceprovider/${variantNamePathComponent}"
variant.registerJavaGeneratingTask(rpTask, File(outputDir))
val kotlinCompileTask = it.tasks.findByName("compile${variant.name.capitalize()}Kotlin") as? SourceTask
if (kotlinCompileTask != null) {
kotlinCompileTask.dependsOn(rpTask)
val srcSet = it.objects.sourceDirectorySet("resourceprovider", "resourceprovider").srcDir(outputDir)
kotlinCompileTask.source(srcSet)
}
}
}
}
//...
}
If you want to get access to the android closure in your plugin, first you’ll need to get an instance of the build’s AppExtension. Do this using the line:
如果您想访问插件中的android闭包,首先需要获取构建的AppExtension的实例。 使用以下行执行此操作:
val appExtension = project.extensions.findByType(AppExtension::class.java)
val appExtension = project.extensions.findByType(AppExtension :: class.java)
Once you have the AppExtension instance, you can do all kinds of stuff, like interrogate the app variants, add tasks into the Android build lifecycle using the dependsOn() function, and even, as suggested by the example above, generate new source files and compile them into your project.
有了AppExtension实例后,您就可以执行各种工作,例如询问应用程序变体,使用dependsOn()函数将任务添加到Android构建生命周期中,甚至按照上述示例的建议生成新的源文件并将它们编译到您的项目中。
Writing an Android Gradle Plugin in Kotlin isn’t too difficult, once you know the tricks of the trade. For the complete source of the project all this came from, head over to https://github.com/Comcast/resourceprovider2 to see how we did it.
一旦您了解交易技巧,就可以在Kotlin中编写Android Gradle插件并不难。 有关项目的完整来源,请转至https://github.com/Comcast/resourceprovider2 ,了解我们是如何做到的。
And if you haven’t already, also check out my article on The New ResourceProvider.
如果您还没有的话,还请查看我在The New ResourceProvider上的文章。
翻译自: https://medium.com/swlh/how-to-build-a-gradle-plugin-for-android-in-kotlin-9a5772575f93
gradle kotlin



所有评论(0)