Gradle subproject conditional plugin configuration with gradle kotlin dsl - gradle

This is how I've previously configured subprojects in gradle if a certain plugin was present:
subprojects {
repositories {
jcenter()
google()
}
plugins.withType(com.android.build.gradle.LibraryPlugin) {
android {
compileSdkVersion 29
...
}
}
}
Now I'm not working with groovy anymore. How can I achieve the same thing with gradle kotlin DSL?
Biggest problem here seems to be to find a way to access the android extension, I can't seem to access it using android nor extensions.findByType(com.android.build.gradle.LibraryExtension::class.java).

I solved this problem by creating a precompiled script plugin, which I applied to all subprojects.
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
dependencies {
implementation("com.android.tools.build:gradle:4.1.2")
}
buildSrc/src/main/kotlin/android-libary.gradle.kts
plugins {
id("com.android.library")
}
android {
compileSdkVersion(29)
...
}
subProject/build.gradle.kts
plugins {
`android-library`
}

Related

Gradle single-project pluginManagement block not working (Kotlin DSL)

I need to change a multi-project build to a single-project build, as there is and only ever will be one project in this repo. Currently, in settings.gradle, I have a custom plugin repo that currently uses a pluginManagement block with resolutionStrategy and my list of repo's:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.meanwhileinhell.plugin') {
useModule('com.meanwhileinhell:gradle-plugin:1.0.0-SNAPSHOT')
}
}
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
// Meanwhileinhell repo
maven {
url "s3://mvn.meanwhileinhell.com/releases"
credentials(AwsCredentials) {
accessKey s3_access_key
secretKey s3_access_secret
}
}
}
plugins {
...
...
}
}
However, deleting settings.gradle and moving this block into my build.gradle.kts (Kotlin DSL) seems to do nothing. I've tried wrapping in a
configurations {
all {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
and also
settings {
pluginManagement {
resolutionStrategy {
eachPlugin {
...
}
}
}
}
I found a SO answer that used settingsEvaluated in order to get the settings object, but again this was a no go.
Currently my build.gradle.kts looks like this, without pulling any plugin in from my repo:
val springBootVersion: String by project
group = "com.meanwhileinhell.myapp"
version = "$version"
repositories {
mavenCentral()
mavenLocal()
maven ("https://repo.spring.io/snapshot")
maven ("https://repo.spring.io/milestone")
maven ("https://plugins.gradle.org/m2/")
maven {
url = uri("s3://mvn.meanwhileinhell.com/releases")
credentials(AwsCredentials::class) {
accessKey = (project.property("s3_access_key") as String)
secretKey = (project.property("s3_access_secret") as String)
}
}
}
plugins {
base
eclipse
idea
java
id("io.spring.dependency-management") version "1.0.9.RELEASE"
// Load but don't apply to root project
id("org.springframework.boot") version "1.5.14.RELEASE" apply false
}
dependencies {
...
}
Whenever I try to add a plugin id like id("com.meanwhileinhell.plugin.hell2java") version "1.0.0-SNAPSHOT" I get an error that looks like it isn't even looking in my S3 location:
* What went wrong:
Plugin [id: 'com.meanwhileinhell.plugin.hell2java', version: '1.0.0-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.meanwhileinhell.plugin.hell2java:com.meanwhileinhell.plugin.hell2java.gradle.plugin:1.0.0-SNAPSHOT')
Searched in the following repositories:
Gradle Central Plugin Repository
Any help on this would be appreciated!
EDIT !!!! -----------------------
I've just found this in the Gradle docs:
https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management
The pluginManagement {} block may only appear in either the settings.gradle file....
Looks like I'm going down the wrong way entirely, so will look into the initialisation script route.
I think you may have missed something about the file structure.
In the Groovy DSL, you have the following files:
build.gradle
settings.gradle
init.gradle
In the Kotlin DSL, you have the same files but with the .kts extension:
build.gradle.kts
settings.gradle.kts
init.gradle.kts
The Kotlin DSL doesn't differ to the Groovy DSL in where to put things. pluginManagement need to go in to the settings file, so for Kotlin that would be settings.gradle.kts. If you are in doubt, look at the documentation. For almost all code examples, you can switch between Groovy and Kotlin DSL to see how to do it (and which files they are supposed go to into).

Add specific plugin version in Gradle using 'Apply'

How to apply a specific plugin version using newer Gradle syntax? I would like to do something like this but this gives an error of unknown property 'version':
apply plugin: 'com.bmuschko.docker-remote-api', version: '2.0.3'
The new plugin syntax can be seen on the Gradle Plugins Repository page for the plugin you wish to apply: https://plugins.gradle.org/plugin/com.bmuschko.docker-remote-api
Using the plugins DSL:
plugins {
id "com.bmuschko.docker-remote-api" version "6.1.3"
}
Using legacy plugin application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.bmuschko:gradle-docker-plugin:6.1.3"
}
}
apply plugin: "com.bmuschko.docker-remote-api"
In your build.gradle file, apply the plugin with a plugins block near the top of your script:
plugins {
id "com.bmuschko.docker-remote-api" version "2.0.3"
}
The syntax you have there is not new, it the legacy plugin application
To specify the version for the legacy way, you need to use the buildscript { } block:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.bmuschko:gradle-docker-plugin:2.0.3"
}
}
apply plugin: "com.bmuschko.docker-remote-api"

Gradle plugin with custom group id

Gradle 6.1
I am having difficulties to use the new plugin configuration mode in Gradle with a custom plugin coming from a custom repository.
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
}
plugins {
java
idea
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
I get this error:
Plugin [id: 'com.custom.gradle.plugin.myplugin', version: '1.1.0'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.custom.gradle.plugin.myplugin:com.custom.gradle.plugin.myplugin:1.1.0')
Searched in the following repositories:
Gradle Central Plugin Repository
Gradle will use the plugin id as its group id.
It works if I use the old ways:
buildscript {
repositories {
maven {
url = uri("https://custom")
}
mavenCentral()
jcenter()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.custom:com.custom.gradle.plugin.myplugin:1.1.0")
}
}
apply(plugin = "com.custom.gradle.plugin.myplugin")
Is there a way to specify the group id with the 'id' command? Or am I breaking the plugin definition's contract with that old plugin?
In order to use the newer/preferred plugins { } DSL, the custom plugin must publish a plugin marker artifact.
If the custom plugin is able to be modified, then I suggest updating to make use of the Java Gradle Plugin Development plugin which will create the marker for you.
If the plugin is not able to be updated, then you can still use the plugins { } block, but you'll need to manually resolve the plugin:
In the main build.gradle:
plugins {
id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}
Then resolve the plugin manually in settings.gradle:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.custom.gradle.plugin.myplugin") {
useModule("com.custom:com.custom.gradle.plugin.myplugin:${requested.version}")
}
}
}
}
See Plugin Resolution Rules
for more details.

Android subprojects with Kotlin DSL

I'm working on migrating by Gradle build files to the Kotlin DSL and I encounter an issue.
On my parent build.gradle, I have the following piece of code
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Version.kotlin}"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate { project ->
if (project.plugins.findPlugin('com.android.application') ?:
project.plugins.findPlugin('com.android.library')) {
android {
compileSdkVersion = Android.SDK_COMPILE
defaultConfig {
minSdkVersion Android.SDK_MIN
targetSdkVersion Android.SDK_TARGET
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
This allows me to configure in only one place all the modules that are android applications or libraries.
However this doesn't seem to work when I migrated to kotlin:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(Dependency.androidGradle)
classpath(Dependency.kotlinGradle)
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate {
if (project.plugins.findPlugin("com.android.application") != null ||
project.plugins.findPlugin("com.android.library") != null) {
android { <<<<------------ Unresolved reference: android
compileSdkVersion(Android.SDK_COMPILE)
defaultConfig {
minSdkVersion(Android.SDK_MIN)
targetSdkVersion(Android.SDK_TARGET)
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
The error is Unresolved reference: android and it looks like the android{} block is not recognized by the script compiler.
My theory is that the if checking for the subproject type is not enough, and I might have to cast or get a reference to some object in which I can call the android{} block, but honestly I do not know enough.
Any clues?
Gradle Kotlin DSL determines the dependencies for each script from the gradle classpath plus the applied plugins. That's why its recommended to use the plugins { ... } block in the Kotlin DSL.
You need to add the android and kotlin plugins to your root without applying it.
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
Unfortunately, that will still not generate the static accessors for the root build script but it will give you access to the plugin classes within the script and you can reference them like:
subprojects {
// BasePlugin is the common superclass of the AppPlugin and LibraryPlugin which are the plugin classes that "com.android.application" and "com.android.library" apply
plugins.withType<BasePlugin> {
// BaseExtension is the common superclass of the AppExtension and LibraryExtension which are the extension classes registered by the two plugins to the name "android"
configure<BaseExtension> {
// This block is typed correctly
defaultConfig {
// ...
}
}
}
}

Adding external source files to a kotlin project

I have Kotlin sources located at, say, repo/project_a/src/. I created a Kotlin Gradle project in IntelliJ IDEA, located at repo/project_b/.... And I can't for the life of me figure out how to add the sources. If I add them through project structure menu it works fine, but as soon as it wants to re-read the gradle file id deletes the structure (It warns as much in the UI).
This is my gradle file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.70'
}
group 'cli'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
I've tried adding all variations of
sourceSets {
main {
kotlin {
srcDirs += "repo/project_a/"
}
}
}
But it does absolutely nothing.
Any ideas?
The path you are giving to Gradle will compile to the current project path plus "repo/project_a/". Try with:
sourceSets {
main {
kotlin {
srcDirs += "../project_a/"
}
}
}

Resources