The Variables

In our Kotlin files, we sometimes define constants, such as the ext constants in the top-level build.gradle file:

buildscript {
    ext.nav_version = "2.3.5"

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.2'
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}
(from build.gradle)

You can also attempt to manipulate these from the “Variables” category of the Project Structure dialog:

Project Structure Dialog, Variables Category
Project Structure Dialog, Variables Category

Here, we see the kotlin_version and nav_version constants that we defined in the top-level build.gradle file. The fields to the right of the constant name are editable, so you can use those to change the constants’ values.

In principle, you can use the “+New variable” options to define new variables. In the opinion of this author, this feature does not work very well.

Also, on the toolstrip on the right, there is a “-” toolbar button that is enabled when you have selected a variable. Clicking this button will delete the selected variable, after a confirmation dialog. Just be careful: the IDE does not validate whether you are still using that variable!


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.