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

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.

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

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
}

Related

External reference/variable in build.gradle file

Is it possible to use some external reference or variable in build.gradle files?
I have several build.gradle files in my app source files, including the one for module app, module base, module player, etc. (it depends on the structure of your code and the names of your packages).
Inside of each of these files is the following or similar structure:
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0.001"
}
Is there any way I can code this the way that I don't have to change these values in every file? Is it possible to use some external reference or variable and that way I can edit my versionCode, versionName, etc. just on one place?
In your Project gradle
ext {
minSdkVersion = 14
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
// App dependencies
supportLibraryVersion = '26.1.0'
mockitoVersion = '1.10.19'
roomVersion = "1.0.0"
}
In your App gradle
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
.
.
}
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
}
Go to File/Project structure/app/flavors then you can get versionCode, versionName, etc then change them what you want and it effects all of your Files.
Check this Image
Yes it is.
In your project-level Gradle config (the one in the root of your project, outside any module folders), you can define variables under the buildscript block:
ext.thisVersionCode = 1
ext.thisVersionName = "1.0.001"
Then you should be able to reference them from your module-level configs:
defaultConfig {
versionCode = rootProject.ext.thisVersionCode
versionName = rootProject.ext.thisVersionName
}
You can define variables in top level build.gradle file and then reference these variables in each module's build.gradle - that way youll be changing them only once in one file.
to give you an example,this is top level file https://github.com/Ejstn/android-starter/blob/master/build.gradle
and this one is module level file: https://github.com/Ejstn/android-starter/blob/master/app/build.gradle
You can also declare whole dependency as variable like in this google's app: https://github.com/google/santa-tracker-android/blob/master/build.gradle

Gradle settings: Invalid variable name. Must start with a letter but was:

Had a issue during settings of my android project:
startup failed:
settings file '/home/user/StudioProjects/project/android/settings.gradle': 1: Invalid variable name. Must start with a letter but was: rootProject
. At [1:1] # line 1, column 1.
rootProject.name = 'project'
^
1 error
Open File
But the thing is I don't have any symbols before 'rootProject' variable name.
Any thoughts about it?
If you're trying to setup some global variables in your root gradle files then reuse them in your modules' build.gradle, how I got it working was:
in ROOT build.gradle :
buildscript {
... blah blah
}
// put HERE all your variables, they will automatically
// be available as rootProject.xxx in the rest of build.gradles
ext {
name = "project"
compileSdkVersion = 27
targetSdkVersion = 27
supportLibraryVersion = "27.0.2"
buildToolsVersion = "26.0.1"
googleServicesVersion = "11.8.0"
}
allprojects {
... blah blah
}
.
.
And then in your APP (or modules) build.gradle, you can use them:
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
applicationId "xxxx"
targetSdkVersion rootProject.targetSdkVersion
.
.
}
I got the same error when i moved my project into a different path.
I tried to do Invalidate Cache/Restart but, it did not work for me.
I finally deleted build.gradle (project level) and opened Android Studio.
It errored out saying 'This is not a gradle based project'.
Then i created the build.gradle, put the contents back and clicked on 'Make Project'. It seemed to work fine then.
Not sure, if this will work for you. But you can give it a shot.

Multiple required build tools for packaging a react-native app to Android [duplicate]

how can I force a library to use sdk build tools 19.1.0 or above without forking/manually editing the build.gradle file for the library?
I keep getting this error when using libraries...
Error:The SDK Build Tools revision (.......) is too low for project ':somelibrary'. Minimum required is 19.1.0
The lack easy way to do it is beyond my understanding. Tons of people use library projects that they don't own, have to build with Jenkins or have other reasons not to touch them and don't want to fork them for personal use.
Anyway, I found a solution here.
Will copy it here just in case:
In you root build.gradle add
ext {
compileSdkVersion = 20
buildToolsVersion = "20.0.0"
}
subprojects { subproject ->
afterEvaluate{
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
This will apply compileSdkVersion and buildToolsVersion to any android modules you have.
And in your main project's build.gradle change dependencies to this:
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
Basically you are defining them once and could use from anywhere.
Cheers.
If you only want to update the compileSdkVersion and buildToolsVersion values only when the buildToolsVersion value is too low, you can first compare the version number of the subproject and only update if needed. This way you make minimal changes to other projects and have less projects to check if things go wrong.
So, let's say that Android Studio is telling you that you need a minimum build tools version of 25.0.0, then in your root build.gradle, here's how you would check each sub-project's buildToolsVersion and only change it if it's less than 25.0.0:
subprojects {
afterEvaluate {project ->
if (project.hasProperty("android") && VersionNumber.parse(project.property("android").buildToolsVersion) < VersionNumber.parse("25.0.0")) {
android {
compileSdkVersion 25
buildToolsVersion '25.0.0'
}
}
}
}

Execution failed for task :appprocessDebugManifest. Manifest merging failed. See console for more info

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.0'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android'
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
}
}
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
}
}
Still no luck after much researching. I change the version number, and the android manifest matches the minSdkVersion and targetSdkVersion as the build.gradle file above. No idea why this doesn't work?!?
In gradle build system minSdkVersion and targetSdkVersion defined in AndroidManifest.xml file will be replaced by what you have mentioned in build.gradle file.
If you are using multiple modules or libraries in your project make sure all build.gradle files as well as AndroidManifest.xml files having same set of configuration for minSdkVersion and targetSdkVersion.
UPDATED
It might possible that any library like google_play_services or other is using different minsdkVersion .
Check your Gradle Console output and make the required changes in your AndroidManifest.xml files as well as in build.gradle files. Also take care that all your build.gradle and AndroidManifest.xml files should have same minsdkVersion. I tried with different values and finally found that most of the libraries uses minSdkVersion 8 .
Also for better practice you can remove min and target sdk from Manifest and have them in build.gradle file only for minimal confusion.

How to force a gradle sub project to use root project's buildToolsVersion/compileSdkVersion?

Looking for some smart way of doing this only once e.g.
allprojects {
android {
buildToolsVersion '18.0.1'
compileSdkVersion 18
defaultConfig {
targetSdkVersion 18
}
}
}
Otherwise I need to up this thing in every subproject (e.g. in referenced library like volley or viewpagerindicator) every time there's a new SDK update available.
Typically the root project wouldn't contain code and hence you would use subprojects. Also you'll have to apply the Android plugin before the android {} block. Other than that, I'd expect this to work.
I answered in another question:
I found a solution here.
Will copy it here just in case:
In you root build.gradle add
ext {
compileSdkVersion = 20
buildToolsVersion = "20.0.0"
}
subprojects { subproject ->
afterEvaluate{
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
This will apply compileSdkVersion and buildToolsVersion to any android modules you have.
And in your main project's build.gradle change dependencies to this:
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
Basically you are defining them once and could use from anywhere.
Cheers.

Categories

Resources