gradlekotlin_如何在Kotlin中为Android构建Gradle插件

gradlekotlin_如何在Kotlin中为Android构建Gradle插件

2023年7月3日发(作者:)

gradlekotlin_如何在Kotlin中为Android构建Gradle插件gradle kotlinBuilding Gradle plugins isn’t new, and Kotlin isn’t new, and Android isn’t new. By the same token, building Gradle pluginsin Kotlin isn’t new, and neither is building Gradle Plugins for Android apps. But building Gradle plugins, in Kotlin, forAndroid? 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 inGroovy. 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 fromscratch, maybe go and google that before continuing, because we’re going to skip right to the good stuff: How do you getaccess to the Android junk in inside your Kotlin Gradle plugin? Glad you asked.因此,有很多资源可以在Kotlin上构建插件,并且很多资源都很棒,因此,如果您只是从头开始,也许在继续之前先搜索⼀下Google,因为我们将直接跳到好东西: 您如何在Kotlin Gradle插件中访问Android垃圾? 很⾼兴你问。Welcome…欢迎…First of all, what do we mean by “Android junk?” Well, when your gradle build script runs to build your Android app, theandroid application plugin is going to add a bunch of tasks to your build, and you’re going to configure those tasks in theandroid closure that the plugin uses for configuration. Your android closure in your file might look somethinglike this:⾸先,“ Android垃圾”是什么意思? 好吧,当您的gradle构建脚本运⾏以构建Android应⽤程序时,android应⽤程序插件将向您的构建中添加⼀堆任务,并且您将在该插件⽤于配置的android闭包中配置这些任务。 您的⽂件中的android关闭可能看起来像这样:android { compileSdkVersion 29 buildToolsVersion '29.0.2' defaultConfig { applicationId "dapp" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName 1.0.0 } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile(''), '' } 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 isdoing. For groovy, this is all pretty easy and straightforward, because groovy doesn’t require you to explicitly importanything. But with Kotlin, you can’t get away with that. So, the first thing you need to do, is add a dependency in yourGradle plugin’s file on the Android Gradle Plugin. So, in your plugin’s script, you’ll need:⽽且,如果您要创建⾃⼰的插件,则可能想了解该闭包中的某些内容或AGP正在执⾏的其他内容。 对于groovy来说,这都是⾮常容易和直接的,因为groovy不需要您显式导⼊任何内容。 但是有了Kotlin,您将⽆法摆脱。 因此,您需要做的第⼀件事就是在Android Gradle插件上的Gradle插件的⽂件中添加⼀个依赖项。 因此,在插件的脚本中,您需要:dependencies { implementation gradleApi() implementation 'up:javapoet:1.10.0' implementation 'up:kotlinpoet:1.0.0-RC1' implementation ":kotlin-stdlib-jdk8:$kotlin_version" compileOnly(":gradle:3.6.3")}The key here is the last line. We’re using compileOnly, since, at runtime, this dependency will be provided by of the Android application that applies your plugin, in its build script. All we need is for your plugin to be awareof the AGP, so we can use the APIs it provides.关键是最后⼀⾏。 我们正在使⽤compileOnly ,因为在运⾏时,依赖项将由应⽤程序插件的Android应⽤程序的在其构建脚本中提供。 我们所需要的只是让您的插件了解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 import timport port mport mport Tokenizerimport ensionimport Taskclass ResourceProviderPlugin : Plugin { override fun apply(project: Project) { val extension = (RP_PLUGIN_NAME, ResourceProviderPluginExtension::) valuate { val appExtension = Type(AppExtension::) appExtension?.applicationVariants?.all { variant -> if (eName == null) { eName = ationId }

val processResourcesTask = ame("process${lize()}Resources") val rpTask = ("generate${lize()}ResourceProvider") { { generateResourceProviderForVariant(project, extension, talize()) } }.dependsOn(processResourcesTask) val variantNamePathComponent = talize() val outputDir = "${ir}/generated/source/resourceprovider/${variantNamePathComponent}" erJavaGeneratingTask(rpTask, File(outputDir)) val kotlinCompileTask = Name("compile${lize()}Kotlin") as? SourceTask if (kotlinCompileTask != null) { sOn(rpTask) val srcSet = DirectorySet("resourceprovider", "resourceprovider").srcDir(outputDir) (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 . Do thisusing the line:如果您想访问插件中的android闭包,⾸先需要获取构建的的实例。 使⽤以下⾏执⾏此操作:val appExtension = Type(AppExtension::)val appExtension = Type(AppExtension :: )Once you have the AppExtension instance, you can do all kinds of stuff, like interrogate the app variants, add tasks into theAndroid build lifecycle using the

dependsOn() function, and even, as suggested by the example above, generate new sourcefiles and compile them into your project.有了AppExtension实例后,您就可以执⾏各种⼯作,例如询问应⽤程序变体,使⽤dependsOn()函数将任务添加到Android构建⽣命周期中,甚⾄按照上述⽰例的建议⽣成新的源⽂件并将它们编译到您的项⽬中。And if you haven’t already, also check out my article on如果您还没有的话,还请查看我在上⽂章gradle kotlin

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688384817a130052.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信