2

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"
    }
Taihouuuuuuuuuuu
  • 87
  • 2
  • 2
  • 10

4 Answers4

1

Your BuildConfig should always be:

import <applicationId>.BuildConfig (replacing <applicationId> with its value)

If you're still getting the error after trying that, then select invalidate caches / restart from the menu. This happened to me, went googling this error, then had a "duh" moment as I remembered that trick. Invalidating your caches will often fix compilation errors that seem like they shouldn't be happening.

Mick
  • 676
  • 5
  • 12
0

You get to it statically like so....

com.foo.project.BuildConfig.VERSION_CODE; //this value will be an int

In your 'Module: app' Gradle file all of this info can be found in the 'defaultConfig' section.

defaultConfig {
    applicationId 'com.foo.project'
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 18
    versionName '2.6'
    signingConfig signingConfigs.config
}
Waxhaw
  • 599
  • 5
  • 9
  • 2
    I tried `com.foo.bar.BuildConfig.DEBUG;` and `BuildConfig` is still marked red and error message remains the same. (I have posted `defaultConfig` in my question.) – Taihouuuuuuuuuuu Apr 21 '20 at 11:49
  • What is your 'applicationID' value ? That is what those defaultConfig values are tied to. That exact string and you have to have successfully done a Gradle build for that to be generated from the Gradle script. – Waxhaw Apr 21 '20 at 14:12
  • It's not the applicationID that is used !!! It is the package id. The one you set in the manifest file. Otherwise you would not be able to configure multiple product versions as they have different applicationID values, while the packageID describe the source code and there is the same when two variants are build from the same source. – Lothar Jan 12 '22 at 00:34
  • 4
    I don't think this answers the question. The OP is getting error on the import as evidenced by "Cannot resolve symbol 'BuildConfig'". How to fix the import? ... I'm having that issue too :( – steve Apr 14 '23 at 16:54
0

Clearly google does not want to fix this issue as it seems to persist since 2014 over multiple android studio versions.

Even writing your own Field (VER_NAME) like this did NOT help: buildConfigField("String","VER_NAME",""${defaultConfig.versionName}"")

The BuildConfig.java file is present in build/generated/buildConfig/release/my.package.name

And is has added the VER_NAME:

public static final String VERSION_NAME = "2.1.0B28";
// Field from default config.
public static final String VER_NAME = "2.1.0B28";

Its just that google does not want the info to be present in the app.

Or maybe they are just unable to fix the issue given the short amount of time (only 9 years).

FrankKrumnow
  • 501
  • 5
  • 13
-1

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 :)