I have just created my first Android Studio project that uses Kotlin script. All previous projects have been using Groovy. Most things seem to work, but I have this problem. In my ~/.gradle/gradle.properties I declare:
androidsdk.version.target = 35
And in my build.gradle, under android.defaultConfig I do:
targetSdk findProperty(“androidsdk.version.target”) as Integer
This does not seem to work in Kotlin script. I get: “Unresolved reference: findProperty”. What is the correct syntax here? I prefer doing it this way so that I can update the targetSdk of all projects I am working on in one go.
findProperty works fine, you just should not try to copy over a Groovy snippet to a Kotlin file 1:1 that will quite often not work, because these are different langauges.
If you for example add the missing = after the targetSdk, the “Unresolved reference” is replaced by the next problem.
Thank you. But moving forward - the same syntax doesn’t seem to work in settings.gradle.kts. I need to use gpr.user and gpr.key from my ~/.gradle/gradle.properties to download dependencies from a private Github Package Repository. These are used in a maven.credentials {} block in dependencyResolutionManagement.repositories.
First of all, findProperty doesn’t work here, only getProperty.
Second, this doesn’t set the username and password:
I get “Redundant string template” here. With the String template, I get a 401 code back from Github, signifying authentication is not working. Without the String template, I get “username is null” error.
How about asking your IDE or JavaDoc or even Google instead of asking here?
Not doing so is one of the main charecteristics of a help vampire.
Settings neither have a findProperty nor getProperty method.
I actually wonder that it compiles for you, it does not for me.
If there were no dot in the property name, you could use property delegation (val foo: String by settings or val foo: String? by settings).
You can also use the same technique in build scripts (val foo: String by project or val foo: String? by project).
But with dotted properties it is different.
There you can for example use findProperty or getProperty in build scripts, in settings scripts you need to use the provider-method (which you could also use in the build script, just does not really make sense there), so
But actually, for repository credentials you should neither in Groovy nor Kotlin DSL use such things, but use the built-in credentials support: Supported Protocols