首页 > 解决方案 > 使用 Actions 时将 build.gradle 中的 local.properties 与 Github Secrets 一起使用

问题描述

在使用 Github Actions 时,我试图用 Github Secrets 值替换位于 local.properties 中的 API 密钥。

键在 local.properties 中声明如下:

sdk.dir=/Users/xxxxxxx/Library/Android/sdk
API_KEY="xxxxxx"

然后,在我的 build.gradle 中,我得到这样的属性:

 buildTypes {

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def api = properties.getProperty('API_KEY')

    debug {
        buildConfigField 'String', "API_KEY", api
        resValue('string', "api", api)

               .....

然后运行 ​​Github Actions 以在此处将值替换为秘密:

  on:
  push:

jobs:
  unit_tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Make gradlew executable
        run: chmod +x ./gradlew
      - name: Update Username from Secrets
        env:
          MOCK_USERNAME: ${{ secrets.API_KEY }}
        run: echo API_KEY="$API_KEY" > ./local.properties

      - name: Assemble app debug APK
        run: bash ./gradlew assembleDebug --stacktrace

      - name: Unit tests
        run: ./gradlew test

但是,每次运行此操作时,构建都会失败并出现以下错误:

  • 出了什么问题:评估项目“:app”时出现问题。指定为非null的参数为null:方法com.android.build.gradle.internal.dsl.BuildType.resValue,参数值

它指向的行是 buildConfigField 'String', "API_KEY", api 行。

我在这里做错了什么?

标签: androidcontinuous-integrationandroid-gradle-plugingithub-actionsbuilding-github-actions

解决方案


我认为你有一个错误:

MOCK_USERNAME: ${{ secrets.API_KEY }}

应该是这样的:

API_KEY: ${{ secrets.API_KEY }}


推荐阅读