Kotlin and Android Development: Could not get unknown property ‘kotlin_version’…

Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in python, js, and - in a basic, command line form - in kotlin and want to port it to Android (web version at https://basilisk.net). Anyway, got to page 8 and was editting the build.graddle file and found that the line

implementation “org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version”

threw up an error “Could not get unknown property ‘kotlin_version’…”

the line jvmTarget = ‘1.8’ was already in the build.graddle file but the dependencies identified were not. Editting out the problematic kotlin_version line and the program runs fine, but I don’t want to waste my time if this is something that causes more problems downstream.

I am on Arctic Fox 2020.3.1 Patch 1.

Hey Nic! This is a known issue when using newer versions of Android Studio. They used to add the $kotlin_version variable for you when creating a new project, but no longer.

If you open the project’s build.gradle file (not the app’s file of the same name), you can add an ext block to the buildscript block to include that variable:

buildscript {
    ext {
        kotlin_version = '1.4.10'
        gradle_version = '4.1.0'
    }
   // The rest of the buildscript lives down here.
}

Let me know if this helps!

Hey, thanks for getting back so promptly. It is frustrating how Android Studio varies so much between releases, but your advice nailed the problem. I’m looking forward to working through the projects!

You’re welcome! Please stop by here if you run into anything else, we’ve already found a few issues with new Kotlin/Android Studio/Gradle versions.

@mfazio23

I had the same issue. Two things I think need mentioning;

  1. The buildscript has to be the First block of code in the gradle file. My file did NOT have that block at all, So I added what is shown in the thread.

  2. Question…the example shows kotlin_version = 1.4.10’

If am am on version 1.7.0 should I change it or leave it as shown?

Thanks,

*Dave

Dave,
I replied as well in the thread you created, but to answer your two questions here:

  1. There are new approaches to adding variables, so they don’t include the buildscript { ... } block by default anymore, but it should work fine.
  2. You probably want to go with a newer version, just be warned that everything in the book was tested with the versions listed in the appendix, so no guarantee on things working with the new stuff!

Hopefully this helps you get rolling!
Michael

Michael,
I hope that by using the current newer versions of the files it does NOT break anything. I’ll be sure to let you know though. If I run into a deal breaker I hope we can work through it. I believe that will benefit everyone who is or might be working through this book. So far the newer versions haven’t
broken anything. As of Aug 27, 2022.

*Dave