如何从另一个gradle文件访问值?

时间:2015-02-20 12:18:54

标签: gradle android-gradle

好的,这是我当前的build.gradle:

apply plugin: 'com.android.application'
apply from: '../config.gradle'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
       applicationId ""
       minSdkVersion 15
       targetSdkVersion 21
       versionCode 1
       versionName "1.0"
    }
    buildTypes {
        release {
        resValue "int", "amountOfTables", amountOfTables
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    howManyTables.execute()
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.pnikosis:materialish-progress:1.4'
}

那就是config.gradle:

def amountOfTables = 15

task howManyTables << {
    println amountOfTables
}

问题是: 为什么我可以从config.gradle访问howManyTables任务。但无法访问已定义的变量?我想创建具有预定义值的自定义config.gradle。然后在我的Android应用中将它们用作变量。 (比如baseURL,数据类型等......)。而他们,根据这些数据构建我的逻辑。无论如何,我希望问题很清楚;)有什么想法吗?

1 个答案:

答案 0 :(得分:8)

因为您使用def定义了变量 - 所以它在脚本本身中是 local 。尝试:

<强> config.gradle

project.ext.amountOfTables = 15

<强>的build.gradle

apply from: 'config.gradle' //correct path should be here
println project.amountOfTables