How to used shared variables as depency versions across multiple modules?
Source: wtanaka, StackOverflow
Question: How can we use a shared variables as depency versions across multiple modules?
Answer:
We define those values inside a ext
block in the top level build.gradle
so it can be used by every module.
But those value won't be visible within the buildscript
block in the top level build.gradle
so we need to move it into that.
Top level build.gradle
:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
//moved inside buildscript
ext {
compileSdkVersion = 27
minSdkVersion = 15
targetSdkVersion = 22
kotlin_version = '1.1.51'
support_version = '27.0.0'
anko_version = '0.10.2'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module build.gradle
:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
applicationId "com.vxhviet.kotlinweather"
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$rootProject.kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile "com.android.support:appcompat-v7:$rootProject.support_version"
compile "org.jetbrains.anko:anko-common:$rootProject.anko_version"
}