简体   繁体   中英

Gradle: passing parameters to `apply from: <file>`

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

  1. apply from: is line include<> you cannot pass parameters.
  2. however it does run on the same context and therefor on the same project, so you can do something of: ext.isUsingApp=true; apply from: 'xxx' ext.isUsingApp=true; apply from: 'xxx'
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM