Jenkins job using Gradle can't find executable - android

I am trying to set up a Jenkins job to build the next big Android app! I've got the Android SDK installed and that part seems to be working fine. I've got Gradle Plugin v1.24 installed on Jenkins and now I think I'm trying to figure out how to get Gradle actually working.
First attempt was to have Jenkins download a specific version automatically from gradle.org. I'm trying to pull v2.2.1 because that seems to be the version my Android app builds with locally. When I do that I get this error in my Jenkins job:
[Gradle] - Launching build.
Invalid tool ID 2.2.1
[Gradle] - [ERROR] Can't retrieve the Gradle executable.
Build step 'Invoke Gradle script' marked build as failure
I tried many different versions but could not get that working. So, next was to tell Jenkins where to download Gradle from. I gave it this location:
https://services.gradle.org/distributions/gradle-2.2.1-bin.zip
I get a different error when building but still not too sure what to do.
[Gradle] - Launching build.
[Journeyman-Android] $ gradle clean build
FATAL: command execution failed
java.io.IOException: Cannot run program "gradle" (in directory "/var/lib/jenkins/workspace/Journeyman-Android"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:816)
at hudson.Launcher$ProcStarter.start(Launcher.java:382)
at hudson.Launcher$ProcStarter.join(Launcher.java:389)
at hudson.plugins.gradle.Gradle.performTask(Gradle.java:262)
at hudson.plugins.gradle.Gradle.perform(Gradle.java:116)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1744)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:186)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 16 more
Build step 'Invoke Gradle script' changed build result to FAILURE
I tried to see what I can do with the Jenkins CLI but that fails for me too. We use Jenkins to build/deploy several other projects successfully so this is a working Jenkins installation.
Does anyone have any pointers or suggestions for what I may need to do?
Update (still failing)
I am able to build my app manually but navigating to the root of my project in the jenkins workspace and executing
./gradlew assemble

Finally figured it out. I needed to update my jenkins job to use the Gradle wrapper instead of a specific Gradle version. Once I did that I had a few more errors to sift through but they were all very informative and gave me the information I needed to resolve them.

I downloaded the Gradle.
Extracted to some place.
In Jenins > Manage Jenkins > Gradle
Unchecked the install automatic.
Set the path to gradle home, (Ex: C:\Graddle\gradle-2.1).
Probelm resolved.

For Jenkins running on linux:
download gradle zip file
cd /opt/
wget https://services.gradle.org/distributions/gradle-2.10-all.zip
unzip
unzip gradle-2.10-all.zip
visit jenkins website
Jenkins > Manage Jenkins > Configure System > Gradle > Gradle installations
uncheck Install automatically, set 2.10 to name, set /opt/gradle-2.10 to GRADLE_HOME.
click your job
Configure > Build > Invoke Gradle script
select Invoke Gradle, select 2.10 in Gradle Version dropdown list.

If one previously configured the build with Invoke Gradle script option and selected a specific Gradle Version (say 2.4 for example), he initiated the build but later switched to Use Gradle Wrapper option without resetting the Gradle Version value back to Default. Jenkins would issue the
[Gradle] - [ERROR] Can't retrieve the Gradle executable.
It is a jenkins' bug as documented by this post.
The workaround is to reset Gradle Version to Default before swithcing to Use Gradle Wrapper.

This will help you remove invalid tool issue:
Manage Jenkins -> Manage Plugins -> Advanced: Click "Check Now" button at the bottom/right of the page.

On an old version of Jenkins (v1.609), I had to delete the "Gradle build script" step, and recreate it :-O

Related

How to solve (Could not initialize class org.codehaus.groovy.reflection.ReflectionCache) issue in react native

$ npx react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 864 file(s) to forward-jetify. Using 4 workers...
info Starting JS server...
info Launching emulator...
error Failed to launch the emulator. Reason: Could not start an emulator within 30 seconds.
warn Please launch an emulator manually or connect a device. Otherwise, the app may fail to launch.
info Installing the app...
FAILURE: Build failed with an exception.
What went wrong:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 1m 23s
error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
FAILURE: Build failed with an exception.
What went wrong:
Could not initialize class org.codehaus.groovy.runtime.InvokerHelper
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 1m 23s
at checkExecSyncError (child_process.js:629:11)
at execFileSync (child_process.js:647:13)
at runOnAllDevices (E:\work\react-native\AwesomeProject1\node_modules\#react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:94:39)
I think I had the same issue. After installing RN (May 20th, 2020), I bumped into this error. I found this solution on https://github.com/gradle/gradle/issues/10248.
In short:
Go to /android/gradle/wrapper/gradle-wrapper.properties file in your RN project
Find this line that starts with distributionUrl. Change the gradle version into gradle-6.3-all.zip.
In my project, it was:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
and I changed it into:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
That's it.
I had the same error on Mac OS and React Native v0.62.
Apply these instructions correctly: React native environment setup
Adding this variable solved my problem:
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
In the React Native version 0.62.x even the initialized application is not ready for a development build.
I change the Gradle version to the latest and I could to have a clean build, so change this file:
// this file => /android/gradle/wrapper/gradle-wrapper.properties
from:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
to:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Pay Attention: The latest version of the Gradle for my time is 6.5. when you are reading this answer please check this link to see the latest version number of the Gradle.
Update Jan 24, 2023
7.6
I fixed this by these steps:
install android studio 3.5 (or later)
install java 1.8
if you have java JDE 14.x.x you have to remove its folder from library/java/JavaVirtualMachines/jdk-14.x.x.jdk (on mac os)
install an older version of JDE (I installed 11.0.6)
npm run android!
Just follow these 2 steps only and take your coffee.
Navigate to 'android' > gradle > wrapper > gradle-wrapper.properties
Change 'distributionUrl' from old to >
distributionUrl=https://services.gradle.org/distributions/gradle-6.3-all.zip
This Error occurs when the gradle version you used is not compatible on your environment, so you just need to update your gradle version to latest Gradle Versions, just go to: /android/gradle/wrapper/gradle-wrapper.properties to your project file.
Auguest 2021
I was using JDK 16.0.1 and faced this issue. So, for JDK 16.0.1 you need 7.0 version of gradle.
I fixed it by going to /android/gradle/wrapper/gradle-wrapper.properties and changing
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip
If you are a macOS user then you should install
brew cask install adoptopenjdk/openjdk/adoptopenjdk8
brew cask install android-platform-tools
update your .bash_profile
nano ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
This would resolve your problem. If you are working on a legacy codebase then changing the distribution URL would lead to other problems. It usually happens because of mismatching java versions.
It's probably because you use Gradle as the build system and JDK14
and the Gradle version is old. Reference:
If you use Gradle Wrapper then refer to
$PROJECT_ROOT/gradle/wrapper/gradle-wrapper.properties
Property distributionUrl should be:
distributionUrl=https://services.gradle.org/distributions/gradle-6.3-bin.zip
If it's an older version then change it, run ./gradlew clean build
and try again.
It is because your Gradle version is not supported by the Java JDK. If you use the JDK version 15.0.2 then you need to Gradle version change 6.6.1 or 6.7
To Change the Gradle Version in Android Studio.
android[project_name]/gradle/wrapper/gradle-wrapper.properties
Open this file and set this
distributionUrl=https://services.gradle.org/distributions/gradle-6.6.1-all.zip
then this problem will be solved.
To check the java JDK version.
open cmd.
run echo %JAVA_HOME%
Then you can also search on google to check the supported version of Gradle to the JDK.
updating JAVA_HOME works for me in mac OS
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
This issue is caused by incorrect version of graddle. so to fix this follow these steps
Make sure you have Android studio installed and SDK is setuped from Android studio menu Tool-> SDK Manager you can setup SDK, install latest SDK if you are using latest version of React Native .
From Android Studio menu Tools > AVD Manager Setup one device and download the image. and test it to see that emulator is running OK.
Make Sure JDK is installed for latest react native install latest JDK : https://www.oracle.com/java/technologies/javase-downloads.html
Next from Android Studio menu File > New > new project create one dummy project aka Hello world and build make sure all android studio issues resolved and your Android studio hello world app is running on emulator.
Now time to fix your react native app. Stop your Android studio hello world app and keep the emulator running.
Locate your Android studio hello world app on your system and go to gradle\wrapper folder and copy the gradle-wrapper.properties file
example in windows case my hello world app is located in my documents:
C:\Users\Superman\Documents\android\hello world\gradle\wrapper
Next go to your react native app folder like : myReactApp\android\gradle\wrapper and paste the gradle-wrapper.properties file you copied from your Android studio hello world app.
finally in your terminal execute : npx react-native run-android
I have the same issue with new project and windows. I try all these way but it doesn'. Ridiculously, I move java/jdk folder from C:\Program Files to another dir in D:\ and it works like a charm.
May be it's useful.
December 2021
If you're using the latest version of Java Development Kit, you'll need to change the Gradle version of your project so it can recognize the JDK. You can do that by going to {project root folder}\android\gradle\wrapper\gradle-wrapper.properties and changing the distributionUrl value to upgrade the Gradle version. You can check out here the lastest releases of Gradle.
If you're looking for a solution for a Java/Kotlin Core Android project and not a React Project because i was also facing this problem in my core android project so here you go...
None of the above solutions worked for me. So basically the idea is to Change the version of gradle. How to do that you ask? Follow these steps:
File>Project Structure
Select SDK Location
You will find something written as "JDK was moved to Gradle Settings". Click on Gradle Settings.
Use Gradle Version 11.0
Sync the project again.
And there you go! A successfully built project! :)

What is gradle wrapper and the gradlew.bat file?

Lately I came to know the power of Gradle as a build system and as an Android developer I wanna understand it deeply.
One article said the following:
You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio.
To run a task with the wrapper, use one of the following commands:
On Windows:
gradlew task-name
Now I have some doubts which goes as follow:
What is Gradle Wrapper and gradlew.bat?
If I've got Android studio installed and it is using gradle to build my apps (so gradle is already installed on my system), do I still need to install gradle for build purpose from command line? As when i write any commend like gradle, gradlew on my command line I get error saying gradlew is not recognized as internal or external command (the same error for other commands). I may be using it on wrong path, help me on what path do I need to use Gradle related command?
If I need to download and install it, how and where can I find the file? And the other processes?
I am using a Windows machine for this.
The Gradle Wrapper is an optional part of the Gradle build system. It consists of four files that you check into version control system. The Unix start script <your root project>/gradlew, the <your root project>/gradlew.bat Windows start script, <your root project>/gradle/wrapper/gradle-wrapper.jar which contains the class files for the wrapper and is started by the start scripts and <your root project>/gradle/wrapper/gradle-wrapper.properties which contains some configuration for the wrapper, for example which Gradle version to use to build the project.
In my opinion, each and every Gradle project, even the tiniest, should make use of the Gradle wrapper.
The Gradle wrapper makes sure your build is always run with the same Gradle version, no matter who executes the build and where or that Gradle is installed or not, as long as the one uses the Gradle wrapper to build the project. This means you can design your build for that Gradle version and be sure that the build will not fail, just because someone is using a different version of Gradle and thus is also an important step in build reproducibility.
Also, someone wishing to build your project only needs to have Java installed and that's it. He does not need to have any Gradle version installed. Actually any already installed Gradle version is ignored. The Gradle wrapper checks whether in ~/.gradle/ the version that is necessary for the build is already present, because some Gradle wrapper of any project put it there already. If it is present already, it is used, otherwise it is automatically downloaded.
If you type gradlew on the commandline and the command is not found, that means you didn't put your root projects path to the PATH environment variable (I wouldn't recommend doing that either), nor are you currently in your root project's directory. To run a Gradle build, you have to be anywhere inside your project and call Gradle or the Gradle wrapper. But like with any executable file that is not on the path, you have to provide its path of course. So if you are in your root project directory, you can simply do gradlew. if you are in <root project dir>/foo/bar/, you would need to call ../../gradlew.
The Gradle Wrapper files are generated by the implicitly available Gradle task wrapper and then get checked into the VCS of the project in question. If those four files are not present for a project, it does not use the Gradle wrapper and you should post an improvement request to the project to add it.
If some project does not use the Gradle wrapper, but builds with Gradle, you can either install Gradle and use gradle instead of gradlew, or you can even call the Gradle wrapper of any other project that you have available on disk. The build will then be run with the Gradle version that wrapper or Gradle installation is using and thus might not behave as expected, which is why really each and every project should use the wrapper if it uses Gradle.
Edited after comments
Gradle is a build system.
This gradle-wrapper is kind of the primary interface to to build Android projects. It the part of Gradle-build-system and does some primary check if gradle in installed or not.
gradlew.bat - its a batch file used on Windows. You can even open it with a notepad to view the instructions in it. Batch files are like 'commands' written in a file to be executed. You use it (in case of Windows) to execute build commands. It also checks if gradle is installed or not. And in case it is not, it downloads and installs it.
Example : to build android app on Windows:
Open command prompt
Navigate to your project's root directory
execute gradlew.bat assembleDebug
It starts the wrapper, checks if Gradle is installed there and
executes all the 'gradle specific' commands to build your project.
Do you need to install Gradle ?
Actually, no. Its the job of this gradlew script to check for that. If gradle its not already there, it would automatically download it and use it for all later builds.
gradlew.bat IS the Gradle Wrapper (for Windows in this case). Gradle Wrapper is just a small utility that will ensure that Gradle is installed (or install it if necessary) so you can always build the project. Gradle itself allows you to manage dependencies and build configurations for your project.
If you have installed Android Studio, you have Gradle installed and are good to go. (Technically, each project will have it's own wrapper to handle installing/using Gradle)
As I mentioned above, you are good to go.
In the end Gradle is a command line tool that you use to build your project and you could very well use that directly (though you don't have to) since it is exactly what Android Studio uses to build your project.

In Android Studio, why is the output of Gradle console always empty?

In Android Studio 2.2.3, the output of Gradle console is always empty no matter what task I triggered. Is it deprecated?
Build Your App from the Command Line In this document About build
types Build a debug APK Build a release APK Run your app on the
emulator Run your app on a device You can execute all the build tasks
available to your Android project using the Gradle wrapper command
line tool. It's available as a batch file for Windows (gradlew.bat)
and a shell script for Linux and Mac (gradlew.sh), and it's accessible
from the root of each project you create with Android Studio.
To run a task with the wrapper, use one of the following commands:
On Windows: gradlew task-name
On Mac or Linux: ./gradlew task-name To
see a list of all available build tasks for your project, execute
tasks:
gradlew tasks
Read More at https://developer.android.com/studio/build/building-cmdline.html
And
https://developer.android.com/studio/run/index.html
I found out that Gradle console is used for build action like "Make Project" in menu bar:
If you run the task from Gradle panel, the output will not be shown on Gradle console.

gradle sync failed on intellij idea 13.1.1

I'm creating an Android project with Gradle Android Module, but as soon as I create it, I have an error I don't understand and I have no idea to resolve it. It says
Gradle project sync failed. Basic functionality (e.g editing, debugging) will not work properly" or "Gradle 'myapp' project refresh failed: A fatal exception has occurred. Program will exit. : Gradle settings
If you have an idea of what causes this problem or if you have already encountered it, please tell me what to do.
Came across this issue when I was having similar issue. I solved this problem as follows
Download and extract gradle file to directory of choice
Set the environmental variable for gradle, I think this has no effect in the solution.
Go to intellij and access setting window through File -> Setting
On setting window, go to Build, Execute. Deployment -> Build Tools -> Gradle
Check "Use local gradle distribution"
From Gradle home field access gradle installed path
Please note, I am working with intellij 15.
I have run into the same problem. Part of it may be that you have recently installed the Android SDK and have not accepted the license agreement. Here are the steps you can take. (I was using IntelliJ Ultimate IDEA 14.0.2.)
Stop IntelliJ and restart it with Admin privileges. (You will be expanding zip files in the Program Files (x86) directory, and won't be able to do so unless you are an administrator).
On the "messages Gradle Sync" (bottom window), there is a hyperlink that says Error: "Install Repository and sync project." Click on it.
Accept the License Agreement.
Post the logs from Event Log window. One possibility could be that the gradle setting file is pointing to a non-existing version of gradle. You can find it under gradle -> wrapper -> gradle-wrapper-properties
Problem: If you are getting error as 'gradle sync issue". We have to build very first time from command prompt (not from intellij)
Solution:
Step 1:
Setup user environment variable for jdk 11:
Variable: JAVA_HOME
Value: C:\Program Files\Java\jdk-11.0.14
Step 2:
Go to your project path (File explorer)
Open cmd prompt
type "gradlew clean build"
build should start
no worries, either build success or failed
Step 3:
Open intellij project and open intellij terminal and again run the same command
"gradlew clean build"
Step 4:
Go to build tab (bottom of the intellij window)
Go to sync tab -> Make sure it should show as "BUILD SUCCESSFUL"
Step 5:
now again go to intellij terminal and again run the same command
"gradlew clean build"
Now you will be able to see your test execution results. Please verify report
Please let me know if there is any question.

Unable to build from Android Studio

I get this error message when trying to build my project from Android Studio
org.gradle.tooling.UnsupportedVersionException: The specified Gradle installation 'C:\Program Files\Android\android-studio\plugins\gradle' is not supported by this tooling API version (1.7, protocol version 4): The specified Gradle installation 'C:\Program Files\Android\android-studio\plugins\gradle' is not supported by this tooling API version (1.7, protocol version 4)
I can build my project from executing
gradlew assemble
I found out if I import module by "Create module from existing sources", this problem went away.
I had selected "Import module from external model" with Gradle. This option generated the above message.
I believe this is a temporary issue with the current build of AndroidStudio. I downloaded and installed gradle locally and referenced that installation for my project via File -> Settings >> Gradle >> Use Local Gradle Distribution and was able to build and install.
Alternatively, using gradlew from the Terminal window worked as well. ./gradlew tasks show you available taslks including "installDebug".

Categories

Resources