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

不提交local.properties,如何通过GitHub Actions构建Android项目?

解决方案:无需提交local.properties即可通过GitHub Actions构建Android项目

当然可以做到,不需要提交local.properties就能完成GitHub Actions构建,以下是具体实现步骤:

1. 在GitHub仓库配置API密钥

  • 打开你的GitHub仓库,进入「Settings」→「Secrets and variables」→「Actions」页面
  • 点击「New repository secret」,创建名为API_KEY的密钥,填入你原本存放在local.properties中的API值并保存

2. 修改Gradle构建脚本,兼容本地与CI环境

调整Module级build.gradle.kts的代码,让它优先读取环境变量,本地环境再回退到local.properties,避免文件不存在的错误:

defaultConfig {
    minSdk = 24
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    consumerProguardFiles("consumer-rules.pro")

    // 优先从环境变量取API_KEY,本地环境则读取local.properties
    val apiKey = System.getenv("API_KEY") ?: run {
        val properties = org.jetbrains.kotlin.konan.properties.Properties()
        val localPropsFile = project.rootProject.file("local.properties")
        if (localPropsFile.exists()) {
            properties.load(localPropsFile.inputStream())
            properties.getProperty("API_KEY") ?: ""
        } else {
            ""
        }
    }

    buildConfigField("String", "API_KEY", "\"$apiKey\"")
}

3. 更新GitHub Actions Workflow配置

在yml文件的Gradle运行步骤中注入API_KEY环境变量,让CI构建时能读取到仓库密钥:

name: Run github actions

on:
  push:
    branches: [master, develop]
  pull_request:
    branches:
      - '**' # All branches

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Upload app to AppSweep with Gradle
        env:
          APPSWEEP_API_KEY: ${{ secrets.APPSWEEP_API_KEY }}
          API_KEY: ${{ secrets.API_KEY }} # 新增该行注入API密钥
        run: ./gradlew uploadToAppSweepRelease

说明

这样修改后,本地开发时依然可以通过local.properties使用API密钥,而GitHub Actions构建时会直接读取仓库中配置的密钥,完全不需要依赖未提交的local.properties文件,同时彻底避免了敏感信息泄露的风险。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 19:05:33