Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import BuildConfig in Android Studio using Gradle

I am new to Android Studio and I am trying to get versionCode in build.gradle.

I have read this post and this post, and I tried their solution:

import com.example.BuildConfig;
...
...
...
// Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;

But IDE keeps saying Cannot resolve symbol 'BuildConfig'. Actually I haven't found any string in the whole project folder named "BuildConfig" (searched in Windows Explorer). Is there something wrong with my project configuration/creation?


defaultConfig in build.gradle (module: app) is like

    defaultConfig {
        applicationId "com.foo.bar"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
like image 908
Taihouuuuuuuuuuu Avatar asked Dec 13 '25 06:12

Taihouuuuuuuuuuu


1 Answers

If you are using Jetpack Compose then you should go to your "build.gradle" file (app level), navigate to the "buildFeatures {}" block and add the following:

buildConfig = true

The block should now look like this:

buildFeatures {
    compose = true
    buildConfig = true
}

Hopefully it helps :)

like image 154
Emmanuel Muturia Avatar answered Dec 15 '25 20:12

Emmanuel Muturia