Amit Pal
Amit Pal

Reputation: 11052

How to set different version number in android build types?

I need to set two different android build types i.e. staging and release.

 defaultConfig {
    applicationId "com.app.testing"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "SERVER_URL", '"http://testing.com"'
        }

dev {
        applicationIdSuffix  ".dev"
        buildConfigField "String", "SERVER_URL", '"http://testing.com"'
    }

Now I want to add versionName for each build type. How can I do that?

Edit

  productFlavors{
   release {
        versionname = "1.0"
   }

   dev{
       versionname = "1.0"
   }
}

Upvotes: 12

Views: 6916

Answers (4)

Benjamin Scholtz
Benjamin Scholtz

Reputation: 823

I need to set two different android build types i.e. staging and release.

Defining buildTypes

As in your question above, you have correctly defined your two buildTypes, "release" and "dev".

Now I want to add versionName for each build type. How can I do that?

Iterating through buildTypes

A clean and more programmatic way to define your versionName (or any other defaultConfig parameter for that matter) for different buildTypes, is to iterate through all application variants and perform a specific action for your buildTypes using if statements:

android {
   ...
   defaultConfig {
      ...
      applicationVariants.all { variant ->
         if(variant.buildType.name == "release") {
            //Release
            versionName "1.2.3"
         } else if(variant.buildType.name == "dev") {
            //Dev
            versionName "3.2.1"
         }
      }
   }
}

Upvotes: -1

Oleksiy Yudkin
Oleksiy Yudkin

Reputation: 91

You can also use different version names for each build type without using flavors

In your app-module build.gradle:

  defaultConfig {
      ...
      versionName ""
  }
      ...
  buildTypes {

      debug {
          ...
          versionNameSuffix 'debug-version-1'
          ...
      }

      release {
          ...
          versionNameSuffix 'version 1'
          ...
      }


  }

versionName "" did the trick.

Upvotes: 9

Michele La Ferla
Michele La Ferla

Reputation: 6884

The version code could be in minor versions to such as the following:

 defaultConfig {
    applicationId "com.app.testing"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 3.2.1
    versionName "1.0"
}

In the example above 3 denotes a major release which drastically updates the way an app would look like or its functionality. 2 denotes a bug fix/improvement which updates the app in some way or another, while 1 denotes a minor update or fix.

Hope this helps.

Upvotes: 0

savepopulation
savepopulation

Reputation: 11921

You can use productFlavors like below:

productFlavors
{
   test
   {
     applicationId 'com.example.test'
     versionName '1.0.0.test'
     versionCode 1
   }

   product
   {
     applicationId 'com.example.product'
     versionName '1.0.0.product'
     versionCode 1
   }
}

You can define it under your default config. You can change from build variants. You can combine your build types with flavors.

Good luck.

Upvotes: 5

Related Questions