Kotlin and Android Development featuring Jetpack: $kotlin_version ? (page 8)

I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into my first brick wall:

Gradle sync is failing on implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

A problem occurred evaluating project ':app'.
> Could not get unknown property 'kotlin_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Any insight into what the problem might be? Is $kotlin_version supposed to be getting set automatically somewhere?

For reference, here’s the build.gradle that’s failing:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "dev.mfazio.pennydrop"
        minSdk 26
        targetSdk 30
        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.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Hey Jeff!
Totally fair question, the $kotlin_version property lives (or should live, at least) in the top-level build.gradle file:

buildscript {
    ext {
        kotlin_version = '1.4.10'
        gradle_version = '4.1.0'
    }
}

If you ever run into issues, you’re always welcome to post here and you can also grab the source code for every chapter at the book’s page: Kotlin and Android Development featuring Jetpack: Build Better, Safer Android Apps by Michael Fazio

Let me know if that fixes things!

OK, I actually managed to fix it last night after comparing the top build.gradle against the one in the .zip file, but couldn’t edit my post because it hadn’t been approved yet.

I originally tried using the .zip file’s build.gradle verbatim, but had to remove the allprojects{ } section and references to jcenter().

Apparently, Android Studio has a (new?) setting whose default value causes it to halt with an error if there’s an allprojects {} section in build.gradle:

Build was configured to prefer settings repositories over project repositories but repository ‘Google’ was added by build file 'build.gradle’

It also complained about jcenter() being deprecated, but didn’t die over it. I removed the reference to it anyway, and left Google’s default reference to mavencentral() in place.

Here’s my final (working) top-level build.gradle that fixed the problem:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        kotlin_version = '1.4.30'
        gradle_version = '4.1.2'

        app_compat_version = '1.2.0'
        constraint_layout_version = '2.0.4'
        core_version = '1.3.2'
        nav_version = '2.3.3'
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

Anyway, thanks for taking the time to write a badly-needed book!

Hey Jeff, I understand now why you ran into the issue in the first place. When creating a new project with previous versions of Android Studio, the kotlin_version variable was created for you automatically. With Arctic Fox, it skips that and instead hard-codes the version for the dependencies.

Short version: you didn’t do anything wrong, the IDE didn’t give you the value that I expected to be there. I’m thinking I’m going to add some errata for the book to tell people the differences with Arctic Fox.