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

Windows10下Cordova构建Android遇Gradle弃用特性报错求助

Cordova构建Android Gradle弃用特性报错解决步骤

首先执行命令定位具体警告源:

  • 进入项目Android平台目录:
    cd C:\CordovaApps\Apps\MyApp\platforms\android
    
  • 带全警告模式执行构建:
    gradlew cdvBuildDebug --warning-mode all
    

这条命令会输出具体的弃用细节,比如哪段脚本、哪个插件使用了过时的Gradle API,是后续修改的核心依据。

一、项目根build.gradle调整

  1. 匹配Gradle与Android Gradle插件(AGP)版本
    找到项目根目录platforms/android/build.gradle里的buildscript块,将AGP版本调整为与Gradle7.6兼容的7.4.x系列,比如:
    buildscript {
        dependencies {
            classpath 'com.android.tools.build:gradle:7.4.2'
        }
    }
    
  2. 指定Java编译版本
    在同一文件的android块中添加Java版本配置(Gradle7.6推荐Java11):
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
    }
    

二、修复插件层面的弃用问题

如果警告指向某款Cordova插件:

  • 优先升级插件到最新版:
    cordova plugin update [插件名称]
    
  • 若升级无效,找到插件目录下的build.gradle(路径示例:platforms/android/plugins/[插件名称]/build.gradle),替换过时的依赖声明:
    • 将compile替换为implementation
    • 将testCompile替换为testImplementation
    • 将androidTestCompile替换为androidTestImplementation

三、通过config.xml锁定版本

在项目根目录config.xml的Android平台配置中,强制指定Gradle相关版本,避免Cordova自动生成不兼容配置:

<platform name="android">
    <preference name="GradlePluginVersion" value="7.4.2" />
    <preference name="GradleWrapperVersion" value="7.6" />
    <preference name="android-targetSdkVersion" value="33" />
</platform>

最后清理缓存重新构建

cordova clean android
cordova build android

内容的提问来源于stack exchange,提问作者Samp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 16:46:21