7

Project builds successfully on local PC in Android Studio after upgrade to Gradle 7 but not in Gitlab pipeline.

Gradle 7.0.2 requires Java 11.

The error message in Gitlab pipeline is

A problem occurred evaluating project ':app'. Failed to apply plugin 'com.android.internal.application'. Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8. You can try some of the following options: - changing the IDE settings. - changing the JAVA_HOME environment variable. - changing org.gradle.java.home in gradle.properties.

(check your gradle version by executing the below command on the local machine from the project directory at the command prompt )

 .\gradlew --version

.gitlab-ci.yml: The original complete .gitlab-ci.yml script is described in this post from 2018 which works with Java 1.8 (not with Java 11) AND with gradle 4 (not with gradle 7) on Gitlab: https://about.gitlab.com/blog/2018/10/24/setting-up-gitlab-ci-for-android-projects/

on local pc in Android Studio: Upgrading from Java 1.8 to 11

On the local pc in Android Studio upgrading from Java 1.8 to 11 required 3 steps(and a reboot):

Step 1: adjusting the Gradle setting in Android Studio to Java 11 or higher

(Arctic Fox 2020.3.1 Patch 4: File->Settings->Build,Execution,Deployment->Build Tools->Gradle->Gradle JDK) enter image description here

Step 2: updating the JAVA_HOME variable (Windows "Edit the System Environment Variables"[enter image description here

Set the JAVA_HOME variable to the same as in Android Studio

Step 3: update build.gradle from JavaVersion.VERSION_1_8 to JavaVersion.VERSION_11 enter image description here

enter image description here

Change it in Menu Project Structure also by selecting Open (in Lightbulb or on top right) enter image description here Reboot.

.gitlab-ci.yml : Upgrading from Java 1.8 to 11

The original .gitlab-ci.yml produced the below error with Gradle 7.0.2 when the code built on the new gradle version 7.0.2 was pushed to gitlab:

1st error with original gitlab.ci.yaml

A problem occurred evaluating project ':app'. Failed to apply plugin 'com.android.internal.application'. Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8. You can try some of the following options: - changing the IDE settings. - changing the JAVA_HOME environment variable. - changing org.gradle.java.home in gradle.properties.

Changing the image in .gitlab-ci.yml on line 1 from

image: openjdk:8-jdk

To

image: openjdk:11-jdk

produced another error:

$ echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema at com.android.repository.api.SchemaModule$SchemaModuleVersion.(SchemaModule.java:156) at com.android.repository.api.SchemaModule.(SchemaModule.java:75) at com.android.sdklib.repository.AndroidSdkHandler.(AndroidSdkHandler.java:81) at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73) at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48) Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 5 more Cleaning up project directory and file based variables 00:01 ERROR: Job failed: exit code 1

I have spent a lot of time on this issue.

Any suggestions ?

Nexus7_2012
  • 654
  • 9
  • 13

2 Answers2

1

Here is the part from the original script which needs changing:

From this:

image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS:   "4333796"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

To this:

image: openjdk:11-jdk

variables:
  ANDROID_COMPILE_SDK: "31"
  ANDROID_BUILD_TOOLS: "32.0.0"
  ANDROID_COMMAND_LINE_TOOLS: "7583922"
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"


before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - mkdir -p android-sdk-linux/cmdline-tools
  - export ANDROID_SDK_ROOT=$PWD/android-sdk-linux
  - cd android-sdk-linux/cmdline-tools
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_COMMAND_LINE_TOOLS}_latest.zip
  - unzip android-sdk.zip
  - rm android-sdk.zip
  - mv cmdline-tools version
  - echo y | version/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | version/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | version/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools/
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | version/bin/sdkmanager --licenses
  - set -o pipefail
  - cd ../../
  - chmod +x ./gradlew

**note that the Variables were also upgraded in the .gitlab-ci-yml. They are also located in the local build.gradle here: enter image description here

enter image description here

Nexus7_2012
  • 654
  • 9
  • 13
1

I had the same problem and I solved it by the template .yml file.

You can check the following link:

https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/lib/gitlab/ci/templates/Android.latest.gitlab-ci.yml

Sıla
  • 11
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 22 '22 at 18:42