apache / cordova-android

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

Android build fails with Android SDK Build-Tools 31.0.0 #1288

Closed kputh closed 1 year ago

kputh commented 2 years ago

Bug Report

Problem

When I run

cordova build android --release -- --gradleArg=-PcdvBuildToolsVersion=30.0.3

I get the following error:

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=undefined (DEPRECATED)
Using Android SDK: /Users/kaiputh/Library/Android/sdk
Reading build config file: /Users/kaiputh/code/project/feuerwehr/viewer/build.json
Subproject Path: CordovaLib
Subproject Path: app
Build-tool 31.0.0 is missing DX at /Users/kaiputh/Library/Android/sdk/build-tools/31.0.0/dx
Build-tool 31.0.0 is missing DX at /Users/kaiputh/Library/Android/sdk/build-tools/31.0.0/dx

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':CordovaLib:compileReleaseAidl'.
> Failed to query the value of task ':CordovaLib:compileReleaseAidl' property 'aidlFrameworkProvider'.
   > Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager.

* 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 1s
Command failed with exit code 1: /Users/kaiputh/code/project/feuerwehr/viewer/platforms/android/gradlew cdvBuildRelease -b /Users/kaiputh/code/project/feuerwehr/viewer/platforms/android/build.gradle -PcdvBuildToolsVersion=30.0.3

The build seems to ignore the pinned Android Build-Tools version.

This issue started appearing in our build pipeline between July 19., 13:20 UTC and July 20., 08:38 UTC. I can't tell when the Android SDK Build-Tools 31.0.0 were release, but cordova-android 10.0.0 was release July 20., 04:17 (timezone?). That may be no coincidence.

What is expected to happen?

The build should succeed.

What does actually happen?

The build fails.

Information

Command or Code

cordova build android --release -- --gradleArg=-PcdvBuildToolsVersion=30.0.3

Environment, Platform, Device

Azure Pipelines, vmImage 'macOS-10.15', and local development (see below)

Version information

Cordova Packages:

Project Installed Platforms:

Project Installed Plugins:

Environment:

Checklist

kputh commented 2 years ago

Workaround

My first attempt to work around this issue was upgrading to cordova-android 10.0.0, and my second one was pinning the Android SDK Build-Tools version as seen above. When both failed, uninstalling the offending dependency did the trick. It can be done in Android Studio or on the command line:

$ANDROID_HOME/tools/bin/sdkmanager --uninstall "build-tools;31.0.0"
breautek commented 2 years ago

Migrated this to cordova-android for you since this is the package that is responsible for handling the android environment.

Our supported API level on cordova-android@10 is API 30, so build-tools version 30 should work. I've placed this issue in the 11.x milestone because generally we only officially support the next API level in a major releases. However, if it is possible to fix this without introducing breaking changes, then we may include it in a future minor release.

Currently cordova-android will look for the latest installed version of build tools, which is probably a bad idea for this very reason. Which is why the uninstalling build-tools 31 is the necessary workaround.

miketimofeev commented 2 years ago

@breautek am I right that none of these options will work and there is no way to pin the build-tools version?

image

breautek commented 2 years ago

@breautek am I right that none of these options will work and there is no way to pin the build-tools version?

Correct, and further investigation on the "why" identifies another bug.

The desired build tools get set here:

https://github.com/apache/cordova-android/blob/0ce66249dafc916c5c2197d4c92cc51b555f777b/templates/project/app/build.gradle#L192

LATEST_INSTALLED_BUILD_TOOLS gets set by:

https://github.com/apache/cordova-android/blob/0ce66249dafc916c5c2197d4c92cc51b555f777b/framework/cordova.gradle#L175-L177

Where BUILD_TOOLS_VERSION is comes from the config shown in your docs screenshot. As the doFindLatestInstalledBuildTools function name might suggest, it finds the latest build tools version, with the parameter being the "minimum" required version.

In fact, this behaviour did change as cordova-android 9.1 did:

https://github.com/apache/cordova-android/blob/c9108d8479641b92b1694b48237830ebbe6a6444/bin/templates/project/app/build.gradle#L164-L167

And only used findLatestInstalledBuildTools if the user parameter wasn't specified. I do believe this wasn't intentional and this issue probably should be reported as a separate issue, which I've already done here.

pawan-logiciel commented 2 years ago

Hey @kputh In order to fix the issue, firstly go to the following location:

C:\Users\User\AppData\Local\Android\Sdk\build-tools\31.0.0

Then find the file d8 (Windows batch file) and rename it to dx.

Then go to:

C:\Users\User\AppData\Local\Android\Sdk\build-tools\31.0.0\lib

Then find the file d8 (Executable Jar File) and rename is to dx.

Your problem will be solved now.

https://stackoverflow.com/questions/68387270/android-studio-error-installed-build-tools-revision-31-0-0-is-corrupted

I have also the same problem which gets resolved with above solution

kputh commented 2 years ago

@pawan-logiciel, thank you for the workaround. Unfortunately, I can't use it in out build pipeline

kputh commented 2 years ago

Sorry, I hit the wrong button.

breautek commented 2 years ago

The PR https://github.com/apache/cordova-android/pull/1293 should provide a better workaround (by fixing the actual gradle arg parameter).

Hoping it will be reviewed and be included in our patch release.

breautek commented 2 years ago

We also merged in https://github.com/apache/cordova-android/pull/1294 which will fix cordova-android to not blindly take in any build tools version, but limit the scope to use the latest patch of a particular major version. We are planning on doing a patch release sometime soon (cannot give any more details then that) so this should address the issue without using any actual parameters, though using the gradle argument to pin to a specific version might be still ideal.

As for supporting build tools 31, it looks like cordova android needs to use AGP 7 which also requires Java 11. These are all breaking changes, so I'll leave this issue open and inside our 11.x milestone.

LucasFebatis commented 2 years ago

I'm working with Ionic and it's forcing me to use cordova-android 9.1.0 and I'm getting the same error build tools 31.0.0

I found out that in the cordovaLib module that is generated, in build.gradle it generates with this snippet

ext {
    apply from: 'cordova.gradle'
    cdvCompileSdkVersion = privateHelpers.getProjectTarget()
    cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
    if (project.hasProperty('cdvMinSdkVersion') && cdvMinSdkVersion.isInteger()) {
        cdvMinSdkVersion = cdvMinSdkVersion as int
        println '[Cordova] cdvMinSdkVersion is overridden, try it at your own risk.'
    } else {
        cdvMinSdkVersion = 22; // current Cordova's default
    }
}

Which makes any attempt to change buildtools by environment variable or arguments impossible, because it will always use the most current version installed on the machine.

What is simple to be solved in a local development, boring but simple.

But now to update on my CI/CDs that already come with all versions available to me, what do I do?

I only see two work around

breautek commented 2 years ago

@LucasFebatis android version 10.0.1 (unreleased, pending votes) will fix the platform so that it will find the latest build tools release of a specific major rather than blindly choosing the latest available. Additionally the same version also fixes the flag (regression introduced in 10.0.0) so that if declared it will use a specific build tools version of your choice, as long as it's >= our minimum build tools version.

You can give it a try by installing the nightly version. Note that Cordova android 9.x won't be patched.

boredom2 commented 2 years ago

Hi there. After "cordova platform add android@10.0.1" and then "cordova build" (without any Plugins, just the Skeleton App), I still get "Failed to find Build Tools revision 31.0.0-rc5". What am I missing?

(before that message, I receive "Build-tool 31.0.0 rc5 is missing DX at C:\Users*\AppData\Local\Android\sdk\build-tools\31.0.0-rc5\dx.bat") Shouldnt the Fix only consider Build Tools from 30.* instead of 31?

Thanks!

dattakadiyala commented 2 years ago

if any one using Azure devOps, Microsoft hosted macOS agents, adding this as a command-line task before the build should do SDKMANAGER=$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager echo y | $SDKMANAGER --uninstall "build-tools;31.0.0"

breautek commented 2 years ago

(before that message, I receive "Build-tool 31.0.0 rc5 is missing DX at C:\Users\AppData\Local\Android\sdk\build-tools\31.0.0-rc5\dx.bat") Shouldnt the Fix only consider Build Tools from 30. instead of 31?

Thanks!

cordova-android@10.0.1 does include a patch that does just that. I wonder if the pre-release label however is causing problems. This was tested against a production release version (e.g. version 31.0.0).

jaydrogers commented 2 years ago

Just adding a note that I adopted @breautek's work around from this comment: https://github.com/apache/cordova-android/issues/1288#issuecomment-884423978

His workaround pointed me in the right direction, but I needed to make a few changes....

The error I was seeing

What brought me to this thread was seeing this error message:

No usable Android build tools found. Highest 30.x installed version is 30.0.2; minimum version required is 30.0.3.

Then when I upgraded to 31.0.0, I realized that did not work either 😅

First, I had a different ENV var

I saw @breautek's comment and noticed his variable was ANDROID_HOME, but mine was ANDROID_SDK_ROOT. Thankfully I noticed this in my logs:

ANDROID_SDK_ROOT=/Users/jay/Library/Android/sdk (recommended setting)
ANDROID_HOME=undefined (DEPRECATED)

So I changed the command to:

$ANDROID_SDK_ROOT/tools/bin/sdkmanager --uninstall "build-tools;31.0.0"

I then installed version 30.0.3:

$ANDROID_SDK_ROOT/tools/bin/sdkmanager --install "build-tools;30.0.3"

Hope this helps as people smarter than me helps diagnose an actual solution 🤓

Thanks for your hard work!

pcwSlide commented 2 years ago

The PR #1293 should provide a better workaround (by fixing the actual gradle arg parameter).

Hoping it will be reviewed and be included in our patch release.

Hey Norman, I agree it needs to addressed correctly. I just removed the 31 sdk - works ok BUT- and yes - another 6 months has passed :) - it does NOT invoke the emulator automatically - it has to be open to be seen by Cordova - an issue ( we - you ) finally fixed last round.. Do I open a new ticket for this? Phil

NitzDKoder commented 2 years ago

@pawan-logiciel saved by day!

breautek commented 2 years ago

The PR #1293 should provide a better workaround (by fixing the actual gradle arg parameter). Hoping it will be reviewed and be included in our patch release.

Hey Norman, I agree it needs to addressed correctly. I just removed the 31 sdk - works ok BUT- and yes - another 6 months has passed :) - it does NOT invoke the emulator automatically - it has to be open to be seen by Cordova - an issue ( we - you ) finally fixed last round.. Do I open a new ticket for this? Phil

Sorry for the late response. I'm not sure if I'm fully understanding your statement but Build Tools 31 requires breaking changes to be done on our end. I don't think we need a new ticket for this, I think this ticket is sufficient is in our cordova-android@11 milestone so we can keep track of issues required to be resolved once cordova-android@11 development starts.

pcwSlide commented 2 years ago

Yes Build 31 does not work - so I went back a version and it did compile but once again sending to the android emulator fails to engage - you have to manually open the emulator first - same old issue.

breautek commented 2 years ago

Yes Build 31 does not work - so I went back a version and it did compile but once again sending to the android emulator fails to engage - you have to manually open the emulator first - same old issue.

Ok, emulator usage is definitely not related to this issue and should be reported as a separate ticket.

webb24h commented 2 years ago

Just adding a note that I adopted @breautek's work around from this comment: #1288 (comment)

His workaround pointed me in the right direction, but I needed to make a few changes....

The error I was seeing

What brought me to this thread was seeing this error message:

No usable Android build tools found. Highest 30.x installed version is 30.0.2; minimum version required is 30.0.3.

Then when I upgraded to 31.0.0, I realized that did not work either

First, I had a different ENV var

I saw @breautek's comment and noticed his variable was ANDROID_HOME, but mine was ANDROID_SDK_ROOT. Thankfully I noticed this in my logs:

ANDROID_SDK_ROOT=/Users/jay/Library/Android/sdk (recommended setting)
ANDROID_HOME=undefined (DEPRECATED)

So I changed the command to:

$ANDROID_SDK_ROOT/tools/bin/sdkmanager --uninstall "build-tools;31.0.0"

I then installed version 30.0.3:

$ANDROID_SDK_ROOT/tools/bin/sdkmanager --install "build-tools;30.0.3"

Hope this helps as people smarter than me helps diagnose an actual solution

Thanks for your hard work!

I confirm this works. Solution for me.

almothafar commented 2 years ago

(before that message, I receive "Build-tool 31.0.0 rc5 is missing DX at C:\Users\AppData\Local\Android\sdk\build-tools\31.0.0-rc5\dx.bat") Shouldnt the Fix only consider Build Tools from 30. instead of 31? Thanks!

cordova-android@10.0.1 does include a patch that does just that. I wonder if the pre-release label however is causing problems. This was tested against a production release version (e.g. version 31.0.0).

I got version 10.1.0 And seems does not solve the issue

image

I manually changed it in Android Studio to 30.0.3

balaji-subramaniyam commented 2 years ago

Hi @breautek, I have doubt that cordova build android --release -- --gradleArg=-PcdvBuildToolsVersion=30.0.3 by using this the app can able to work on Android 11 devices.

Thanks in advance.

almothafar commented 2 years ago

Any update on this? also android platform 32 and built tools 32.0.0 have already been released!

JC0NN0R commented 2 years ago

Hey @kputh In order to fix the issue, firstly go to the following location:

C:\Users\User\AppData\Local\Android\Sdk\build-tools\31.0.0

Then find the file d8 (Windows batch file) and rename it to dx.

Then go to:

C:\Users\User\AppData\Local\Android\Sdk\build-tools\31.0.0\lib

Then find the file d8 (Executable Jar File) and rename is to dx.

Your problem will be solved now.

https://stackoverflow.com/questions/68387270/android-studio-error-installed-build-tools-revision-31-0-0-is-corrupted

I have also the same problem which gets resolved with above solution

Bro I spent all day trying to get the Android Mobile setup for the Unreal Engine and this absolutely worked....I'm running on Windows 11 so I don't know if that had anything to do with it. Your comment made my day.

breautek commented 1 year ago

Resolved by https://github.com/apache/cordova-android/pull/1427