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

Expo+React Native项目EAS构建中如何添加Android依赖至build.gradle?

在Expo EAS构建中添加Android Gradle依赖

方法1:使用Expo Config Plugin(推荐)

这是最贴合Expo生态的方案,通过自定义插件可以在EAS构建阶段自动修改Android的build.gradle文件,无需手动操作。

  • 步骤1:在项目根目录创建插件文件,比如withThirdPartyDependency.js
  • 步骤2:编写插件逻辑,自动注入依赖:
const { withAppBuildGradle } = require('expo/config-plugins');

module.exports = function withThirdPartyDependency(config) {
  return withAppBuildGradle(config, (config) => {
    // 处理Groovy格式的build.gradle
    if (config.modResults.language === 'groovy') {
      config.modResults.contents = config.modResults.contents.replace(
        /dependencies {/,
        `dependencies {
        implementation 'com.x.x.x:v'`
      );
    } 
    // 处理Kotlin DSL格式的build.gradle.kts(如果项目使用)
    else {
      config.modResults.contents = config.modResults.contents.replace(
        /dependencies {/,
        `dependencies {
        implementation("com.x.x.x:v")`
      );
    }
    return config;
  });
};
  • 步骤3:在app.json或app.config.js中注册插件:
{
  "expo": {
    "plugins": ["./withThirdPartyDependency.js"]
  }
}

方法2:使用EAS构建前脚本

若不想编写Config Plugin,可通过预安装脚本在构建前修改build.gradle:

在项目package.json中添加eas-build-pre-install脚本:

{
  "scripts": {
    // macOS环境适用
    "eas-build-pre-install": "sed -i '' 's/dependencies {/dependencies {\\n        implementation \\'com.x.x.x:v\\'/g' android/app/build.gradle"
  }
}

如果构建环境是Linux,需调整sed命令参数:

{
  "scripts": {
    "eas-build-pre-install": "sed -i 's/dependencies {/dependencies {\\n        implementation \\'com.x.x.x:v\\'/g' android/app/build.gradle"
  }
}

注意:这种方法依赖build.gradle的格式稳定性,若文件结构变更可能失效。

方法3:Bare项目直接提交修改(仅适用于Bare React Native项目)

如果你的项目是Bare架构,可直接修改android/app/build.gradle添加依赖,然后提交到版本控制。EAS构建时会直接使用该文件;但Managed架构项目不可用,因为其gradle文件由Expo自动生成,提交的修改会被覆盖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 18:42:41