Hannan Shaik
Hannan Shaik

Reputation: 1306

How to fetch the version number from defaultConfig in gradle?

I am new to Android and recently started working with Android Studio and gradle. I have a query on fetching the version number information from the gradle file.

Below is my gradle file.

defaultConfig {
    versionCode 3
    versionName "2.0.0"
    minSdkVersion 14
    targetSdkVersion 19
}

buildTypes {
    debug {
        buildConfigField "String", "versionNo", "\"2.1\""
    }
}

Currently I am fetching the versionNo from the buildTypes as below.

String versionNo=BuildConfig.versionNo;

But I would like to fetch from the defaultConfig section. Can someone please help me on this?

Upvotes: 4

Views: 5603

Answers (2)

G. Steigert
G. Steigert

Reputation: 177

I could simply use "android.defaultConfig.versionName", even though Android Studio said it couldn't resolve "defaultConfig".

Upvotes: 0

Hannan Shaik
Hannan Shaik

Reputation: 1306

Here is the answer for this question. The gradle file when built creates a BuildConfig.java file. This file contains all the information.

public final class BuildConfig {
   public static final int VERSION_CODE = 3;
   public static final String VERSION_NAME = "2.0.0";
   public static final String versionNo = "2.1";
}

I got the version information with the below statement.

String versionNo=BuildConfig.VERSION_NAME;

Apologies for the inconvenience and Thank you.

Upvotes: 8

Related Questions