android - 找不到 Gradle DSL 方法 : 'versionCode()'

标签 android git gradle build grgit

构建我的 Android 项目时遇到问题。我使用 Grgit在gradle中填写versionCodeversionName。一切正常,直到我将 Android Studio 和 gradle 工具更新到更新版本(构建 3.4.0 和分发 5.1.1)。

./gradlew build 失败并显示错误:

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find method versionCode() for arguments [1555447320] on ProductFlavor_Decorated{name=dev, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null
, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfi
g=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.ProductFlavor.

提供外部的脚本(project/tools/script-git-version.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //previous: 'org.ajoberstar:grgit:1.5.0'
        classpath 'org.ajoberstar.grgit:grgit-gradle:3.1.1'
    }
}

import org.ajoberstar.grgit.Grgit

/**
 * git.describe()
 *
 * Find the most recent tag that is reachable from HEAD. If the tag points to the commit,
 * then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional
 * commits on top of the tagged object and the abbreviated object name of the most recent commit.
 *
 * More info: https://git-scm.com/docs/git-describe
 */

ext {
    git = Grgit.open(currentDir: projectDir)
    gitVersionName = git.describe(tags: true)
    gitVersionCode = git.tag.list().size()
    gitVersionCodeTime = git.head().time
}

task printVersion() {
    println("Version Name: $gitVersionName")
    println("Version Code: $gitVersionCode")
    println("Version Code Time: $gitVersionCodeTime")
}

任务 ./gradlew printVersion 运行正常并显示:

Version Name: v0.6.2-42-g5e33c1a
Version Code: 5
Version Code Time: 1555447320

模块build.gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply from: "$project.rootDir/tools/script-git-version.gradle"

// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
def keystorePropertiesFile = rootProject.file("$project.rootDir/tools/keystore.properties")

// Initialize a new Properties() object called keystoreProperties.
def keystoreProperties = new Properties()

// Load your keystore.properties file into the keystoreProperties object.
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.itti.sample"
        minSdkVersion 21
        targetSdkVersion 28
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    ..

    flavorDimensions "build"
    productFlavors {
        dev {
            versionCode gitVersionCodeTime
            versionName gitVersionName
            dimension "build"
            applicationIdSuffix ".dev"
        }

        prod {
            versionCode gitVersionCode
            versionName gitVersionName
            dimension "build"
        }
    }
}

..

最佳答案

使用 .toInteger() 版本代码解决。

productFlavors {
    dev {
        versionCode gitVersionCodeTime.toInteger()
        versionName gitVersionName
        dimension "build"
        applicationIdSuffix ".dev"
    }

    prod {
        versionCode gitVersionCode.toInteger()
        versionName gitVersionName
        dimension "build"
    }
}

关于android - 找不到 Gradle DSL 方法 : 'versionCode()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55749856/

相关文章:

android - 如何在 AOSP 类中使用 DEBUG 标志?

android - 设置 imageView 较小与缩放图像本身之间的区别

xcode - 对于 Xcode 项目,是否也可以将 .xcodeproj 文件添加到源代码管理中?

windows - Windows:无法从Groovy脚本执行gradle命令

android - "Contacts"是否可通过模拟器获得?

java - 如何使用Java上传图像Url上传到Fire Storage并同时将其保存在fireStore中

git - 将 git 存储库添加到 docker 镜像(工作流最佳实践)

git - 比较 Git 对当前文件的提交更改

java - Gradle - 更改主要发行版 - 删除 bin 和 lib 文件夹

firebase - Proguard因Firebase消息失败而失败:17.3.4