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

Mac端Gradle为何在条件不满足时仍要求解析可选依赖?

React Native构建Gradle依赖解析异常(Mac端独有)

背景与配置变更

原正常构建配置

buildscript {
    ext {
        buildToolsVersion = "33.0.0"
        minSdkVersion = 23
        compileSdkVersion = 33
        targetSdkVersion = 33
        ndkVersion = "23.1.7779620"
        kotlinVersion = "1.8.0"
        kotlin_version = "1.8.0"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        ...plugins
    }
}

适配HMS后的修改配置

buildscript {
    ext {
        ...same versions
        // Required due to a Notifee & HMS issue:
        reactNativeProjects = []
    }
        
    repositories {
        maven { url 'https://developer.huawei.com/repo/' }
        if (System.getenv("ANDROID_VENDOR") == "huawei") {
            maven { url "$rootDir/../node_modules/@notifee/react-native/android/libs" }
            maven { url "https://pdftron-maven.s3.amazonaws.com/release" }
        }
        else {
            google()
        }
        mavenCentral()
    }
    dependencies {
        ...same plugins
        classpath('com.huawei.agconnect:agcp:1.4.1.300')
    }
}

allprojects {
    repositories {
        mavenCentral()
        maven { url 'https://developer.huawei.com/repo/' }
        if(System.getenv("ANDROID_VENDOR") == "huawei") {
            maven { url "https://pdftron-maven.s3.amazonaws.com/release" }
            maven { url "$rootDir/../node_modules/@notifee/react-native/android/libs" }
        }
    }
}

问题现象

当设置ANDROID_VENDOR=google时,构建抛出依赖解析错误:

> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not find any matches for app.notifee:core:+ as no versions of app.notifee:core are available.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/app/notifee/core/maven-metadata.xml
       - https://developer.huawei.com/repo/app/notifee/core/maven-metadata.xml
       - https://dl.google.com/dl/android/maven2/app/notifee/core/maven-metadata.xml
       - https://www.jitpack.io/app/notifee/core/maven-metadata.xml
     Required by:
         project :app > project :notifee_react-native

pdftron依赖也存在相同问题。移除allprojects中的if语句后,Google环境可正常构建,但pdftron仓库的慢解析会使构建耗时增加5分钟。仅Mac端出现该问题,Windows同事无异常,清理Gradle缓存无效。

原因分析

  1. 依赖解析的强制性
    :notifee_react-native模块会无条件声明对app.notifee:core:+的依赖,Gradle必须找到该依赖才能完成构建流程。由于该依赖未发布到公共maven仓库(google、mavenCentral等),仅存在于@notifee/react-native的本地libs目录中,当ANDROID_VENDOR=google时,你的allprojects.repositories未添加该本地仓库,因此触发找不到依赖的错误。

  2. Mac与Windows的环境变量差异
    Mac上环境变量的传递逻辑与Windows不同:

  • 若通过IDE(如Android Studio)启动构建,终端中设置的ANDROID_VENDOR变量不会自动注入IDE进程,导致System.getenv("ANDROID_VENDOR")返回null,无法进入预期的分支逻辑。
  • 部分终端环境(如zsh)的变量大小写敏感,若变量设置为ANDROID_VENDOR=GOOGLE(大写),会与代码中的"huawei"、"google"(小写)判断不匹配,导致仓库配置错误。
  1. Gradle配置阶段的特性
    Gradle的配置阶段会执行所有仓库声明逻辑,即使条件分支不满足,也会完成变量读取和分支判断。若环境变量读取异常,会导致仓库配置不符合预期,进而引发依赖解析失败。

解决方案

方案1:分离Notifee与Pdftron的仓库配置

由于Notifee的本地仓库在所有环境下都需要(依赖必须解析),仅将Pdftron的仓库限制在HMS环境:

allprojects {
    repositories {
        mavenCentral()
        google() // 补全原配置中缺失的google仓库
        maven { url 'https://developer.huawei.com/repo/' }
        // 所有环境都添加Notifee本地仓库
        maven { url "$rootDir/../node_modules/@notifee/react-native/android/libs" }
        if(System.getenv("ANDROID_VENDOR") == "huawei") {
            maven { url "https://pdftron-maven.s3.amazonaws.com/release" }
        }
    }
}

方案2:确保环境变量正确传递到Gradle进程

  • 终端构建:在终端执行构建前,确保变量正确设置(注意大小写):
    export ANDROID_VENDOR=google
    ./gradlew assembleDebug
    
  • IDE构建:在Android Studio的「Run/Debug Configurations」中,手动添加ANDROID_VENDOR=google到环境变量列表中。

方案3:彻底清理Mac端Gradle缓存

删除Mac上的Gradle用户缓存目录,确保无残留缓存影响:

rm -rf ~/.gradle/caches

内容的提问来源于stack exchange,提问作者Milan Smolík

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:57:03