Hey im having some trouble configuring the kotlin ...
# gradle
v
Hey im having some trouble configuring the kotlin compile java version using the kotlin dsl for gradle. I cant seem to find
kotlinOptions
. Im using gradle version 6.5
s
Copy code
tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}
v
I tried this but get
unresolved reference KotlinCompile
any idea what would cause that?
I tried
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
but had the same issue
o
do you have the kotlin plugin applied?
☝️ 1
v
Copy code
plugins {
    `kotlin-dsl`
}
do you mean this or do you mean the android studio plugin?
o
I mean like
kotlin("jvm")
you might need to specify a version,
kotlin("jvm") version "1.3.72"
v
oh... did not have that. Didnt realize I needed that haha
been compiling fine without it. My bad!
wait I already have
kotlin("android")
do I need
kotlin("jvm")
with it?
o
I don't think so
v
ok then im not sure what my issue is... 🤔
o
they all use the same underlying JAR, which has mutliple declared plugins in it
s
Kotlin for Android doesn't allow setting the java version afaik. You're stuck with 1.6
That, or it is possible, just through a different method
v
uh im not sure thats true. Android supports java 1.8
I think you are capped at 1.8 afaik
looks like it routes differently
v
hmm what do you mean?
Copy code
kotlinOptions {
    jvmTarget = "1.8"
  }
this is what im trying to accomplish
o
it's nested in the
android
block, not configured via tasks
v
yes I tried in that block also 🤔
s
Give me a minute
👍 1
v
it cant find
kotlinOptions
I see this issue but they said its fixed in gradle 5.6... Im on gradle 6.5 and its not working
but even the workaround is not working
ok so I think I just needed
Copy code
implementation(kotlin("gradle-plugin", version = "1.3.72"))
I still cant use
kotlinOptions
but at least I can configure the
KotlinCompile
task
o
that's pretty weird, considering that the source of
kotlin("android")
is that plugin I think
v
really? wtf...
let me post my exact code
v
Copy code
class AndroidLibraryCommon : Plugin<Project> {

    override fun apply(target: Project) {
        target.plugins.apply("com.android.library")
        target.plugins.apply("kotlin-android")
        target.plugins.apply("kotlin-android-extensions")

        target.extensions.getByType<LibraryExtension>().configure()
    }

    private fun LibraryExtension.configure() {
        compileSdkVersion(...
ok sorry for the weird formatting
v
could it be because its a custom plugin?
o
if it's a custom plugin, you need to add the gradle-plugin to the classpath of the plugin too
v
how do I do that?
o
which means yes,
implementation(kotlin("gradle-plugin", version = "1.3.72"))
but in your
dependencies
block, not nested in
buildscript
v
ok weird. why can I still not reference
kotlinCompile
though?
o
generated accessors don't work in plugins
v
oh wait ya my bad
o
also, if you have
kotlin("android")
in your plugin's buildscript, I don't think that's very useful
considering the plugin itself is not an android project
v
ok second somewhat unrelated question. I have a setup where I have a
Dependencies.kt
file with a
KOTLIN_VERSION
constant. Do you know if theres a way to reference that from within the
buildSrc/build.gradle.kts
? I seem to only be able to reference it from other build files
kotlin("android")
is being applied to the plugins
AndroidLibrary
extension
o
no, I'm pretty sure
buildSrc/build.gradle.kts
(and settings too) cannot access any declared files that need to be compiled, since you need the
build.gradle.kts
to define how to build them
you might be able to get away with applying a common
Dependencies.kts
though? I'm not sure how that works out
v
hmm ok. do you know of a way to share the kotlin version between those gradle files then?
o
well, the
buildSrc
stuff is usually compiled against
embeddedKotlinVersion
(defined by Gradle) since it is used by the Gradle build, not your project
so in practice I don't find that I need to declare the same kotlin version, because that is technically incorrect
v
oh I see
man this gets confusing lol
o
yes, a little bit
v
do I have to specify version for that dependency then?
for the
gradle-plugin
dependency
o
I don't think so
v
ok cool. thanks so much for the help
o
hmm, let me dig up one of my projects to double check
it's a weird balance you need to do 🙂
v
been trying to make a custom plugin so I can share build files between modules
tooons of boiler plate if you have a lot of modules (in android)
o
so my plugin declares
compileOnly(kotlin("gradle-plugin"))
-- that is, it uses the embedded version for compiling against, but does not enforce it on the end build, and does not provide a specific version there
then the actual build is free to use e.g. 1.4-M3 instead
v
Ohhh I see. That's a good idea. I'll change it to that
hey stupid question but if I put this under the
buildscript { dependencies {
block as suggested here: https://kotlinlang.org/docs/reference/using-gradle.html then it doesnt work
I have to put it under the normal dependency block as
implementation
otherwise
KotlinCompile
cant be found. Any ideas as to why the difference from what the gradle docs show?
o
that's because you're making a plugin, not writing it in the
build.gradle.kts
buildscript
is for the `build.gradle.kts`'s classpath
v
oh I see. man this gradle stuff is hard to keep track of lol. thanks for the help
does that get included in the actual apk as as dependency then? or just for the
buildSrc
module?
o
your plugin resides on your project's
build.gradle.kts
classpath, not on the compile classpath
so it won't get included anywhere
v
isnt anything included using
implementation
the compile classpath?
o
it's in the
buildSrc
compile classpath for sure, but not your main project
assuming you're writing it in
buildSrc/build.gradle.kts
v
ya ok that makes sense... I guess im just still slightly confused when to use
implementation
vs
classpath
o
classpath
is always for when you need a class in the
build.gradle.kts
you're currently in, it's only valid inside of
buildscript { dependenices {} }
implementation
is for when you need to compile against a class, regardless of if that is for a plugin or for an application
v
I have
classpath(kotlin("gradle-plugin"))
in my projects
build.gradle.kts
(not buildSrc) If I have it in the buildSrc as well, is it redundant to have it both places?
ok I think that makes sense, im just not totally sure what is shared between BuildSrc and the rest of the modules
o
anything in buildSrc's runtime classpath is added to the classpath of the buildscripts in other modules
I think that declaring
kotlin("gradle-plugin")
in both places as a dependency is redundant
v
ok wow this makes way more sense now. Thanks you so much for the help
o
however I'm not 100% sure if you might also need to declare the one in
buildSrc
as
api
instead, so that it appears on the compile classpath of the main
build.gradle.kts
try it and see 😛
v
ill try it out!