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
Types of Input Arguments When we want to pass input arguments from the Gradle CLI, we have two choices: setting system properties with the -D flag. setting project properties with the -P flag.
build.gradle.kts. apply(from = "other.gradle.kts") Script plugins are automatically resolved and can be applied from a script on the local filesystem or at a remote location. Filesystem locations are relative to the project directory, while remote script locations are specified with an HTTP URL.
You can run gradle installDist to create an image of the application in build/install/projectName . You can run gradle distZip to create a ZIP containing the distribution, gradle distTar to create an application TAR or gradle assemble to build both.
gradle file is located inside your project folder under app/build. gradle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With