GItlab CI :- How to create the multiple apk(like development, staging and production) in Android using the Gitlab-CI?

吃可爱长大的小学妹 提交于 2020-11-25 03:58:33

问题


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


回答1:


@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 \app\src):

  • 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/



回答2:


Either set BASE_URL within the .gitlab-ci.yml:

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/

development:
  stage: build
  tags:
    - dev-ci
  script:
    - ./gradlew assembleDevelopment
  artifacts:
    paths:
      - app/build/outputs/
  variables:
    BASE_URL: "https://stackoverflow.com"

staging:
  stage: build
  tags:
    - dev-ci
  script:
    - ./gradlew assembleStaging
  artifacts:
    paths:
      - app/build/outputs/
  variables:
    BASE_URL: "https://yahoo.com"

production:
  stage: build
  tags:
    - dev-ci
  script:
    - ./gradlew assembleProduction
  artifacts:
    paths:
      - app/build/outputs/
  variables:
    BASE_URL: "https://google.com"

You must create the Android Flavours development, staging, production in your gradle file, and make them use the BASE_URL environment variable.

Or, just like Alexander Hoffmann is suggesting, do this all in your gradle file without setting variables in the .gitlab-ci.yml.



来源:https://stackoverflow.com/questions/64641302/gitlab-ci-how-to-create-the-multiple-apklike-development-staging-and-produc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!