欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

android build获取ext,android – 如何在Gradle中获取当前构建类型

发布时间:2025/3/20 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 android build获取ext,android – 如何在Gradle中获取当前构建类型 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

我的问题非常直接且易于理解.

在Gradle中,有什么办法可以在运行时获取当前的构建类型.例如,在运行assembleDebug任务时,build.gradle文件中的任务是否可以根据此任务与调试构建变量相关的事实做出决策?

示例代码

apply plugin: 'com.android.library'

ext.buildInProgress = ""

buildscript {

repositories {

maven {

url = url_here

}

}

dependencies {

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

}

}

configurations {

//get current build in progress here e.g buildInProgress = this.getBuildType()

}

android {

//Android build settings here

}

buildTypes {

release {

//release type details here

}

debug {

//debug type details here

}

anotherBuildType{

//another build type details here

}

}

}

dependencies {

//dependency list here

}

repositories{

maven(url=url2_here)

}

task myTask{

if(buildInProgress=='release'){

//do something this way

}

else if(buildInProgress=='debug'){

//do something this way

}

else if(buildInProgress=='anotherBuildType'){

//do it another way

}

}

综上所述

有没有办法让我在myTask {}中获得正在进行的构建类型?

解决方法:

您可以通过解析applicationVariants来获取确切的构建类型:

applicationVariants.all { variant ->

buildType = variant.buildType.name // sets the current build type

}

实现可能如下所示:

def buildType // Your variable

android {

applicationVariants.all { variant ->

buildType = variant.buildType.name // Sets the current build type

}

}

task myTask{

// Compare buildType here

}

您也可以查看this和this类似的答案.

更新

This回答this问题帮助提问者解决问题.

标签:android,android-gradle,android-studio,build-gradle,gradle

来源: https://codeday.me/bug/20190823/1697494.html

总结

以上是生活随笔为你收集整理的android build获取ext,android – 如何在Gradle中获取当前构建类型的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。