Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 405
Hannan Shaik Avatar asked Feb 14 '23 08:02

Hannan Shaik


1 Answers

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.

like image 141
Hannan Shaik Avatar answered Mar 02 '23 18:03

Hannan Shaik