PragProg Customers Kotlin and Android Development: pg. 8 Build Gradle error: Could not get unknown property 'kotlin_version'

I am using Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0.12+0-b1504.28-7817840 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.5.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 16
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: com.kite.intellij (1.9.4), org.intellij.plugins.markdown (212.5457.16), Dart (212.5744), com.thoughtworks.gauge (212.4746.52), org.jetbrains.kotlin (212-1.7.10-release-333-AS5457.46), io.flutter (69.0.2)

Penny Drop App , will not build.

I made the gradle additions to the Module gradle file and it will not build. It does not seem to know about the property ‘kotlin_version’. I saw the previous post about it, but honestly, I don’t see how to fix it there.

My gradle "project file looks like this;

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And my gradle “Module” file looks like this;

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.pennydrop"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}```

Hey Dave!
This was a fun change Google made just after the book was published: the kotlin_version property is no longer automatically included when you create a project.

What used to happen is that you’d get a buildscript { ... } block in your project-level build.gradle file with an ext { ... } inside. This block contained any variables you wanted to use in your Gradle scripts.

You can either create this section in that project-level build.gradle file:

buildscript {
    ext {
        kotlin_version = '1.7.10'
        // Other variables can live here
    }
    // Other blocks can be here
}

Otherwise, you can change the kotlin-stdlib-jdk8 dependency to include the Kotlin version rather than use the variable:
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10"

Also, with newer versions of Kotlin, you should be able to exclude the explicit Kotlin dependency altogether.

Let me know if that works!
Michael

Michael @mfazio23, Thank you for your reply. I managed to get it happy. As of Pg. 19 (top) paragraph of code.
I had to update some things to the new version levels. Here are my gradle files for CHIPMUNK 2021.2.1 Patch 2.

** TOP LEVEL Project file **

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

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

And here is the working version of the Gradle app Module file;

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.pennydrop"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

This builds and runs without any errors on my MAC.
Thanks for checking in and replying.

*Dave