Hey how to convert groovy code in kotlin gradle. I...
# gradle
v
Hey how to convert groovy code in kotlin gradle. I added my kotlin gradle in thread. I want to read github.properties file in my root project
Copy code
def githubPropertiesFile = rootProject.file("github.properties"); 
 def githubProperties = new Properties() 
 githubProperties.load(new FileInputStream(githubPropertiesFile))
build.gradle.kts
Copy code
plugins {
      kotlin("multiplatform") version "1.6.21"
      id("com.android.application")
  }
  
  group = "com.abc"
  version = "0.0.1"
  
  repositories {
      google()
      mavenCentral()
  }
  
  kotlin {
      android()
      iosX64()
      iosArm64()
      iosSimulatorArm64()
  
      sourceSets {
          val ktorVersion = "2.0.0"
          val commonMain by getting {
              dependencies {
                  implementation("io.ktor:ktor-client-core:$ktorVersion")
                  implementation("io.ktor:ktor-client-logging:$ktorVersion")
                  implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                  implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
                  implementation("io.ktor:ktor-client-auth:$ktorVersion")
                  implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
                  implementation("io.insert-koin:koin-core:3.2.0-beta-1")
              }
          }
          val androidMain by getting {
              dependencies {
                  implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                  implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
              }
              val iosX64Main by getting
              val iosArm64Main by getting
              val iosSimulatorArm64Main by getting
              val iosMain by creating {
                  dependsOn(commonMain)
                  iosX64Main.dependsOn(this)
                  iosArm64Main.dependsOn(this)
                  iosSimulatorArm64Main.dependsOn(this)
                  dependencies {
                      implementation("io.ktor:ktor-client-darwin:$ktorVersion")
                  }
              }
          }
      }
  }
  
  android {
      compileSdk = 21
      sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
      defaultConfig {
          applicationId = "com.abc.kotlinmultiplatform"
          minSdk = 21
          targetSdk = 31
      }
      @Suppress("UnstableApiUsage")
      compileOptions {
          sourceCompatibility = JavaVersion.VERSION_1_8
          targetCompatibility = JavaVersion.VERSION_1_8
      }
  }
g
Do you have any particular problem? Looks that conversion is very striagiht forward, just replace def with val and remove “new” from constructors
v
Besides that even the Groovy version has a file handle leak as you never close the stream
Should in Kotlin more be something like
Copy code
githubPropertiesFile.inputStream.use(githubProperties::load)
v
@Vampire I tried you suggestion and giving me error
message has been deleted
what is
load
?
g
load is mehtod of Properties
v
ok and do you know the above error?
g
Are you sure that you imported correct Properties class?
You need java.util.Properties
v
As well as the normal Kotlin
use
, not the strange one you imported
And if you care about configuration cache, you also need to change the file reading
Copy code
val githubPropertiesFile = providers.fileContents(rootProject.layout.projectDirectory.file("github.properties"))
val githubProperties = Properties()
githubPropertiesFile.asBytes.get().inputStream().use(githubProperties::load)
v
What should I do ?
v
Import the right class as gildor said?
v
the same thing as your code
Sorry what gildor?
v
Why don't you just do as you are told?
Import the correct class
Which gildor already told you
The highlighted one is not the correct class, as little as the
use
from that library
v
okk sure thanks
g
v
ohh sorry it's your name
v
message has been deleted
v
if I import java.util.properties
it giving me this error
v
That is NOT an import
Now you try to access the property
util
of the
java
extension
Which is an unlucky fact in Gradle build scripts that use the Java plugin or any derivative
You cannot use a fully-qualified name, besides that it looks ugly anyway
Use an import as said multiple times
v
but it giving me same error
g
because as Björn said, it’s because your githubProperties is incorrect, you should use import, now it resolves to property
v
g
You already imported java.util.Properties, now just change this line to:
Copy code
val githubProperties = Properties()
1
v
Sorry, 🥄 s are out, seems gildor has some left 🙂
v
Okk now I got it, thanks a million for you guys
👌 1
g
It’s standard Kotlin code, nothing magical here in terms of code, all this snippet can be written in any Kotlin .kt file and it will work the same way, nothing Gradle specific here (except this gotcha with
java
is Gradle accessor, so you cannot use fully qualified class name
v
fully qalified class name, thats not an import, don't confuse him even more 😛
😬 1
1
v
Sorry for inconvenience
👌 1
c
@Vivek Modi following your questions in the last month in several channels over here, I think it would be good for you to learn the basics of software development, the Java programming language and then Kotlin before you start writing libraries and publish them.
v
I am learning that's why I asked in here, If I don't understand then I asked in the channel.
c
I see, sure, but this workspace is about Kotlin and not about “how I do this” or “how I do that”. There a “forums”, “classes” and tutorials out there that you should work through first before digging into the stuff you are trying to do.
👍 1
@Vampire confusion with the name might come from that not everybody uses the same config on how to display a user.
v
Oh, ok. Wasn't aware of that or forgot about it. I'm pretty sure I use the default setting, but I might be wrong. I'll mention next time, hoping it displays according to the settings next time.
🙂 1