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"
   }
}
Community
  • 1
  • 1
Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • `versionName` is per flavor. buildTypes has `versionNameSuffix`, that, as you can imagine, adds a suffix to `versionName`. Does it suit your needs? – Blackbelt Feb 16 '16 at 08:43
  • possible duplicate of [enter link description here](http://stackoverflow.com/questions/19726119/android-change-flavor-version-name-based-on-build-type) – Hussnain Azam Feb 16 '16 at 08: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 – Amit Pal Feb 16 '16 at 08:48

4 Answers4

8

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.

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.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • 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. – Amit Pal Feb 16 '16 at 08:47
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.

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
  • I think you misunderstood my question. I want to define `versionnumber` for all `build types` – Amit Pal Feb 16 '16 at 09:37
  • 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. – Michele La Ferla Feb 16 '16 at 09:45
-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"
         }
      }
   }
}
Benjamin Scholtz
  • 823
  • 6
  • 15
  • 1
    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. – Rahul Ahuja Sep 22 '17 at 12:10
  • 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`. – soshial Feb 28 '18 at 09:03