You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

因Gradle版本变更导致NativeScript Jenkins编译失败的解决问询

NativeScript项目构建失败:Gradle版本兼容性问题及解决方案

问题背景

我的NativeScript项目通过Jenkins构建,7月9日之前一切正常,提交代码后构建失败,回退代码问题依旧。分析日志发现:tns处理firebase插件时自动下载的Gradle版本从7.6变为8.4,推测是nativescript-plugin-firebase等组件存在版本兼容性问题。

成功构建日志片段

Entrypoint bundle = runtime.js vendor.js bundle.js
[../$$_lazy_route_resource lazy recursive] ../$$_lazy_route_resource lazy namespace object 160 bytes {bundle} [built]
[./app.css] 10.8 KiB {bundle} [built]
[./app/app-routing.module.ts] 1.13 KiB {bundle} [built]
[./app/app.component.ts] 13 KiB {bundle} [built]
[./app/app.module.ts] 27.1 KiB {bundle} [built]
[./app/classes/custom-translate-loader.ts] 438 bytes {bundle} [built]
[./app/classes/modal-helper/modal-dialog-parameters.ts] 967 bytes {bundle} [built]
[./app/customPipe/backer-transform.ts] 1.1 KiB {bundle} [built]
[./app/customPipe/budged-order-id.pipe.ts] 1.38 KiB {bundle} [built]
[./app/customPipe/labels-transform.ts] 1.16 KiB {bundle} [built]
[./app/customPipe/url-transform.ts] 1.45 KiB {bundle} [built]
[./app/services/auth/token.interceptor.ts] 6.42 KiB {bundle} [built]
[./app/services/localstorage-budget-order/localstorage-budget-order.service.ts] 10 KiB {bundle} [built]
[./environments/environment.ts] 1.3 KiB {bundle} [built]
[./main.ts] 2.71 KiB {bundle} [built]
    + 1367 hidden modules
Webpack compilation complete.
nativescript-plugin-firebase: /var/lib/jenkins/workspace/ript_Application_Android_develop/platforms/android/.pluginfirebaseinfo not found, forcing prepare!
nativescript-plugin-firebase: running release build or change in environment detected, forcing prepare!
Downloading https://services.gradle.org/distributions/gradle-7.6-bin.zip

失败构建日志片段

Entrypoint bundle = runtime.js vendor.js bundle.js
[../$$_lazy_route_resource lazy recursive] ../$$_lazy_route_resource lazy namespace object 160 bytes {bundle} [built]
[./app.css] 10.8 KiB {bundle} [built]
[./app/app-routing.module.ts] 1.13 KiB {bundle} [built]
[./app/app.component.ts] 13 KiB {bundle} [built]
[./app/app.module.ts] 27.1 KiB {bundle} [built]
[./app/classes/custom-translate-loader.ts] 438 bytes {bundle} [built]
[./app/classes/modal-helper/modal-dialog-parameters.ts] 967 bytes {bundle} [built]
[./app/customPipe/backer-transform.ts] 1.1 KiB {bundle} [built]
[./app/customPipe/budged-order-id.pipe.ts] 1.38 KiB {bundle} [built]
[./app/customPipe/labels-transform.ts] 1.16 KiB {bundle} [built]
[./app/customPipe/url-transform.ts] 1.45 KiB {bundle} [built]
[./app/services/auth/token.interceptor.ts] 6.42 KiB {bundle} [built]
[./app/services/localstorage-budget-order/localstorage-budget-order.service.ts] 10 KiB {bundle} [built]
[./environments/environment.ts] 1.3 KiB {bundle} [built]
[./main.ts] 2.71 KiB {bundle} [built]
    + 1367 hidden modules
Webpack compilation complete.
nativescript-plugin-firebase: /var/lib/jenkins/workspace/ript_Application_Android_develop/platforms/android/.pluginfirebaseinfo not found, forcing prepare!
nativescript-plugin-firebase: running release build or change in environment detected, forcing prepare!
Downloading https://services.gradle.org/distributions/gradle-8.4-bin.zip

当前报错情况

  1. 初始报错:提示构建需要Java 17而非11,安装Java 17后出现新错误:
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'background_http'.
> Failed to notify project evaluation listener.
   > Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
      > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.
        
        If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.
   > Could not get unknown property 'generateReleaseBuildConfig' for root project 'background_http' of type org.gradle.api.Project.
  1. 修改node_modules/nativescript/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties指定Gradle 7.6后,出现错误:
* What went wrong:
A problem occurred evaluating root project 'background_http'.
> Failed to apply plugin 'com.android.internal.version-check'.
   > Minimum supported Gradle version is 8.4. Current version is 7.6. If using the gradle wrapper, try editing the distributionUrl in platforms/tempPlugin/background_http/gradle/wrapper/gradle-wrapper.properties to gradle-8.4-all.zip

解决方案

方法一:通过NativeScript配置固定Gradle版本

在项目根目录的App_Resources/Android/app.gradle文件中添加以下配置,强制指定Gradle和Android Gradle Plugin(AGP)版本:

buildscript {
    ext {
        gradleVersion = "7.6"
        androidGradlePluginVersion = "7.4.2" // 与Gradle 7.6兼容的AGP版本
    }
    dependencies {
        classpath "com.android.tools.build:gradle:${androidGradlePluginVersion}"
    }
}

方法二:锁定Gradle wrapper配置(避免插件覆盖)

  1. 复制项目根目录下自动生成的gradle/wrapper/gradle-wrapper.properties文件(若不存在则手动创建),修改distributionUrl为:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
  1. 在项目的package.json中添加postinstall脚本,确保每次安装依赖后自动同步该配置:
{
  "scripts": {
    "postinstall": "cp gradle/wrapper/gradle-wrapper.properties node_modules/nativescript/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties"
  }
}

方法三:降级不兼容的插件

检查nativescript-plugin-firebase和background_http插件版本,降级到与Gradle 7.6兼容的版本:

  • 查看插件的官方文档确认兼容版本
  • 修改package.json中的插件版本号,执行npm install重新安装

关键说明

  • Gradle与AGP版本必须匹配:Gradle 7.6对应AGP 7.4.x版本,避免版本不匹配导致的构建错误
  • 不要直接修改node_modules内的文件,因为依赖重装后会被覆盖,使用postinstall脚本或项目配置来持久化修改

内容的提问来源于stack exchange,提问作者Miguel Mesquita Alfaiate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:27:33