Hi there, is it possible to create a Kotlin gradle...
# gradle
d
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:
Copy code
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:
Copy code
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
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
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
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
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
do you have
compileOnly(gradleApi())
in your dependencies block?
d
I don't have it, no
Adding it doesn't change anything
message has been deleted
j
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
I did that already, unfortunately
This is how my build.gradle looks like, now:
Copy code
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
what's the build failure you are getting?
d

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

Basically the APIs under org.gradle aren't available anymore for some reasons
g
compileOnly(gradleApi())
is not required if java-gradle-plugin applied
@Dylan what is path to this source file with import problem?
d
buildSrc/src/main/java/...
g
Just a note, you can apply both plugins in
plugins {}
block
d
message has been deleted
I have this issue if I use
plugins {}
g
Remove byildscript block, it's not needed
d
More issues, I think I'm going to keep apply plugin, but thanks
g
But it's completely standard setup, maybe if you have issue its some another problem
Do you have buildSrc/settings.gradle file?
d
No I don't
The other issue I had while using plugins {} was:
Copy code
* 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
Because plugin id is incorrect
d
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:
Copy code
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
Is it IDE only issue or it cannot resolve it during build too?
d
It cannot resolve during build
g
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
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
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
Invalidate cache / Restart. And same bug above
g
Could not find implementation class?
d
This

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

g
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
what version of the IDE are you using?
g
Dylan says this issue reproducible also during build, not only IDE problem
d
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
I tried with a Java file as well
does it work?
d
No
For instance, as mentioned earlier, the issue (which made org.gradle APIs not available) only started to appear after invalidating the cache.
g
What kind cache?
d
Invalidate cache/Restart
g
in IDE? but it’s not related on gradle
d
But that's how the bug appears
g
Maybe you have some wrong gradle wrapper implementation?
d
At least, in the test project I made, that's how it appeared
g
have you tried to build from console
d
Yes
a
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
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
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
Awesome, thanks!