"ionic cap add android" fails with "Error running gradle sync"

I have been building ionic for android and ios successfully until suddenly today something is wrong.

I deleted android directory: rm -rf android

I built my ionic project with no problems: ionic build --prod --release

I try ionic cap add android, and get this output:

$ ionic cap add android
> npm i -E @capacitor/android@latest

removed 1 package, and audited 1728 packages in 4s

1 package is looking for funding
  run `npm fund` for details

63 vulnerabilities (3 low, 50 moderate, 8 high, 2 critical)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.
> capacitor add android
[capacitor] âś” Adding native android project in android in 31.55ms
[capacitor] Error running gradle sync
[capacitor] FAILURE: Build failed with an exception.
[capacitor]
[capacitor] * What went wrong:
[capacitor] Executing Gradle tasks as part of an undefined build is not supported. Make sure that you are executing Gradle from a folder within your Gradle project. Your project should have a 'settings.gradle(.kts)' file in the root folder.
[capacitor]
[capacitor] * Try:
[capacitor] 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.
[capacitor]
[capacitor] * Get more help at https://help.gradle.org
[capacitor]
[capacitor] BUILD FAILED in 550ms
[capacitor]
[capacitor] Unable to infer default Android SDK settings. This is fine, just run npx cap open android and import and sync gradle manually

I cannot figure out what is suddenly wrong here…

Here is my info:

$ ionic info

Ionic:

   Ionic CLI                     : 6.16.1 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.5.1
   @angular-devkit/build-angular : 0.1000.8
   @angular-devkit/schematics    : 10.0.8
   @angular/cli                  : 10.0.8
   @ionic/angular-toolkit        : 2.3.3

Capacitor:

   Capacitor CLI      : 3.0.0
   @capacitor/android : 3.0.0
   @capacitor/core    : 3.0.0
   @capacitor/ios     : 3.0.0

Cordova:

   Cordova CLI       : 10.0.0
   Cordova Platforms : none
   Cordova Plugins   : no whitelisted plugins (1 plugins total)

Utility:

   cordova-res (update available: 0.15.3) : 0.15.2
   native-run                             : 1.3.1

System:

   Android SDK Tools : 26.1.1 (/Users/vtn2/Library/Android/sdk)
   NodeJS            : v12.19.0 (/usr/local/bin/node)
   npm               : 7.5.2
   OS                : macOS Big Sur
   Xcode             : Xcode 12.5 Build version 12E262

Is this possibly a Capacitor v3 problem?

(Note that when I just ignore this error and go on to try to build the project in Android Studio, i get “This version of the Android Support plugin for IntelliJ IDEA cannot open this project, please retry with version 4.2 or newer.” Not sure that is related or not…

This is frustrating! I’m so close to being done with this project, and now I can’t build it!

2 Likes

I have the same problem!
I could’t find any solutions.
I’m stuck with work.
I also changed gradle version. Nothing.

Ionic:

   Ionic CLI                     : 6.16.2 (xxxxxxx\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.6.4
   @angular-devkit/build-angular : 0.1102.9
   @angular-devkit/schematics    : 11.2.9
   @angular/cli                  : 11.2.9
   @ionic/angular-toolkit        : 3.1.1

Capacitor:

   Capacitor CLI      : 3.0.1
   @capacitor/android : 3.0.1
   @capacitor/core    : 3.0.1
   @capacitor/ios     : not installed

Utility:

   cordova-res : 0.15.3
   native-run  : 1.4.0

System:

   NodeJS : v14.17.0 (C:\Program Files\nodejs\node.exe)
   npm    : 7.16.0
   OS     : Windows 10

I upgraded Android Studio to 4.2.1 and all problems went away. I’m not sure if that’s the reason they went away, but they went away…

My Android Studio was already 4.2.1 …
No hope…

Gradle sync failed: Sync failed: reason unknown

I’m getting this too - did you get anywhere? (I’m also on 4.2.1 of Android Studio already)

Sorry, I’m stuck with it.
I suppose the problem is among Gradle and Capacitor 3.
I updated gradle, but without success: I got only some changes from the error messages.
I think we will have to wait some time…

For me I was just missing the required SDK version. Try installing the same SDK referenced in your project

1 Like

I have the latest android studio version installed (Android Studio Arctic Fox | 2020.3.1 Patch 3) and I’m still running into the same issue.

Has anyone found a working solution for this?

P.S.: I also have installed the required SDK version.

Solução é dentro do projeto realize a configuração do gradle:

gradle init

Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1…4] 2

Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1…6] 3

Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1…2] 2

Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1…2] 2

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
Project name (default: nameproject):
Source package (default: nameproject): br.com.nameproject

BUILD SUCCESSFUL in 1m 24s
2 actionable tasks: 2 executed

depois Ă© sĂł rodar o comando:
ionic capacitor run android

Abraços!

Wemerson Costa
Engenheiro de Software Mobile

1 Like