https://kotlinlang.org logo
Title
d

Dylan

05/25/2020, 1:21 PM
Hi there, is it possible to create a Kotlin gradle plugin without using Kotlin DSL? I tried using Kotlin DSL. It works, but I need to use the basic build.gradle file instead. Unfortunately, I'm not able to figure out how my .gradle should looks like. Here's how it looks:
buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
    }
}

repositories {
    google()
    jcenter()
}

dependencies {
    implementation("com.squareup:kotlinpoet:1.5.0")
}
With that configuration, after trying to build my project, I have the following exception:
Caused by: org.gradle.api.plugins.InvalidPluginException: Could not find implementation class 'com.example.gradle.MyPlugin' for plugin 'MyPlugin' specified in [Path to buildSrc jar]
After looking into the Jar itself, it looks like
MyPlugin.properties
is here, but not the class
MyPlugin
. Do you have any idea?
g

gildor

05/25/2020, 1:26 PM
Yes, Kotlin DSL plugin is not required. Curious, why you don't want to use Kotlin DSL plugin? I didn't understand what kind config is above? Is it buildSrc build.gradle? Or it's standalone plugin project and its build.gradle?
d

Dylan

05/25/2020, 1:27 PM
Sorry, yes it's the build.gradle of buildSrc
I don't really want to use Kotlin DSL plugin yet because it's not fully supported in Android Studio.
g

gildor

05/25/2020, 1:30 PM
Hm, not clearly sure what you mean. Kotlin DSl is just a collection of helper function and some other tooling for writing Gradle plugins, you don't need any AS support for it, it will work for consumer as any other plugin
Anyway, you config doesn't looks as correct config of plugin. You should use at least java-gradle-plugin plugin to develop a plugin
The only difference that kotlin-dsl plugin applies it for you (and adds support for Precompiled Script Plugins too)
d

Dylan

05/25/2020, 1:38 PM
Kotlin DSL does work in Android Studio, Android Studio just doesn't support it yet to update values, or detect new dependencies. I tried adding
apply plugin: 'java-gradle-plugin'
at the top of build.gradle of buildSrc, but I still face the same issue.
Looks like I also need
apply plugin: 'kotlin'
? But I now run into a new issue. All org.gradle.* classes are not recognized in my plugin.
n

no

05/25/2020, 1:58 PM
do you have
compileOnly(gradleApi())
in your dependencies block?
d

Dylan

05/25/2020, 1:59 PM
I don't have it, no
Adding it doesn't change anything
j

Justin

05/25/2020, 3:25 PM
Maybe adding gradleApi was the fix and Android Studio's cache is being a punk. Try invalidating and restarting, syncing gradle, clean -> build, etc.
d

Dylan

05/25/2020, 3:36 PM
I did that already, unfortunately
This is how my build.gradle looks like, now:
apply plugin: 'java-gradle-plugin'
apply plugin: 'kotlin'

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
    }
}


repositories {
    google()
    jcenter()
}

dependencies {
    implementation gradleApi()
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72")
   implementation("com.squareup:kotlinpoet:1.5.0")
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
n

no

05/25/2020, 3:47 PM
what's the build failure you are getting?
d

Dylan

05/25/2020, 3:48 PM

https://kotlinlang.slack.com/files/UP93BG1NF/F014XH1K9QQ/image.png

Basically the APIs under org.gradle aren't available anymore for some reasons
g

gildor

05/25/2020, 3:48 PM
compileOnly(gradleApi())
is not required if java-gradle-plugin applied
@Dylan what is path to this source file with import problem?
d

Dylan

05/25/2020, 3:49 PM
buildSrc/src/main/java/...
g

gildor

05/25/2020, 3:50 PM
Just a note, you can apply both plugins in
plugins {}
block
d

Dylan

05/25/2020, 3:52 PM
I have this issue if I use
plugins {}
g

gildor

05/25/2020, 3:52 PM
Remove byildscript block, it's not needed
d

Dylan

05/25/2020, 3:53 PM
More issues, I think I'm going to keep apply plugin, but thanks
g

gildor

05/25/2020, 3:54 PM
But it's completely standard setup, maybe if you have issue its some another problem
Do you have buildSrc/settings.gradle file?
d

Dylan

05/25/2020, 3:55 PM
No I don't
The other issue I had while using plugins {} was:
* What went wrong:
Plugin [id: 'kotlin'] was not found in any of the following sources:

- Gradle Core Plugins (not a core plugin, please see <https://docs.gradle.org/5.6.4/userguide/standard_plugins.html> for available core plugins)
- Plugin Repositories (plugin dependency must include a version number for this source)
g

gildor

05/25/2020, 3:58 PM
Because plugin id is incorrect
d

Dylan

05/25/2020, 3:59 PM
Okay found the solution. But I still got the previous issue
org.gradle is still missing
This is how my build.gradle looks like now:
plugins {
    id 'java-gradle-plugin'
    id "org.jetbrains.kotlin.jvm" version "1.3.72"
}


repositories {
    google()
    jcenter()
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72")
    implementation("com.squareup:kotlinpoet:1.5.0")
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
g

gildor

05/25/2020, 4:02 PM
Is it IDE only issue or it cannot resolve it during build too?
d

Dylan

05/25/2020, 4:02 PM
It cannot resolve during build
g

gildor

05/25/2020, 4:03 PM
Maybe you have sample project It's standard setup, should work
Also, you have to configure gradlePlugin extension
I don't know, would it cause issue with Gradle API or not, but you anyway need this config
One more suggestion, I would update Gradle, if it possible. Such old Gradle probably causes all your issuea with Kotlin DSL plugin and AS support, a lot of related things improved from 5.x
d

Dylan

05/25/2020, 4:12 PM
I already use Gradle 5.6.4, but thank you.
I still need to figure out why org.gradle isn't recognized in my Kotlin file 🤔
Are there any docs about actually creating a Gradle plugin with Kotlin without using Kotlin DSL ? Anything I find is about Kotlin DSL
Looks like after creating a new project with the exact same build.gradle, it works. So I'll look into the issue in my real project.
After clearing cache, it's not working anymore. What am I missing? Shouldn't clearing cache actually fix the issue, not produce one?
Finally decided to completely remove the plugin, I'll do what my plugin was doing manually.
g

gildor

05/25/2020, 6:56 PM
It's exactly the same with Kotlin as with Java without Kotlin DSL, no difference
What kind cache you cleared? And what is not working?
d

Dylan

05/25/2020, 8:27 PM
Invalidate cache / Restart. And same bug above
g

gildor

05/26/2020, 1:18 AM
Could not find implementation class?
d

Dylan

05/26/2020, 2:47 AM
This

https://kotlinlang.slack.com/files/UP93BG1NF/F014XH1K9QQ/image.png

g

gildor

05/26/2020, 7:47 AM
I cannot reproduce it on sample project
also, have your tried with Java file?
but I still worry that it may be an issue of Gradle 5
n

no

05/26/2020, 8:00 AM
what version of the IDE are you using?
g

gildor

05/26/2020, 8:02 AM
Dylan says this issue reproducible also during build, not only IDE problem
d

Dylan

05/26/2020, 8:04 AM
Yes, I tried with a Java file as well. I use the latest stable version of Android Studio, but the issue isn't only in the IDE. Building my project fails while compiling buildSrc.
g

gildor

05/26/2020, 8:05 AM
I tried with a Java file as well
does it work?
d

Dylan

05/26/2020, 8:06 AM
No
For instance, as mentioned earlier, the issue (which made org.gradle APIs not available) only started to appear after invalidating the cache.
g

gildor

05/26/2020, 8:10 AM
What kind cache?
d

Dylan

05/26/2020, 8:10 AM
Invalidate cache/Restart
g

gildor

05/26/2020, 8:12 AM
in IDE? but it’s not related on gradle
d

Dylan

05/26/2020, 8:12 AM
But that's how the bug appears
g

gildor

05/26/2020, 8:13 AM
Maybe you have some wrong gradle wrapper implementation?
d

Dylan

05/26/2020, 8:13 AM
At least, in the test project I made, that's how it appeared
g

gildor

05/26/2020, 8:13 AM
have you tried to build from console
d

Dylan

05/26/2020, 8:13 AM
Yes
a

Arun

05/26/2020, 9:44 AM
For me when I start Gradle plugin projects, I prefer to use
gradle init
command which provides options to scaffold all the build scripts correctly. Please refer here https://docs.gradle.org/current/userguide/build_init_plugin.html
d

Dylan

05/26/2020, 9:48 AM
Interesting, thanks! 🙂 I was also just reading the following article, so I'll see. Thanks again. https://proandroiddev.com/stop-using-gradle-buildsrc-use-composite-builds-instead-3c38ac7a2ab3
a

Arun

05/26/2020, 9:51 AM
I recommend that as well, I personally feel
buildSrc
has poor separation of concerns since it is implicit. If you are looking for sample, I use composite builds for gradle plugins in one of my projects here.
d

Dylan

05/26/2020, 9:53 AM
Awesome, thanks!