2015-11-22 22 views
1

我有一個Android gradle項目,我有兩個buildTypes:QA和prod。我想根據構建類型更改.properties文件中的屬性。我也想根據buildType更改settings.gradle文件中的一個值。如果這是一個非常基本的問題,我對Android和Gradle很陌生,所以請原諒。更改.properties文件和基於buildType的android項目中的settings.gradle文件

這是我的屬性文件:

build_number=3.1-SNAPSHOT 
version_number=3.1.0 
environment_name=prod //this needs to changed to environment_name=qa in qa environment 

這是我settings.gradle文件:

rootProject.name = 'tom-android-prod' //this needs to changed to rootProject.name = 'tom-android-qa' in qa environment 

這是我的build.gradle文件:

apply plugin: 'com.android.application' 

buildscript { 
    repositories { 
     jcenter() 

    } 

    dependencies { 

    classpath 'com.android.tools.build:gradle:1.3.1' 

    } 
} 


android { 
    compileSdkVersion 21 
    buildToolsVersion "21.1.0" 

    defaultConfig { 
     multiDexEnabled true 
    } 

    dexOptions { 
     javaMaxHeapSize "4g" 
    } 

    packagingOptions { 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/license.txt' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/NOTICE.txt' 
     exclude 'META-INF/notice.txt' 
     exclude 'META-INF/ASL2.0' 
    } 

    configurations { 
     all*.exclude group: 'com.android.support', module: 'support-v4' 
    } 

} 

dependencies { 
.... 
} 
+0

我不認爲你可以做到這一點。你想實現什麼? –

+0

@GabrieleMariotti我想根據buildType加載不同的屬性。例如。如果buildType爲QA,則environment_name = qa,如果buildType爲prod,則在.properties文件中使用environment_name = prod。 – eureka19

+0

在你的buildType中定義一個變量,或者簡單地在你的中使用不同的屬性(不是值)。屬性文件。 –

回答

1

我m不太確定你的目標是什麼,environment_name未被使用,但想象你只需要爲特定的bui設置一個變量LD那麼你可以做這樣的事情:

ext { 
    environment_name = null 
} 
android { 
    // other stuff... 
    buildTypes { 
     qa { 
      project.ext.environment_name = "qa" 
     } 
     prod { 
      project.ext.environment_name = "prod" 
     } 
    } 
} 

// later you can use the variable like this 
println "Hello variable: $project.ext.environment_name" 

使用buildTypes應該刪除需要修改rootProject.name = 'tom-android'。或者你可以用相同的方式使用閉包......取決於你想要爲每個構建配置明確指定的內容。

相關問題