Search code examples
androidflutterazure-devops

error: invalid source release: 17 on Azure DevOps but not on localhost


I can't build my Flutter app on Azure DevOps, but the build is successful on localhost. The flutter version used is 3.24.1 in both environments:

Here is the error I got in 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

The steps in the YAML file is:

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

I have tried putting the following block in android/build.gradle but it didn't help:

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')
}

Why does my code not build on Azure DevOps with the same version of Flutter?


Solution

  • This error happens because default JDK on Microsoft runners is 11. It's impossible to build for java 17 (your sourceCompatibility and sourceCompatibility) with older version, 17 or newer should be used.

    To solve this, you basically should use JDK 17 for your build. Easiest way, in my opinion, is to use JavaToolInstaller which will choose preinstalled version (runners have jdk 17 and even 21 preinstalled) and modify PATH and JAVA_HOME.

    It will look like this in your pipeline, you should add it as a first step:

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

    Good luck!