错误:无效源版本:17 在 Azure DevOps 上,但不在本地主机上

问题描述 投票:0回答:1

我无法在 Azure DevOps 上构建我的 Flutter 应用程序,但在 localhost 上构建成功。两个环境使用的flutter版本都是3.24.1:

这是我在 Azure DevOps 中遇到的错误:

Running Gradle task 'assembleRelease'...                        
Checking the license for package Android SDK Platform-Tools in /usr/local/lib/android/sdk/licenses
License for package Android SDK Platform-Tools accepted.
Preparing "Install Android SDK Platform-Tools (revision: 35.0.2)".
"Install Android SDK Platform-Tools (revision: 35.0.2)" ready.
Installing Android SDK Platform-Tools in /usr/local/lib/android/sdk/platform-tools
"Install Android SDK Platform-Tools (revision: 35.0.2)" complete.
"Install Android SDK Platform-Tools (revision: 35.0.2)" finished.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app_links:compileReleaseJavaWithJavac'.
> error: invalid source release: 17

YAML文件中的步骤是:

    steps:
      - task: FlutterInstall@0
        inputs:
          channel: 'stable'
          version: 'latest'
      - bash: dart run build_runner build
      - task: FlutterBuild@0
        inputs:
          target: apk
          projectDirectory: .

我尝试将以下块放入

android/build.gradle
但没有帮助:

subprojects {
    afterEvaluate { project ->
        if (project.plugins.hasPlugin("com.android.application") ||
                project.plugins.hasPlugin("com.android.library")) {
            project.android {
                compileSdkVersion 34
                buildToolsVersion "34.0.0"
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_17
                    targetCompatibility JavaVersion.VERSION_17
                }
            }
        }
    }
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

为什么我的代码没有使用相同版本的 Flutter 构建在 Azure DevOps 上?

android flutter azure-devops
1个回答
0
投票

发生此错误是因为 Microsoft 运行器上的默认 JDK 是 11。使用旧版本不可能为 java 17(您的

sourceCompatibility
sourceCompatibility
)构建,应使用 17 或更高版本。

要解决这个问题,您基本上应该使用 JDK 17 进行构建。在我看来,最简单的方法是使用 JavaToolInstaller ,它将选择预装版本(运行程序预装了 jdk

17
甚至
21
)并修改
PATH
JAVA_HOME

它在您的管道中看起来像这样,您应该将其添加为第一步:

  # there are more options but only these are required for this task.
  - task: JavaToolInstaller@0
    inputs:
      versionSpec: '17' # this will choose jdk 17
      jdkArchitectureOption: 'x64'
      jdkSourceOption: 'PreInstalled' # this option will not spend time to download sources from somewhere.

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.