当前位置: 首页 > 知识库问答 >
问题:

找不到Gradle DSL方法:“Version Code()”

姚树
2023-03-14

在我的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.
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运行良好并显示:

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

..

共有1个答案

公西毅
2023-03-14

使用.ToInteger()对版本代码进行求解。

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

    prod {
        versionCode gitVersionCode.toInteger()
        versionName gitVersionName
        dimension "build"
    }
}
 类似资料:
  • 当尝试在我的魅族MX3上运行Android项目时,我得到了主题中提到的问题。 任何帮助都将得到感激。

  • 问题内容: 我正在重写一个小应用程序,但是当它加载时,它给了我一个运行时异常。例外是 这是RunClient: 这是ClientSettings.java,这是从各种文件中调用的,args始终使用 在RunClient中 问题答案: 的第41行在上寻找默认构造函数,该构造函数不存在。您定义了一个带有的构造函数,因此不会生成任何默认构造函数。您发布的代码似乎不是正在运行的代码,因为它似乎显示了正确的

  • 问题内容: 我在使用简单的hello world程序时遇到了麻烦!我希望有人能对此有所启发。 因此,我收到的错误如下: 因此,通过错误,我可以看到它 显然 缺少main,但是它在那里: 如果有帮助,我就在Mac OS / X上。 问题答案: 问题在于您的方法没有采用String数组作为参数。请使用以下签名: 要么 其他有效选项是: 在Java语言规范中,这被告知如下: 方法main必须声明为pub

  • 问题内容: 我有一个带有本机函数的小型JNI文件,该函数将char数组转换为字节数组(因此我可以将其发送到我的C ++客户端)。 定义如下: 软件包名称(加载库的位置是: 并且该类中的本机deffiniton如下: 但是当调用ConvertString函数时,我得到以下错误: 问题可能是该类(communicationmoduleTCPIP)是可运行的类吗?我不在类中运行,并且具有以下定义(当我注

  • 问题内容: 嗨,我试图建立一个小型的webapp,但是我遇到了错误。下面是我的代码 我正在使用httpclient-4.0-beta2.jar和httpcore-4.0.1.jar。看起来BasicHttpContext与我的应用程序中的其他jar冲突了,但是我无法弄清楚。任何线索将不胜感激。 问题答案: 您似乎有一个jar文件,其中包含的旧/更新版本。如果直接发生冲突,您会收到。通常,Class

  • 我知道这被问了好几次,但看起来我尝试的一切都不奏效。 我也是新手,如果我犯了任何错误,我很抱歉。 我尝试使用Gradle和eclipse构建java jar。 Gradle构建如下所示: 但我总是以: 在DefaultDependencyHandler类型的对象上找不到参数[org.springframework.boot: spring-boot-starter-data-jpa]org.gra