apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.59k stars 1.52k forks source link

Can't find Gradle (Android Studio) #845

Closed domske closed 2 years ago

domske commented 4 years ago

Bug Report

Problem

Could not find an installed version of Gradle either in Android Studio, or on your system to install the gradle wrapper. Please include gradle in your path, or install Android Studio.

What is expected to happen?

Just work. The message is wrong. Android Studio is already installed. Leave a link to the docs for this problem. Because everything is correctly setup (by the message). Also the enviroment path variables are set.

What does actually happen?

The Android Studio is installed and I can use it. I already installed a apk by the Studio and Flutter. But Cordova does not work.

Information

The whole Android enviroment and JDK 1.8 (8), Ionic, Cordova etc. are installed. I can run Android apps on my physical phone. e.g. using Flutter and the Android Studio. It's always tortuous to bring Cordova Android to get running.

Command or Code

ionic cordova run android

Environment, Platform, Device

Running Cordova 9 with native-run of Ionic 4 on Windows 10 64bit.

Version information

Click to expand Ionic: Ionic CLI : 5.2.5 Ionic Framework : @ionic/angular 4.9.0 @angular-devkit/build-angular : 0.801.3 @angular-devkit/schematics : 8.1.3 @angular/cli : 8.1.3 @ionic/angular-toolkit : 2.0.0 Cordova: Cordova CLI : 9.0.0 (cordova-lib@9.0.1) Cordova Platforms : android 8.0.0 Utility: cordova-res : not installed native-run : 0.2.8 System: NodeJS : v10.16.3 npm : 6.9.0 OS : Windows 10

Checklist

Workaround

Install Gradle manually. Maybe the latest Android Studio does not include it for external usage. Whatever... Cordova should remove it from the error text. Because it's not solved by installing the Android Studio. In my case I already had and set up the environment variables. But still not worked.

Mapiac commented 4 years ago

Torturous indeed

breautek commented 4 years ago

does the gradle command exists on the command line? You may need to modify your system variable PATH to include the path of the android studio gradle install.

domske commented 4 years ago

@breautek I can't remember exactly. It always worked before last Android Studio update or new setup. But know I see in the Android Studio path the Gradle folder. I could set the enviroment variable to this path instead of downloading gradle separatly. But currently it works with my workaround from my prev. post. But the path to C:\Program Files\Android\Android Studio\gradle\gradle-5.1.1\bin should also work. It's weird, I never had to set the path before. Something has changed. How detect Cordova the Gradle? Is the Gradle cli required or does it look up the Android Studio Path? Installing the Android Studio without any hacks will not work anymore. The error message of Cordova should be changed. It's very trivial if you know what's the problem is and how to fix it. I was just annoyed to get one error after another. - Be free to close it.

breautek commented 4 years ago

For me I have export PATH=$PATH:/home/norman/android-studio/gradle/gradle-4.4/bin

(I'm running linux)

I don't remember if I have this set because of cordova or for another reason. But cordova does search for paths for other requirements like java for example, so I can't see why it shouldn't search for plausible locations for gradle either.

breautek commented 4 years ago

gradle lookup for windows happens here...

https://github.com/apache/cordova-android/blob/c35a990c09f1ab278873754eb7f81e52250893a3/bin/templates/cordova/lib/check_reqs.js#L90-L113

It has comments about a hackish solution, so perhaps there is room for improvements here.

domske commented 4 years ago

On another PC (with a newer version of Android Studio) there is no gradle folder in C:\Program Files\Android\Android Studio. It looks like that the setup has been changed. In the Android Studio settings Build, Execution, Deplayment > Gradle you can see the "service directoy". In my case it's %USERNAME%\.gradle.

In the .gradle folder there is a wrapper\dist folder. There may be several versions. In my case gradle-4.10.3-all and gradle-5.4.1-all. Each folder has a random id like 81msde2dx9p4vji0mjgtvxkcb. (random or id / hash?). Inside and a few folder deeper you can find the bin folder and the bat file etc.

Summary it's a bit more difficult to find the Gradle stuff for Cordova. :/

mbayadinov commented 4 years ago

check_reqs.js for Windows should check if androidStudioPath === null in line 113 and if so check if dir %USERPROFILE%\.gradle exists as it gets created by Android Studio when it opens a project and downloads gradle (version at the time of the comment is 5.4.1). Then check_reqs.js for Windows should search %USERPROFILE%\.gradle\wrapper\dists for first sub-dir starting with gradle- then pick it's first sub-folder (hash) then inside this hash sub-folder pick first sub-dir starting with gradle-

Cordova won't like gradle 5 during build process so it will download version 4.10.3 to %USERPROFILE%\.gradle\wrapper\dists and then start it's daemon.

Installing gradle to C:\Program Files\Android\Android Studio\gradle\gradle-4.10.3 does the trick, but it wastes 210MB

ghost commented 4 years ago

I think there simply is no more gradle bin anymore, its a .jar in android-studio/plugins/gradle/lib and cordova is not uptodate with that change. When i add the android platform, open android studio and import exisitng gradle project i can build and run my cordova app for one time. Then when I need to do a cordova build because of the changes its complaining about the gradle bin. So there does not sem to be another way than install gradle standalone and work with its bin.

chuchuva commented 4 years ago

I can confirm, cordova build android command started failing with "CordovaError: Could not find an installed version of Gradle..." after I upgraded Android Studio from 3.4 to 3.5. Full error message:

CordovaError: Could not find an installed version of Gradle either in Android Studio, or on your system to install the gradle wrapper. Please include gradle in your path, or install Android Studio

Workaround: I installed gradle 6.1 to C:\Gradle and added C:\Gradle\bin to my PATH environment variable.

breautek commented 4 years ago

Workaround: I installed gradle 6.1 to C:\Gradle and added C:\Gradle\bin to my PATH environment variable.

Do note that android studio doesn't officially support gradle 6 (although still may work just fine)

See https://developer.android.com/studio/releases/gradle-plugin#updating-gradle

ghost commented 4 years ago

I wish this worked on my linux install. Unbuntu 18.04

The strange thing is from the command line I can issue gradle -v and it can find it. I do not understand why cordova build android fails saying that it cannot

DustinTheDev commented 4 years ago

Workaround: Create a symlink to the new directory using an Administrator command prompt.

mklink /J "C:\Program Files\Android\Android Studio\gradle" "%USERPROFILE%\.gradle\wrapper\dists\gradle-5.6.4-all\ankdp27end7byghfw1q2sw75f\"

rolinger commented 4 years ago

On windows installs, as both @domske @mbayadinov stated, gradle now exists in %USERDIR%/.gradle - all versions of gradle will be there.

I had to add a new path for %userdir%/.gradle/wrapper/dists/gradle-4.10.3-all/71msde2dx9p4jvasdflakdfa/gradle-4.10.3/bin - thats a helluva path though.

pcwSlide commented 4 years ago

Why has this not been fixed in Cordova?? Every 6 months I come back to use Cordava and it's broken.. Who is maintaining it? 2-8-2020 - In hind site I understand that a lot of work is voluntary so - bit harsh..

ahmedaniss25 commented 3 years ago

i fix this issue here https://youtu.be/54mtkVh7yXs

brodybits commented 3 years ago

I have pinned this issue, which has the help wanted label. Most of the maintainers are volunteers, unfortunately pretty overloaded, and dealing with some other requirements in the major Cordova release. This thread on the mailing list ([1]) about modernizing the cordova-android build may be of interest.

[1] https://lists.apache.org/thread.html/ra54447f23a6df77128dbec69ad3130281221e8a1b724c5ceb02052db%40%3Cdev.cordova.apache.org%3E

anibalsanchez commented 3 years ago

I had to rebuild my development workstation and I revisited the topic on Linux.

I'm finding that android-studio-ide-183.5692245-linux.tar.gz Android Studio v3.4.2 works fine with Cordova CLI 10.0.0. Android Studio v3.4.2 builds the App for API 29/30 and the artifacts are ok for the upcoming Google Play November deprecation (when Apps built with previous API versions are not going to be accepted anymore).

The following Android Studio versions (v3.5.3, v3.6.3 and v4.0.2) don't have gradle as a standalone executable. However, the latest Android Studio v4.0.2 can be downloaded and installed to configure the general environment; and then, install gradle-6.6.1-bin.zip in an external folder and add it to the path. cordova detects that the gradle executable is in the path and builds the app.

$ cordova build
....
Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=..../android-sdk (recommended setting)
ANDROID_HOME=..../android-sdk (DEPRECATED)
Using Android SDK: ..../android-sdk

Welcome to Gradle 6.6.1!

Here are the highlights of this release:
 - Experimental build configuration caching
 - Built-in conventions for handling credentials
 - Java compilation supports --release flag

For more details see https://docs.gradle.org/6.6.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 3s
1 actionable task: 1 executed
Subproject Path: CordovaLib
Subproject Path: app
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :app:compileDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 14s
40 actionable tasks: 40 executed
Built the following apk(s): 
        ..../platforms/android/app/build/outputs/apk/debug/app-debug.apk
breautek commented 3 years ago

Thank you for your analysis @anibalsanchez

So I think the best course of action is to improve the error message as simply installing android studio is no longer applicable, as the cordova error states, and ensure our documentation is updated for configuring Gradle.

pcwSlide commented 3 years ago

Hi Guys, I have it working using JDK8, AND4 with a separate Gradle 6 and modifications to the Memory allocation. My questions are:

  1. Can we set the jdk in a config file or in the CLI to tell Cordova where the JDK8 is? It is not a good fix to say we have to install only JDK8.
  2. It would be best to use the same gradle on the Android install - this worked months ago - Has anyone looked at this?

Thought: If Google are going to continually change perhaps we set our variables independently of them - say CORDOVA_JDK and CORDOVA _GRADLE and just set them once on our machine..

Many thanks for your hard work.. Phil

breautek commented 3 years ago

Can we set the jdk in a config file or in the CLI to tell Cordova where the JDK8 is? It is not a good fix to say we have to install only JDK8.

I think this is a good idea.

Thought: If Google are going to continually change perhaps we set our variables independently of them - say CORDOVA_JDK and CORDOVA _GRADLE and just set them once on our machine..

Also :+1:

It would be best to use the same gradle on the Android install - this worked months ago - Has anyone looked at this?

Android Studio doesn't appear to include a gradle install anymore. Even the path located in my previous comment doesn't exist anymore. I'm not sure about other OS' but on linux I had to install gradle myself.

pcwSlide commented 3 years ago

Not sure how I reply to my previous post? To be clear "Can we set the jdk in a config file or in the CLI to tell Cordova where the JDK8 is? - this is not possible currently?

breautek commented 3 years ago

Not sure how I reply to my previous post? To be clear "Can we set the jdk in a config file or in the CLI to tell Cordova where the JDK8 is? - this is not possible currently?

Short answer: There is no configuration variable that you can set to tell just cordova to use a specific JDK installation.

Long answer: But Cordova will read the JAVA_HOME variable, but obviously many other systems installed on your machine will also use this variable.

There are potentially some workarounds to make only Cordova use a specific JDK installation... such as providing the system variable inline with the cordova command:

JAVA_HOME=/path/to/jdk cordova build android

I haven't tested this, but you may need to override the PATH variable as well... e.g:

JAVA_HOME="/path/to/jdk" PATH="/path/to/jdk/bin/:$PATH" cordova build android

Obviously this isn't ideal to type out every single time, but it could be incorporated into a build script.

I personally would support a CORDOVA_JDK variable that one can set and then not have to worry about, but I don't know if other PMC members will agree to this.

pcwSlide commented 3 years ago

It turns out that most of my students already have JDK8 because of some other software they had to use..The problem is the class machines - I will try and work it through with the techs.. JDK8 appears ok for Android Studio.. Whilst not ideal it is simple just to install Gradle external to AS.

So is this how a proposal is put for a change like " CORDOVA_JDK and CORDOVA _GRADLE"? It would give us some independence.. Thanks.. P

CodeStudent09 commented 3 years ago

I encountered this problem on 11/15/20. Using Windows 10, Cordova 10.0.0, Android Studio 4.1, API 29

I tried adding 'GRADLE_HOME' variable to both user and system environment settings with the path to gradle (default location inside /USERS/{USER}/.gradle ) to no avail.

I ended up having to install Gradle from the website (binary version) and adding that to the end of the System 'Path' variable. It worked then.

pcwSlide commented 3 years ago

Hi, a new year - does Cordova support a newer version of Java than 8?

breautek commented 3 years ago

Hi, a new year - does Cordova support a newer version of Java than 8?

No. The blocking element here to support newer versions is the android SDK, which contains programs like the avdmanager that doesn't place nicely with anything > Java 8.

pcwSlide commented 3 years ago

Thanks - does this mean that all frameworks trying to output a pkg file have to have Java 8 - React - Natice OR Angular - Capacitor - View etc - this seems very strange..

breautek commented 3 years ago

Afaik, building android apps can be done with modern Java versions, but the android sdk tools to manage the android emulators or the android SDKs can only be used with Java 8.

Because of the latter, Cordova requires Java 8.

pcwSlide commented 3 years ago

Actually I was thinking - can we use the Android Java Version - Open Source as I understand it.. It would be good to tell Cordova to use that and allow the system to install a modern version for other apps - as inevitably people will install Android Studio - for the emulator and convenience- I know there are emulator alternatives - just a thought..

breautek commented 3 years ago

can we use the Android Java Version

We could probably use the java install included in Android studio, but we cannot rely on only this option. We still need to support environments without an AS install, generally for CI environments, for example.

This is kinda off topic though and not related to gradle. AS has gradle lib files, but I don't think they have an actual executable. So I'm not sure if the same strategy would work for gradle.

NateZimmer commented 2 years ago

Ultimately for gradle, you need to expose gradle.bat to your PATH. gradle.bat is located some level within the bin directory of a gradle installation. Also note, %USERDIR%/.gradle folder won't appear now until you build atleast 1 sample android project which dynamically pulls in the latest gradle. So both java & gradle that cordova needs can come from the latest 2022 android studio but you need to do some finagling to get it all set up. Alternatively as others reccomended, you can just download a gradle distribution and point your path to its gradle.bat file.

breautek commented 2 years ago

Closing.

It's now documented that Gradle needs to be installed.