Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gradle

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

like image 619
Denis Itskovich Avatar asked Jun 05 '15 15:06

Denis Itskovich


People also ask

How do you pass arguments in Gradle?

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.

What is apply from in Gradle?

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.

What does Gradle installDist do?

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.

How do I access the build Gradle file?

gradle file is located inside your project folder under app/build. gradle.


1 Answers

  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'
  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.

like image 131
codeScriber Avatar answered Sep 30 '22 18:09

codeScriber