Reputation: 4523
I'd like to put some common boilerplate gradle
script code into shared .gradle
file. Then, I can reuse it using apply from:
statement.
The question is whether it's possible to pass parameters to applied script? For example, I'd like to reuse the following boiler plate:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.robolectric'
configurations {
apt
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
The problem is here: apply plugin: 'com.android.application'
. I'd like to reuse this code either for application projects or android library projects. So I need some parameter in order to decide which plugin to apply:
// assuming <isApplicationProject> - is a script parameter
if (isApplicationProject) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
Of course, I can just define some project-level property in this particular case, but I'd like to know whether it's possible to pass parameters upon script invocation
Upvotes: 14
Views: 5813
Reputation: 4612
ext.isUsingApp=true; apply from: 'xxx'
if that's the only difference i would advise you to do something of the following:
myinclude.gradle:
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.robolectric'
configurations {
apt
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
then in your build.gradle app:
apply plugin: 'com.android.application'
apply from: '../includes/myinclude.gradle'
then in your libs build.gradle:
apply plugin: 'com.android.library'
apply from: '../includes/myinclude.gradle'
for a simple Boolean no need to create parameter.
Upvotes: 7