12

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"
   }
}
3
  • versionName is per flavor. buildTypes has versionNameSuffix, that, as you can imagine, adds a suffix to versionName. Does it suit your needs? Commented Feb 16, 2016 at 8:43
  • possible duplicate of enter link description here Commented Feb 16, 2016 at 8:44
  • @Blackbelt It is throwing me an error: Error:(39, 0) ProductFlavor names cannot collide with BuildType names . I defined product flavour in edit section Commented Feb 16, 2016 at 8:48

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

Actually we use product flavour for different things like "paid version of the product" or "free version of product". Also if the package is different as per product flavour.
0

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.

2 Comments

I think you misunderstood my question. I want to define versionnumber for all build types
This is defined when uploading the app to google play. When you upload a new version of the, the developer console on google play will take the version number of the app from the version code in the gradle file. It won't let you upload an app with the same version code. The version code must be incremental.
-1

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"
         }
      }
   }
}

2 Comments

This does not overwrite value of VERSION_NAME in BuildConfig.java. Instead is=t generates two variable of name VERSION_NAME. VERSION_NAME = -1 (taken as default even if we do not specify) VERSION_NAME = "3.2.1" (sample value) issue comes coz there are two variables of same name in same class.
I wish this were a working solution, but it gave me Error: Execution failed for task ':app:processDebugManifest'. It might should have been debug instead of dev.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.