https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hiring-french
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
r

Robert

12/18/2018, 7:30 PM
Anyone has a clue what this even means?
protoc { 
         ^ Type mismatch: inferred type is () -> Unit but Closure<(raw) Any!>! was expected`
Raw? Any? protoc looks like this:
public void protoc(Closure configureClosure) {
o

octylFractal

12/18/2018, 7:32 PM
Closure
is a groovy concept
() -> Unit
is a Kotlin lambda that takes & returns nothing, and it can't be converted to
Closure
implicitly
👍 1
you can use
delegateClosureOf
or
closureOf
to do it explicitly
or the class
KotlinClosure1
which is a little more complicated
r

Robert

12/18/2018, 7:35 PM
It is already complicated enough lol 😛 Thanks, I'll look into it!
b

BorzdeG

12/18/2018, 7:37 PM
I use such config:
import com.google.protobuf.gradle.GenerateProtoTask
import com.google.protobuf.gradle.ProtobufConvention
import com.google.protobuf.gradle.ProtobufPlugin
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.proto
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc

buildscript {
  dependencies {
    classpath("com.google.protobuf:protobuf-gradle-plugin:+")
  }
}

apply {
  plugin<JavaPlugin>()
  plugin<ProtobufPlugin>()
}

dependencies {
  "compile"("com.google.protobuf:protobuf-java")
  "compile"("io.grpc:grpc-stub")
  "compile"("io.grpc:grpc-protobuf")

  if (JavaVersion.current().isJava9Compatible) {
    "implementation"("javax.annotation:javax.annotation-api:+")
  }
}

configure<ProtobufConvention> {
  protobuf {
    protoc {
      artifact = "com.google.protobuf:protoc"
    }
  }
}

configure<JavaPluginConvention> {
  sourceSets {
    this["main"].java.srcDir(the<ProtobufConvention>().protobuf.generatedFilesBaseDir + "/main/java")
  }
}

tasks["build"].dependsOn(tasks.withType(GenerateProtoTask::class))
r

Robert

12/18/2018, 7:48 PM
@BorzdeG that is actually very usefull! Where does this point?
the<ProtobufConvention>().protobuf.generatedFilesBaseDir + "/main/java"
b

BorzdeG

12/18/2018, 7:56 PM
$PROJECT_DIR$/build/generated/source/proto/main/java
r

Robert

12/18/2018, 7:56 PM
I think I may be getting close, now getting this error:
> Task :compileJava FAILED
/home/robert/testproto/build/generated/source/proto/main/java/sample/TestOuterClass.java:9: error: package com.google.protobuf does not exist
      com.google.protobuf.ExtensionRegistryLite registry) {
Do i have to install it on my computer or something?
b

BorzdeG

12/18/2018, 8:00 PM
I'm sorry - I use dependency management:
configurations.all {
    resolutionStrategy {
      failOnVersionConflict()

      eachDependency {
        when (requested.group) {
          "com.google.protobuf"             -> useVersion("3.6.1")
          "io.grpc"                         -> useVersion("1.17.0")}}}}
r

Robert

12/18/2018, 8:07 PM
I never used that, looks nice to centralize versions. But I copied it in, still the same error. Should gradle take care of installing protobuf or should I do it manually and change my classpath?
Maybe because the last few lines use "main"-sourceSet and mine is supposed to be "commonMain" (which does not work when i changed it 😛 )
b

BorzdeG

12/18/2018, 8:37 PM
working example: https://github.com/BorzdeG/examples-proto-kotlin-multiplatform yes, it loads the protoc compiler for the OS you need did not add yet the plugin
kotlin-multiplatform
-example sketched in a quick way
r

Robert

12/18/2018, 8:51 PM
That is so awesome, thank you very very much, i'm checking that out!
👍 1
IT WORKED!
I changed some things around, but some things in your example were crucial I guess! Thank you very much. Been struggling with this for over 2 days! Finally, I got output
🔥 3
@BorzdeG one more question, sorry, I hope you don't mind too much. My output is now Java. It should've been Kotlin because of the
kotlin-gradle-plugin
right? Any clue? Or shouldn't I be looking in
build/generated/source/proto/main/java
?
b

BorzdeG

12/18/2018, 10:33 PM
Yes, Java classes are generated. And then Java classes are used in Kotlin code. Something like this: http://tinyurl.com/ydefd47v
r

Robert

12/18/2018, 10:49 PM
Ah, when i read this i thought it would be Kotlin code: https://grpc.io/blog/kotlin-gradle-projects but I guess they mean what you showed me with "being picked up by Kotlin/grpc" 🙂
g

gildor

12/19/2018, 12:47 AM
There are a couple community projects about generating Kotlin code for grpc, they allow to use more Kotlin features
@BorzdeG You shouldn't use legacy `buildscipt`API for applying plugins, especially with Kotlin DSL If you use
plugins{}
you will get much more help from Gradle, including generated type safe accessors for things like
proc
extension, java convention and even to task.build
@BorzdeG @Robert I cleaned up Victor’s config to be more idiomatic and use all the features Kotlin DSL, as you can see everything is type safe now, do not require knowledge about particular types used by plugin (except GenerateProtoTask) and tricks how to access convention configs: https://gist.github.com/gildor/cf37ebb30cd8cf43b65cc70a055ec017 Just a side note, probably instead add genereteProto tasks as dependencies to build which is meta task, you should do that as dependency to assemble, or to be even more precise, use something like this:
tasks.assemble {
    dependsOn(tasks.named("generateProto"))
}

tasks.test {
    dependsOn(tasks.named("generateTestProto"))
}
👍 1
Actually I just checked, and looks that those tasks already added as dependencies to assemble and test, so no need to configure it explicitly
One more thing, looks that no need to register source set manually, protobuf does this automatically, but to have integration with Idea you should just apply
idea
plugin
I removed grpc dependencies from my example, because it’s not enough to configure grpc, but you can return them in your real project. As you can see final result is much cleaner
b

BorzdeG

12/19/2018, 5:18 AM
@gildor I use buildscript instead of plugins because of several things: 0) it allows you to specify dynamic versions of plugins 1) Plug-ins can be applied "as needed", i.e. depending on conditions At the same time, I have not so much large projects as to have an impact on performance. Including the fact that I try not to use the Gradle daemon for "clean" launches, so as not to catch artifacts from previous builds
But here it is more convenient for someone. PS: I did not find information on the Gradle website that buildscript is outdated. And the collector itself does not inform that this section is outdated.
g

gildor

12/19/2018, 5:21 AM
Plug-ins can be applied “as needed”, i.e. depending on conditions
You also can do this with plugins block
what is advantage of dynamic version of plugins? Why do you need this? This slow down whole build pipeline and make it fragile
buildscript is not yet deprecated, but it’s recommended to use
plugins
instead and I believe eventually buildscript will be used only for non-plugin dependencies
b

BorzdeG

12/19/2018, 5:23 AM
For example, I do not want to follow the updates of a plugin, but I always want to use its latest version. Or the version of the plugin always coincides with the version of dependencies from the same package - I don’t want to write the same version twice
g

gildor

12/19/2018, 5:24 AM
Or the version of the plugin always coincides with the version of dependencies from the same package - I don’t want to write the same version twice
No need to do that, you can extract it to buildSrc or use some other way to share versions
For example, I do not want to follow the updates of a plugin, but I always want to use its latest version.
If plugin works, why would you update it implicitly, it’s against reproducible builds and performance
but I always want to use its latest version
But why? What is you real use case?
b

BorzdeG

12/19/2018, 5:25 AM
Or here is a small project that I don’t know how to implement without buildscript: https://github.com/itbasis/kotlin-assistant-config-project-plugin
g

gildor

12/19/2018, 5:27 AM
What is exactly doesn’t work without buildscript?
b

BorzdeG

12/19/2018, 5:27 AM
But why? What is you real use case?
The version file contains an explicit version of the plugin. But at the same time, there is a separate Job, which tests on the latest versions. Thus, it is operatively visible when the assembly fails with updates. This allows you to “look ahead” to CI and have less headaches when updating
g

gildor

12/19/2018, 5:27 AM
tasks.withType(Wrapper::class.java).configureEach {}
->
tasks.withType<Wrapper> { }
or even
tasks.wrapper {}
(which is not exactly the same, but in your case will be the same) Also you usually don’t need this task, easier just update wrapper version using command line or manually
If you would use plugins dsl for this, everything will be type safe and clean: https://github.com/itbasis/kotlin-assistant-config-project-plugin/blob/master/samples/sample-jvm-junit4/build.gradle.kts
plugins {
    kotlin("platform-jvm")
}

dependencies {
  expectedBy(project(":samples:sample-common"))
}
Also, see Kotlin DSL migration guide, which also strongly recommend to use plugins dsl instead https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/#applying_plugins
b

BorzdeG

12/19/2018, 5:32 AM
kotlin("platform-jvm")
But this is not a static compilation and "magic". When explicitly calling a class, it is always clear where it comes from and where to look at the sources and documentation. This allows you to view the code without IDEA - in some other IDE or in VIM
g

gildor

12/19/2018, 5:33 AM
this is not a magic, it’s application of plugin by plugin id
Also everything on this script depends on it
and will fail in configuration time
Even for such small script you have dynamic things which completely non-necessary
I don’t want to convince you against your will. I just pretty sure this is against best practices and much less safe, you have no IDE help and slower. So I wouldn’t recommend use it, especially when we have now so many nice static accessors, I finally can write configs without magic class names and just have help from IDE So I just want to show that Kotlin DSL is actually great if you use it in a right way
Or here is a small project that I don’t know how to implement without buildscript
I see what you mean, you have custom plugin in buildSrc, and it’s completely fine to use with
plugins
block and have type safe accessors for extensions and tasks. You can check this example https://github.com/gradle/kotlin-dsl/tree/master/samples/buildSrc-plugin Also there are a few tasks that should simplify it even more, you already can use precompiled plugins API for simple cases which is not exactly the same, but allows to create simple plugins without registration, just putting them to plugin-name.gradle.kts files in buildSrc
b

BorzdeG

12/19/2018, 5:48 AM
you have no IDE help
On the contrary - it is much more, since I can "jump" in the IDE via Ctrl + Click into the description of a particular class. While IDEA does not allow me to do this with the "best practices". Although it may have already learned - it is necessary to check.
I really want to show you what it is.
I proceed from the simple - if something cannot be used, then it should be prohibited at the language level. If the use of the buildscript section is prohibited, but you need to leave it only in Groovy DSL and not drag in Kotlin DSL Also, once, it was a problem to use plug-ins used in external * .kts files - the configuration was described not by one file, but was split into several semantic ones - testing.gradle.kts, application.gradle.kts, etc ... I'll see how things are with this now and maybe update the configuration.
g

gildor

12/19/2018, 5:49 AM
e IDEA does not allow me to do this with the “best practices”
It’s completely not true
b

BorzdeG

12/19/2018, 5:50 AM
Although it may have already learned - it is necessary to check.
g

gildor

12/19/2018, 5:51 AM
Also, once, it was a problem to use plug-ins used in external * .kts files - the configuration was described not by one file, but was split into several semantic ones - testing.gradle.kts, application.gradle.kts, etc ...
this is different story not about plugins/builscript, related to script plugins. It’s also not the best way to deal with configuration with Kotlin DSL, because script plugins has only dynamic API for now. If you really want to extract big parts of config, better to use buildSrc And I’m talking about precompiled plugins, not just a separate kts file applied using
apply(from=)
precompiled plugins is relatevely new thing
if you have some real use cases for buildscript and want to use Kotlin DSL, I would report them to Gradle team, maybe they have better solution than use builscript and do not have any help from Kotlin DSL
If the use of the buildscript section is prohibited, but you need to leave it only in Groovy DSL and not drag in Kotlin DSL
It’s of course not prohibited, there are some valid use cases even now, for example to add some dependency to buildscript, but usually better to use plugins and for plugins you don’t need buildscript
Although it may have already learned
it’s never was true. Maybe just some IDE problems with navigation, but all static accessors code is available and you can see what they actually do and see used classes
The rest does not want to collect / search. When problems arise, I report them to YouTrack with enough examples to reproduce. Usually, either the error in IDEA is corrected, or it is written how to do it so that it is correct.
Moreover, an example that in the screenshot, works great in the Groovy DSL. I would like the same from Kotlin DSL
g

gildor

12/19/2018, 6:14 AM
yes, you don’t have access to
compile
because you don’t use plugins block
Moreover, an example that in the screenshot, works great in the Groovy DSL
Yes because in groovy it’s completely dynamic and checked only on runtime
See, the same example with plugins DSL, everything works perfectly, if you really want you can also remove version and use resolutionStrategy instead, how you do in your example
I have autocomplete and navigation in this case, also less typing
b

BorzdeG

12/19/2018, 6:17 AM
The plugin is applied in a clearly legal way for Gradle - at this stage you can already see what it is and use it.
g

gildor

12/19/2018, 6:18 AM
It’s legal, but not recomended for Kotlin DSL
otherwise you don’t have statically typed accessors
b

BorzdeG

12/19/2018, 6:19 AM
"not recommended" does not mean that he should tell me how to use it - just advise. If it is not possible to use it this way, then such an entry should be prohibited. Otherwise, he should understand what he wrote.
g

gildor

12/19/2018, 6:20 AM
Yes, this is just an advice. You show code snippet with problem, I advice you better solution
b

BorzdeG

12/19/2018, 6:20 AM
g

gildor

12/19/2018, 6:20 AM
then such an entry should be prohibited
I would happy, but it’s not so easy, requires much longer deprecation cycle.
But I sure at some point plugins dsl
also it doesn’t solve all the possible cases, but solves most of them
This part about external dependencies! Which is much wider thing, not only plugins can be applied using buildscript
Instead, please read chapter about plugins https://docs.gradle.org/5.0/userguide/plugins.html
Which covers exactly what I said
and also, if you want to know best practices with Kotlin DSL, just check official migration guide that focus on such things
b

BorzdeG

12/19/2018, 6:27 AM
Was reading. I use section https://docs.gradle.org/5.0/userguide/plugins.html#sec:old_plugin_application It is legal. Yes, legacy, but not deprecated
💩 1
g

gildor

12/19/2018, 6:29 AM
This is exactly what I said in my first message
Also it’s not just “legacy because we have new shiny thing”, but because Kotlin DSL just doesn’t work properly with buildscript, you cannot use most of features, so plugins is even more important for Kotlin DSL
b

BorzdeG

12/19/2018, 6:30 AM
But if you look at the link, then there are examples for the Kotlin DSL and it is not written that it is impossible to write like that at all. Therefore - there are no deviations from the documentation.
g

gildor

12/19/2018, 6:31 AM
There is very clear instructions about this in official migration guide on Gradle website. Be free to report documentation issue if you think it should be added to section about plugins
then there are examples for the Kotlin DSL and it is not written that it is impossible to write like that at all
Not sure what you mean. Do you see some invalid snippet in docs?
b

BorzdeG

12/19/2018, 6:33 AM
g

gildor

12/19/2018, 6:34 AM
All those samples are valid
but even on exactly the same page explained, that this is legacy API
and better to use plugins dsl
I really don’t understand what is not clear, this is reference, that shows all available APIs, also has references to other sections If you need best practices read migration guide
b

BorzdeG

12/19/2018, 6:35 AM
e walk in a circle. When this is marked as "deprecated", and not "legacy", then you can say that you can not write like that. In the meantime, it's just "not recommended"
💩 1
g

gildor

12/19/2018, 6:36 AM
I give up %)
Even if something is deprecated, nobody, even I, cannot force you to write in a particular way I just tried to help, sorry for that
👍 2
b

BorzdeG

12/19/2018, 6:39 AM
But I have long agreed with you:
I'll see how things are with this now and maybe update the configuration.
g

gildor

12/19/2018, 6:42 AM
why do you arguing than? I mean yeah, your config is valid, I just suggested how you can improve it.
b

BorzdeG

12/19/2018, 6:45 AM
And I agreed with this 🙂 PS: probably here's an example, why I am writing this way. It will be necessary to try to rewrite it on Kotlin DSL without losing functionality https://github.com/BorzdeG/name.alenkov.habr.gradle-dynamic-dependency
g

gildor

12/19/2018, 7:17 AM
@BorzdeG I see what you trying to do. But I don’t think that you need all this dynamic stuff with dynamically include some directory. Now instead of this you can just use composite builds
1
Also with coposite build you have no problems with versions
it still uses buildscript because you have dynamic version of plugin to apply
b

BorzdeG

12/19/2018, 12:54 PM
accepted pull request. Thank!