NativeScript Android构建错误:无法找到com.android.tools.build:gradle:7.3
我基于现有项目模板创建了新的NativeScript JavaScript项目,iOS构建正常,但Android构建报错:
FAILURE: Build failed with an exception.
- What went wrong:
A problem occurred configuring root project 'nativescript_google_maps'.Could not resolve all files for configuration ':classpath'.
Could not find com.android.tools.build:gradle:7.3..
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.3./gradle-7.3..pom
- https://jcenter.bintray.com/com/android/tools/build/gradle/7.3./gradle-7.3..pom
Required by:
project :
项目package.json内容:
{"name": "rrr","main": "app/app.js","version": "1.0.0","private": true,"dependencies": {"@bradmartin/nativescript-urlhandler": "^2.0.1","@kefah/nativescript-google-maps": "1.0.7","@master.technology/permissions": "^2.0.1","@nativescript-community/ui-material-bottom-navigation": "7.0.2","@nativescript/appversion": "^2.0.0","@nativescript/contacts": "2.0.4","@nativescript/core": "8.3.5","@nativescript/email": "^2.0.5","@nativescript/firebase": "^11.1.3","@nativescript/geolocation": "8.0.2","@nativescript/iqkeyboardmanager": "2.0.0","@nativescript/theme": "^3.0.2","@triniwiz/nativescript-toasty": "^4.1.3","base-64": "^1.0.0","nativescript-bitmap-factory": "^1.8.1","nativescript-clipboard": "^2.1.1","nativescript-danem-google-maps-utils": "^1.0.18","nativescript-drop-down": "6.0.1","nativescript-pdf-view": "^3.0.0-1","nativescript-phone": "^3.0.3","nativescript-screenshot": "^0.0.2","nativescript-sqlite": "^2.8.6","nativescript-ui-listview": "10.0.2","nativescript-ui-sidedrawer": "10.0.2","patch-package": "^6.5.1"},"devDependencies": {"@nativescript/android": "8.1.1","@nativescript/ios": "8.2.3","@nativescript/webpack": "5.0.6"},"scripts": {"postinstall": "patch-package"}}
执行tns build android --log trace时发现新项目自动获取了@nativescript/android@8.4.0对应的Gradle版本(gradleVersion:7.5,gradleAndroidPluginVersion:7.3.),但原有项目无此行为。已尝试在buildscript.gradle添加依赖声明、多次执行tns clean、更新Android Studio构建工具,均无效。
tns doctor输出:
✔ Getting environment information
No issues were detected.
✔ Your ANDROID_HOME environment variable is set and points to correct directory.
✔ Your adb from the Android SDK is correctly installed.
✔ The Android SDK is installed.
✔ A compatible Android SDK for compilation is found.
✔ Javac is installed and is configured properly.
✔ The Java Development Kit (JDK) is installed and is configured properly.
✔ Xcode is installed and is configured properly.
✔ xcodeproj is installed and is configured properly.
✔ CocoaPods are installed.
✔ CocoaPods update is not required.
✔ CocoaPods are configured properly.
✔ Your current CocoaPods version is newer than 1.0.0.
✔ Python installed and configured correctly.
✔ The Python 'six' package is found.
✔ Xcode version 13.3.1 satisfies minimum required version 10.
✔ Getting NativeScript components versions information...
⚠ Update available for component nativescript. Your current version is 8.1.5 and the latest available version is 8.4.0.
⚠ Update available for component @nativescript/core. Your current version is 8.3.5 and the latest available version is 8.4.6.
⚠ Update available for component @nativescript/ios. Your current version is 8.2.3 and the latest available version is 8.4.1.
⚠ Update available for component @nativescript/android. Your current version is 8.1.1 and the latest available version is 8.4.0.
请问是否有办法控制构建时使用的Gradle版本?
解决方法
1. 锁定@nativescript/android版本
项目package.json已指定@nativescript/android为8.1.1,但可能存在缓存或依赖解析异常:
- 删除
node_modules、package-lock.json(或yarn.lock) - 重新执行
npm install - 检查
package-lock.json中@nativescript/android的版本是否为8.1.1,确保未被意外升级
2. 手动指定Gradle版本
在项目根目录的App_Resources/Android/buildscript.gradle文件(不存在则创建)中添加以下内容,强制指定兼容的Gradle版本:
buildscript { ext { gradleVersion = "7.4.2" // 适配@nativescript/android 8.1.1的Gradle版本 androidPluginVersion = "7.2.3" // 对应Android Gradle插件版本 } repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:$androidPluginVersion" } }
注意:版本需与@nativescript/android 8.1.1匹配,避免使用错误的版本号(如日志中缺失一位的7.3.)
3. 彻底清理缓存
执行以下命令清理NativeScript和项目缓存:
tns clean rm -rf ~/.nativescript rm -rf platforms/android
之后重新构建项目:
tns build android
4. 对齐全局CLI版本
全局NativeScript CLI版本(8.1.5)与项目依赖的@nativescript/android版本可能存在兼容性问题,降级全局CLI到匹配版本:
npm install -g nativescript@8.1.5
内容的提问来源于stack exchange,提问作者David

