Gradle DSL method not found: 'versionCode()'

北城以北 提交于 2019-12-11 07:58:43

问题


Have a problem build my Android project. I using Grgit to fill versionCode and versionName in gradle. Everything worked fine, until I updated Android Studio and gradle tools to newer version (build 3.4.0 and distribution 5.1.1).

./gradlew build fail and show error:

* 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.

Script which provide externals (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")
}

Task ./gradlew printVersion run fineand show:

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

Module build.gradle file:

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"
        }
    }
}

..

回答1:


Solved using .toInteger() for version code.

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

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


来源:https://stackoverflow.com/questions/55749856/gradle-dsl-method-not-found-versioncode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!