How to run gradle from the command line for Android Studio when versions mismatch? - android-studio

I've got an Android Studio project and have been building it just using Build/Rebuild Project - and it builds fine.
However I'm making additions to the build.gradle file and am having an issue using AS as often the output in the Gradle Console within AS just vanishes. This means if I've made a mistake with the build.gradle additions I can't see what the problem is.
So I thought I'd run things from the command line. So I just type 'gradle' from my mac's terminal window but I get this:
A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'com.android.application']
> Gradle version 2.2 is required. Current version is 2.10. If using the gradle wrapper, try editing the distributionUrl in /Users/Cequint/Desktop/CityIDGradle/android/CityID/gradle/wrapper/gradle-wrapper.properties to grade-2.2-all.zip
The gradle-wrapper.properties file in AS contains this:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
Yet if I type 'gradle -version' from the command line it shows I have Gradle 2.10 installed.
I tried changing the cradle-wrapper.properties file in AS to contain grade-2.2-all.zip as it said in the error message, but it made no difference.
I also tried changing it to be grade-2.10-all.zip but its still the same error message.
So the question is:
1) what can I update in the AS gradle files to build from the command line?
2) If I have a different version of gradle downloaded onto the mac, than got installed with AS. How can I download the version with AS to be the same i.e. to be v2.10?

When you run gradle from the command line, it invokes gradle from your system path, which apparently is gradle 2.10
When you add a wrapper section to your gradle.build file, as is the case with Android Studio projects, you will see a gradlew.bat and a gradlew shell script in the project root folder. These scripts will invoke (and download if required) the version of gradle defined in your wrapper section.
TL;DR: Run ./gradlew instead of gradle

Related

Could not find com.android.tools.build:gradle:7.5.1

So I was building out a test APK in Android Studio tonight that had to be signed. It failed and said my Gradle was out of date. So I upgraded my gradle from 7.3.3 to the newest 7.5.1
I changed the wrapper setting to distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
and then I changed my build.gradle dependency to classpath 'com.android.tools.build:gradle:7.5.1'
Note that after doing the changes, and restarting Android Studio, it did upgrade the version in the app directory.
Here's the error getting thrown below:
* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all files for configuration ':classpath'.
> Could not find com.android.tools.build:gradle:7.5.1.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.5.1/gradle-7.5.1.pom
- https://repo.maven.apache.org/maven2/com/android/tools/build/gradle/7.5.1/gradle-7.5.1.pom
Required by:
project :
Note that this was a project from a client, and I did not write this originally. These dependencies were in place, but fairly recent, and the app would build fine with it using 7.3.3. However it wouldn't let me sign my app.
I suppose I could use some wisdom on both of these issues. What I'm going to do is go back to version 7.3.3, build the signed APK again, and copy that exact error to report back here with. It was a gradle version error.
For any any Flutter noobs like myself passing through, this error occured because the Android Tools Plugin version DOES NOT match the Gradle Version:
https://developer.android.com/studio/releases/gradle-plugin#groovy
They match up just vaguely enough in number, that you would think that my line com.android.tools.build:gradle:7.5.1. uses gradle 7.5.1, but instead, Android Tools for Gradle is actually a separate entity, and at 7.3.3 as of typing this reply.
So you CAN set your Gradle to the newest version somewhere else in your project, but it has to match the compatible Android Tools version, which is set here. Its silly and confusing at first, and can easily trip up newcomers. Hopefully the versions diverge enough, or the code syntax changes for some better clarity, but as of now, this is it.

Android Studio and gradle home issue - Ubuntu

I'm using Ubuntu 18.04
Android Studio Version 3.6.3
I think I have two installed Gradle locations If I'm not wrong
One of them installed using sdkman, when I use terminal to locate the Gradle
which gradle
Also, I've found (I don't remember if I installed Gradle in another way instead of sdkman) in my home directory, this is the one which Android Studio is using (I think) and make issues when opening a project
I think I need to use sdkman as it's easier to update the Gradle version etc.
So, my question is should I remove the gradle from: "/home/khaledomara/.gradle". If so, what is the best method to do that and what path should I put in the settings of Android studio?
Should it be "/home/khaledomara/.sdkman/candidates/gradle/current/bin/gradle"?
You only have one Gradle installed - the ~/.gradle directory is where a running Gradle process stores data and it can be safely deleted (assuming you don't have a gradle.properties or similar with any settings you want to save - but there is no need to delete it).
It is not a bad idea to do a gradle init in your Android Studio project - this will give your project its own Gradle that you can upgrade at your leisure without having to rely on the globally installed Gradle.

android studio/flutter infinitely says "initializing gradle"

I'm trying to run the demo flutter app through android studio but but whenever I hit run, it says "initializing gradle" on the terminal and never runs the application. I ran the flutter doctor command and it says that there are zero issues so I'm not sure what to do.
Firstly, make sure your Gradle version is above gradle-4.6-all
Then, change this property:
distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
It works!
In gradle-wrapper.properties file make sure distributionUrl is set to your installed gradle version. check your .gradle\wrapper\dists location for your version. make sure you have 4.6 or above.
you need open projectPath/android/gradle/wrapper/gradle-wrapper.properties, find distributionUrl, download gradle file from distributionUrl, move gradle file(don't unzip file) to
C:\Users\.gradle\wrapper\dists\\

ARToolKit ARSimpleProj Error

When I open the ARSimpleProj in Android Studio and try to update the gradle version I get this message:
Failed to update the version of the Android Gradle plugin.
Please click 'OK' to perform a textual search and then update the build files manually.
After this, I'm getting this error:
Error:(58, 0) Gradle DSL method not found: 'android()'
Possible causes:
* The project 'ARSimpleProj' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0). Fix plugin version and sync project
* The project 'ARSimpleProj' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
* The build file may be missing a Gradle plugin. Apply Gradle plugin.
I tried removing the android->buildToolsVersion and dependencies tag from the top-level, build.gradle file (as indicated in the AS_Migration document) but still it doesn't work.
I finally make it work. When opening the project on the Android Studio, I select "Remind me later" to the Android Gradle Plugin Update recommendation. After this, I could execute and install the application on my cell phone.
Make sure you are using JDK 1.7 in Project Structure. I had problems when using the JDK 1.8 with ARToolkit for Android.

Android studio preview 0.4.5: The project is using an unsupported version of the Android Gradle plug-in

I got the following error message when I was importing a project from github:
Failed to refresh Gradle project 'XXX'
The project is using an unsupported version of the Android Gradle plug-in.
Fix plug-in version and re-import project
This is not a duplicate of this because your get thee error message before
gradle/wrapper/gradle-wrapper.properties
is generated.
The problem appeared after updates. The updated Android Gradle plug-in did not support the outdated version of Gradle I had. But the error message is misleading, if you don't attempt different build options you won't see what the problem is.
After downloading a newer version of Gradle, Imported the project again, then:
Use local gradle distribution
Then Gradle home set to the folder where the new version of gradle was downloaded
that solved it
Edit: From Scott (below comments) : It's preferable to use the wrapper and to change the Gradle version in gradle-wrapper.properties instead of downloading and installing Gradle yourself. As for the Android-Gradle plugin, that's set in the dependencies.classpath block in your build file; see this
I get this gradle-errors in 4 Studio updates! My solution: I copy the Studio-folder, and if gralde throws an error, I copy the old version back. It's not a good way, but until google is not able to deliver an update without producing always the same errors (even if mentioned in many forums), I stay on 4.9 and wait for the first final release.

Resources