GItlab CI :- 如何使用 Gitlab-CI 在 Android 中创建多个 apk(如开发、登台和生产)? [英] GItlab CI :- How to create the multiple apk(like development, staging and production) in Android using the Gitlab-CI?

查看:47
本文介绍了GItlab CI :- 如何使用 Gitlab-CI 在 Android 中创建多个 apk(如开发、登台和生产)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过 Gitlab 中的以下方法使用 Gitlab-CI 创建像 debug.apk 这样的单一构建(apk).

在我的 .gitlab-ci.yml 里面,我已经完成了这个条目.请检查一次,

图像:jangrewe/gitlab-ci-android阶段:- 建造before_script:- 导出 GRADLE_USER_HOME=$(pwd)/.gradle- chmod +x ./gradlew缓存:键:${CI_PROJECT_ID}路径:- .gradle/建造:阶段:构建标签:-dev-ci脚本:- ./gradlew assembleDebug文物:路径:- 应用程序/构建/输出/

我已经创建了 docker 映像,并在 Gitlab 上每次推送时都获得了构建(apk).

我的问题是我们可以设置不同的阶段,如 DevelopmentStagingProduction 指出应用程序的不同BASE_URL.我也在文档中搜索过,但没有得到解决方案.请帮助我.谢谢

解决方案

@mles 解决方案有效,但它依赖于设置环境变量.这对于 CI 用例非常有用,但如果您想在本地快速构建开发/暂存/发布版本,则应该改用构建类型或风格.查看

更改 CI 工作

在您的 CI 中,使用不同的配置调用 assemble 来构建不同的版本:

  • ./gradlew assembleDebug
  • ./gradlew assembleStaging
  • ./gradlew assembleRelease

您示例中的最终 gitlab-ci 配置应如下所示:

图像:jangrewe/gitlab-ci-android阶段:- 建造before_script:- 导出 GRADLE_USER_HOME=$(pwd)/.gradle- chmod +x ./gradlew缓存:键:${CI_PROJECT_ID}路径:- .gradle/建造:阶段:构建标签:-dev-ci脚本:- ./gradlew assembleDebug assembleStaging assembleRelease文物:路径:- 应用程序/构建/输出/

I am able to create the single build(apk) like debug.apk using the Gitlab-CI by below approach in Gitlab.

Inside my .gitlab-ci.yml , I have done this entry.Please check it once,

image: jangrewe/gitlab-ci-android

stages:
  - build

before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew

cache:
  key: ${CI_PROJECT_ID}
  paths:
    - .gradle/

build:
  stage: build
  tags:
    - dev-ci
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
      - app/build/outputs/

And I have created the docker image and got the build (apk) on each push on the Gitlab.

My question is that can we set the different stages like Development , Staging and Production which point out different BASE_URL of the application. I have also searched out in the documents but did not get the solution.Please help me on it. Thanks

解决方案

@mles Solution works but it relies on setting an environment variable. This is great for the CI usecase but if you also want to quickly build a dev/staging/release version locally, you should use build types or flavors instead. Check out the Configure build variants guide for further information.

The following example shows how to achieve this with build types, but using flavors is also very similar.

Configuring different API base URLs using gradle build types and source sets

Add an additional build type

The default gradle configuration should already contain a debug and release build type. Add an additional staging build type by adding this lines in your app module's build.gradle file inside the buildTypes:

staging {
    initWith debug
}

Your build.gradle should then look like this:

android {
    ...

    defaultConfig {
        ...
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        staging {
            initWith debug
        }
    }
}

dependencies {
    ...
}

Execute a gradle sync afterwards (by pressing the "Sync Project with Gradle" button with the Elephant logo at the top).

Add additional source sets

You can tell gradle to package specific code and resources for specific build configurations. In our example, we will create an ExampleConfig file which contains the base URl, for each build type.

object ExampleConfig {
    const val BASE_ULR = "http://example.com/"
}

In your code, you can just reference this file to access the base URL. Depending on the selected build type, gradle will then automatically use the correct version of the file.

For this, add the following folders inside your modules src folder (for example inside appsrc):

  • debug/java
  • staging/java
  • release/java

There, create the ExampleConfig class / object which contains a baseUrl String with different values. The result should look like this:

It should look like this:

Changing the CI job

In your CI, build the different versions by calling assemble with different configurations:

  • ./gradlew assembleDebug
  • ./gradlew assembleStaging
  • ./gradlew assembleRelease

The final gitlab-ci configuration in your example should look like this:

image: jangrewe/gitlab-ci-android

stages:
  - build

before_script:
  - export GRADLE_USER_HOME=$(pwd)/.gradle
  - chmod +x ./gradlew

cache:
  key: ${CI_PROJECT_ID}
  paths:
    - .gradle/

build:
  stage: build
  tags:
    - dev-ci
  script:
    - ./gradlew assembleDebug assembleStaging assembleRelease
  artifacts:
    paths:
      - app/build/outputs/

这篇关于GItlab CI :- 如何使用 Gitlab-CI 在 Android 中创建多个 apk(如开发、登台和生产)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆