apache / cordova-android

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

cordova-android build tools not found issue #1691

Closed thangkho closed 2 months ago

thangkho commented 3 months ago

I create new project and add android platform. When I run 'sudo cordova build android' it build FAILED

v002024@v002024s-Mac-mini hello % sudo cordova build android
Password:
Checking Java JDK and Android SDK versions
ANDROID_HOME=undefined (recommended setting)
ANDROID_SDK_ROOT=undefined (DEPRECATED)
Using Android SDK: /opt/homebrew/Caskroom/android-platform-tools/33.0.3
Subproject Path: CordovaLib
Subproject Path: app
Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* Where:
Script '/Users/v002024/Documents/cordova/hello/platforms/android/CordovaLib/cordova.gradle' line: 69

* What went wrong:
A problem occurred evaluating script.
> No installed build tools found. Please install the Android build tools version 33.0.2.

* 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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 3s

I flow https://github.com/apache/cordova-android/issues/1613 and add JAVA_HOME, ANDROID_HOME, ANDROID_SDK_ROOT into zshrc export PATH="/Users/v002024/FlutterDev/flutter/bin:$PATH" export PATH="$PATH":"$HOME/.pub-cache/bin" export JAVA_HOME="/Users/v002024/Library/Java/JavaVirtualMachines/jdk1.8.0_381.jdk/Contents/Home" export ANDROID_HOME=/Users/v002024/Library/Android/sdk export ANDROID_SDK_ROOT=$ANDROID_HOME

export PATH=$PATH:$ANDROID_HOME/platform-tools export PATH=$PATH:$ANDROID_HOME/tools export PATH=$PATH:$ANDROID_HOME/tools/bin export PATH=$PATH:$ANDROID_HOME/emulator

When I check ANDROID_HOME and ANDROID_SDK_ROOT it return same value

Mac-mini hello % echo $ANDROID_HOME
/Users/v002024/Library/Android/sdk
Mac-mini hello % echo $ANDROID_SDK_ROOT
/Users/v002024/Library/Android/sdk

In Android Studio I have Android SDK Location and build tools

Screenshot 2024-01-16 at 17 29 05

I check in build-tools folder, 30.0.2 already:

Screenshot 2024-01-16 at 16 08 04 When I run cordova requirements, it's ok

Screenshot 2024-01-16 at 17 31 21

I use cordova 12.0.0 (cordova-lib@12.0.1)

Please help me fix it!

breautek commented 3 months ago

I create new project and add android platform. When I run 'sudo cordova build android' it build FAILED

TL;DR; Shouldn't use sudo. If you npm installed or cordova plugin/platform add with sudo, then running without sudo may be difficult. In which case you may want to delete your node_modules/, plugins/, and platforms/ so you can reinstall without sudo. This makes all the files owned by your own user, not by the root user.

When you run with sudo (aka root) you may be running under a different environment. For example it looks like your Android SDK and JDK is installed for a local single user, v002024, not the root user.

While echo $ANDROID_HOME and sudo echo $ANDROID_HOME will produce a printout as expected. Running:

sudo su
echo $ANDROID_HOME

does not produce what you'd expect. Cordova launches several sub-processes including calling on gradle, java, and other binaries, so it's about the equivalent of doing the above. It won't have the environment variables that you set as expected. The root user account needs to be configured appropriately for this to work, HOWEVER, it's also not secure to be running under the root. You're running a lot of software that I'm sure you haven't vetted yourself, or it can change easily without you knowing. Using root for develpoment tools makes you very susceptible to supply chain attacks so it's advisable to install locally without the sudo/root privileges.

If you've installed NodeJS through the official NodeJS channel, the NPM shipped is configured to install global packages in a directory where root is required to access. Which might be why you're using sudo to begin with. If this is the case, have a read at fixing-npm-permissions.md, which allows you to reconfigure to a directory that your local user has read/write access to. This will avoid the sudo requirement when installing global packages. After changing the install prefix, you will have to reinstall your global packages.

If you want to quickly confirm if this the issue without messing with your environment, try doing this:

cd ~/
cordova create testapp
cd testapp
cordova platform add android
cordova build android

The build should be successful, no sudo was use.