Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify versionCode in android/gradle projects project root build.gradle

I recently switched from Eclipse to Android Studio (for test purposes) with an production project and it feel really great. I like the gradle way very much.

In Android Studio the project structure looks (simplified) something like this

+RandomProject
|-+Random
| |- build.gradle (lets call it build2)
| |- [...]
|- build.gradle (lets call it build1)
|- [..]

The build1 file has the following content by default:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

I wonder if its possible/a good practice to specify the versionName and versionCode in that (build1) file, so that it is going to be "inherited" to the build2 file. (and if so, how?)

Thanks for the input.

like image 685
Langusten Gustel Avatar asked Nov 07 '13 23:11

Langusten Gustel


People also ask

Should I set android versionCode to specify the application version?

As you may know, on android you have to define two version fields for an app: the version code (android:versionCode) and the version name (android:versionName). The version code is an incremental integer value that represents the version of the application code.

How would you specify in your build Gradle file that your app requires?

You can do this by adding extra properties to the ext block in the top-level build.gradle file. // of properties you can define. // You can also create properties to specify versions for dependencies.

How do I change the Gradle path in Android Studio?

To change its path go to this path File > Settings... > Build, Execution, Deployment > Gradle In the Global Gradle settings Change Service directory path to what you want. Save this answer.


2 Answers

You can do it with ExtraPropertiesExtension.

RandomProject\build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext.compileSdkVersion=19
ext.buildToolsVersion="19"
ext.versionName="1.0.7"
ext.versionCode=7

RandomProject\Random\build.gradle

android {

    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    defaultConfig {
        versionName rootProject.versionName
        versionCode rootProject.versionCode
    }
}
like image 167
Sergii Pechenizkyi Avatar answered Oct 10 '22 18:10

Sergii Pechenizkyi


The New Build System site now has a tip about this. It's similar to Sergii's answer, but subtly different:

In the root project's build.gradle:

ext {
  compileSdkVersion = 19
  buildToolsVersion = "19.0.1"
}

in all the android modules:

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion
}
like image 36
Pierre-Luc Paour Avatar answered Oct 10 '22 18:10

Pierre-Luc Paour