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

通过Bitrise工作流编辑build.gradle统一React Native Android依赖构建工具版本

Automatically Add buildToolsVersion Configuration to Root build.gradle via Bitrise Workflow

To automate adding your desired build tool version configuration to the root build.gradle file for your React Native Android project, you can use a Script step in your Bitrise workflow. Here's a practical, step-by-step solution:

Step 1: Add a Script Step to Your Bitrise Workflow

  1. Open your Bitrise project dashboard and head to the Workflow tab.
  2. Pick the stage where you want to inject this configuration—ideally right after the "Install dependencies" step, before the "Build Android" step.
  3. Click Add Step, search for the Script step (under the "General" category), and add it to your workflow.

Step 2: Configure the Script to Edit build.gradle

Replace the default script content with the following shell code. This script checks if your configuration already exists, and adds it to the root build.gradle only if it's missing:

# Define the code snippet we need to add
CODE_SNIPPET='subprojects {
    afterEvaluate { subproject ->
        if ((subproject.plugins.hasPlugin('\''android'\'') || subproject.plugins.hasPlugin('\''android-library'\'') )) {
            android {
                buildToolsVersion "26.0.2"
            }
        }
    }
}'

# Check if the configuration is already present to avoid duplicates
if ! grep -q "buildToolsVersion \"26.0.2\"" ./build.gradle; then
    # Append the code snippet to the end of the root build.gradle file
    echo "$CODE_SNIPPET" >> ./build.gradle
    echo "✅ Successfully added buildToolsVersion configuration to root build.gradle"
else
    echo "ℹ️ buildToolsVersion configuration already exists in root build.gradle"
fi

Quick Breakdown of the Script:

  • Path Accuracy: Bitrise runs scripts from your project's root directory, so ./build.gradle directly targets the correct file.
  • Idempotent Design: The grep check ensures you won't end up with duplicate code if the workflow runs multiple times.
  • Quoting Handling: Single quotes in the code snippet are escaped with \' to play nicely with the shell environment.

Optional: Insert Code at a Specific Location

If you want to keep your build file structure organized (e.g., inserting the subprojects block right after the allprojects block), use this modified sed command instead of the echo line:

# For Ubuntu-based Bitrise stacks (GNU sed)
sed -i '/allprojects {/a '"$CODE_SNIPPET"'' ./build.gradle

# For macOS-based Bitrise stacks
sed -i '' '/allprojects {/a '"$CODE_SNIPPET"'' ./build.gradle

This will place your configuration immediately after the allprojects { line, matching standard Android build file conventions.

Step 3: Save and Test the Workflow

Save your workflow changes, then trigger a new build. The script will automatically handle adding the build tool version configuration—no manual edits required.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:38:02