android - 在根项目的gradle子项目中设置Android版本代码/名称

标签 android gradle

在构建Android应用程序的子项目中,我试图根据根build.gradle中的变量设置版本代码/名称。

子项目build.gradle:

apply plugin: 'com.android.application'

repositories {
    google()
    mavenCentral()
    jcenter()
}

android {
    compileSdkVersion 24

    // current atak build tools version"
    buildToolsVersion "28.0.3"
    defaultConfig {
        multiDexEnabled true
        versionCode project.commit_head_count
        versionName project.full_version
    }

    lintOptions {
        abortOnError false
    }
}

// set the build info prior to building
build.dependsOn rootProject.setBuildInfo

根build.gradle:
// gathers git / build information and dumps it to VERSION files to be read by web app and data analysis program
task setBuildInfo() {
    doLast {
        // read the core version file and store in variable
        def coreVersionFile = new File("$projectDir/VERSION.txt")
        def coreVersion = coreVersionFile.readLines().get(0)

        // get the git hash value (short)
        def getShortGitHashCmd = "git rev-parse --short HEAD"
        def getShortGitHashProcess = getShortGitHashCmd.execute()

        // get the count of commits on this branch at HEAD
        def getCommitCountCmd = "git rev-list HEAD --count"
        def getCommitCountProcess = getCommitCountCmd.execute()

        ext.commit_head_count = getCommitCountProcess.text.trim()
        ext.git_hash = getShortGitHashProcess.text.trim()
        ext.full_version = "$coreVersion.$ext.commit_head_count"
        ext.build_date = new Date().format('yyyy-MM-dd HH:mm:ss')

        // assigns the full_version for global use in other task
        // https://stackoverflow.com/a/29597784/680268
        project.ext.$full_version = ext.full_version
        project.ext.$commit_head_count = ext.commit_head_count


        def fileContent =
                "Short Version:$coreVersion\n" +
                        "Long Version:$ext.full_version\n" +
                        "Git hash:$ext.git_hash\n" +
                        "Commit count:$ext.commit_head_count\n" +
                        "Build date:$ext.build_date\n"
        print fileContent
    }
}

// set the build info prior to building
compileJava.dependsOn setBuildInfo

执行此操作时,Android应用程序子项目表示它不知道commit_head_count变量指的是什么。我觉得如果我真的可以让setBuildInfo首先运行,它将可以工作,但不能正常工作

最佳答案

当执行某些其他任务时,请使用以下代码片段执行您的自定义任务:

tasks.matching { it.name == 'name of dependent task (i.e build)' }.all { Task task ->
    task.dependsOn setBuildInfo
}

这将使所有其他匹配任务强制依赖于您的自定义任务。

或其他解决方案是在评估项目时制作您的构建信息。

将此代码放在您的根级别 build.gradle 中,并检查结果:
project.afterEvaluate {
    print("This line prints every single time")
}

关于android - 在根项目的gradle子项目中设置Android版本代码/名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57907317/

相关文章:

Android GridView - 如何在 dp 中设置 ColumnWidth

android - 将数据从一个 Activity fragment 传递到另一个 Activity fragment

java - 如何使用 Gradle 4.4 创建一个包含所有依赖项的 jar?

gradle - Gradle:删除早于特定时间的文件

android - 从另一个项目编译模块

java - Gradle 构建配置的签名者

Android Volley SQLite 集成/组合

java - 使用 Libgdx 写入和读取 JSON

java - Android——获取RNC( radio 网络 Controller )

android - 有没有办法在 Android 项目中将 Eclipse 与 Gradle 集成?