[英]GItlab CI :- How to create the multiple apk(like development, staging and production) in Android using the Gitlab-CI?
我能夠創建一個版本(APK),如debug.apk
通過下面的方法在Gitlab使用Gitlab-CI。
在我的.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/
build:
stage: build
tags:
- dev-ci
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/
我已經創建了docker
鏡像,並在每次推送 Gitlab 時獲得了構建(apk)。
我的問題是我們可以設置不同的階段,如Development
、 Staging
和Production
,它們指出應用程序的不同BASE_URL
。 我也在文檔中搜索過,但沒有得到解決方案。請幫助我。 謝謝
在.gitlab-ci.yml
設置BASE_URL
:
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"
您必須在 gradle 文件中創建 Android Flavors development
、 staging
、 production
,並使它們使用BASE_URL
環境變量。
或者,就像Alexander Hoffmann所建議的那樣,在您的 gradle 文件中執行所有這些操作,而無需在.gitlab-ci.yml
設置變量。
@mles 解決方案有效,但它依賴於設置環境變量。 這是偉大的CI用例,但如果你也想快速構建一個在本地開發/分期/發行版,你應該使用構建類型或香料代替。 查看配置構建變體指南以獲取更多信息。
下面的示例展示了如何使用構建類型來實現這一點,但使用風格也非常相似。
默認 gradle 配置應該已經包含debug
和release
構建類型。 添加額外的staging
通過添加在你的應用模塊的該行構建類型build.gradle
內部文件buildTypes
:
staging {
initWith debug
}
你的 build.gradle 應該是這樣的:
android {
...
defaultConfig {
...
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
staging {
initWith debug
}
}
}
dependencies {
...
}
之后執行 gradle 同步(通過按下頂部帶有 Elephant 標志的“Sync Project with Gradle”按鈕)。
您可以告訴 gradle 為特定的構建配置打包特定的代碼和資源。 在我們的示例中,我們將為每個構建類型創建一個包含基本 URl 的ExampleConfig
文件。
object ExampleConfig {
const val BASE_ULR = "http://example.com/"
}
在您的代碼中,您只需引用此文件即可訪問基本 URL。 根據所選的構建類型,gradle 將自動使用正確版本的文件。
為此,在您的模塊src
文件夾中添加以下文件夾(例如在\\app\\src
):
debug/java
staging/java
release/java
在那里,創建ExampleConfig
類/對象含有baseUrl
字符串與不同的值。 結果應如下所示:
在您的 CI 中,通過使用不同的配置調用 assemble 來構建不同的版本:
./gradlew assembleDebug
./gradlew assembleStaging
./gradlew assembleRelease
您示例中的最終 gitlab-ci 配置應如下所示:
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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.