根据buildType更改android项目中的.properties文件和settings.gradle文件 [英] Change .properties file and settings.gradle file in android project based on buildType

查看:761
本文介绍了根据buildType更改android项目中的.properties文件和settings.gradle文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android gradle项目,我有两个buildTypes:QA和prod。我想根据构建类型更改.properties文件中的属性。我也想根据buildType更改settings.gradle文件中的一个值。我是新来的android和gradle,所以如果这是一个非常基本的问题,请原谅我。



这是我的.properties文件:

  build_number = 3.1-SNAPSHOT 
version_number = 3.1.0
environment_name = prod //需要在qa环境中更改为environment_name = qa

这是我的settings.gradle文件:

  rootProject.name ='tom-android-prod'//这需要在qa环境中更改为rootProject.name ='tom-android-qa'


$ b

这是我的build.gradle文件:

  apply plugin:'com.android.application'

buildscript {
repositories {
jcenter()

}

依赖关系{

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

}
}


android {
compileSdkVersion 21
buildToolsVersion21.1.0

defaultConfig {
multiDexEnabled true

$ b dexOptions {
javaMaxHeapSize4g
}

packagingOptions {
exclude'META-INF /相关的
排除'META-INF / LICENSE'
排除'META-INF / LICENSE.txt'
排除'META-INF / license.txt'
排除'META- INF / NOTICE'
排除'META-INF / NOTICE.txt'
排除'META-INF / notice.txt'
排除'META-INF / ASL2.0'
}
$ b配置{
全部* .exclude组:'com.android.support',模块:'support-v4'
}

}

依赖性{
....
}


解决方案

我并不确定你的目标是以 environment_name 作为未使用的目标,但想象你需要设置一个变量只适用于特定版本,那么你可以这样做: {
environment_name = null
}
android {
//其他东西...
buildTypes {
qa {
project.ext。 environment_name =qa
}
prod {
project.ext.environment_name =prod
}
}
}

//稍后您可以使用像这样的变量
printlnHello variable:$ project.ext.environment_name

使用buildTypes应该不需要修改 rootProject.name ='tom-android'。或者你可以用相同的方式使用 productFlavors 闭包......取决于你想要为每个构建配置明确指定的内容。


I have a android gradle project where i have two buildTypes: QA and prod. I want to change a property in .properties file depending on the build type. I also want to change one of the value in settings.gradle file based on the buildType. I'm new to android and gradle so excuse me if this is a very basic question.

This is my .properties file:

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

This is my settings.gradle file:

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

This is my build.gradle file:

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 {
....
}

解决方案

I'm not real sure what your goal is with the environment_name as its not used, but imagine you need to set a variable only for a specific build then you could do something like this:

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"

Using buildTypes should remove the need to modify the rootProject.name = 'tom-android'. Or you could use the productFlavors closure in the same way... depends on what all you want to specify explicitly for each build configuration.

这篇关于根据buildType更改android项目中的.properties文件和settings.gradle文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆