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?
问题描述
我可以通过以下方法在Gitlab中使用Gitlab-CI创建单个构建(apk),如 debug.apk
.
在我的 .gitlab-ci.yml
中,我已经完成了此输入.请检查一次,
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/
我已经创建了 docker
图片,并在每次启动Gitlab时得到了构建(apk).
And I have created the docker
image and got the build (apk) on each push on the Gitlab.
我的问题是,我们能否设置不同的阶段,例如 Development
, Staging
和 Production
BASE_URL
.我也已经在文档中搜索过,但是没有找到解决方案.请帮我解决这个问题.谢谢
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解决方案可以工作,但是它依赖于设置环境变量.这对于CI用例非常有用,但是如果您还想要在本地快速构建开发/登台/发布版本,则应改用构建类型或风味.请查看配置构建变体指南以获取更多信息.
@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.
以下示例显示了如何使用构建类型来实现这一点,但是使用flavor也非常相似.
The following example shows how to achieve this with build types, but using flavors is also very similar.
默认gradle配置应该已经包含debug
和release
构建类型.通过在buildTypes
:
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
}
您的build.gradle应该如下所示:
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 {
...
}
此后执行gradle同步(通过按下顶部带有Elephant徽标的与Gradle同步项目"按钮).
Execute a gradle sync afterwards (by pressing the "Sync Project with Gradle" button with the Elephant logo at the top).
您可以告诉gradle为特定的构建配置打包特定的代码和资源.在我们的示例中,我们将为每个构建类型创建一个ExampleConfig
文件,其中包含基本URl.
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/"
}
在您的代码中,您只需引用此文件即可访问基本URL.然后,根据所选的构建类型,gradle将自动使用文件的正确版本.
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.
为此,请在模块src
文件夹内添加以下文件夹(例如,在\app\src
内):
For this, add the following folders inside your modules src
folder (for example inside \app\src
):
-
debug/java
-
staging/java
-
release/java
debug/java
staging/java
release/java
在那里,创建ExampleConfig
类/对象,该类/对象包含具有不同值的baseUrl
字符串.结果应如下所示:
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:
在您的CI中,通过调用具有不同配置的组装来构建不同版本:
In your CI, build the different versions by calling assemble with different configurations:
-
./gradlew assembleDebug
-
./gradlew assembleStaging
-
./gradlew assembleRelease
./gradlew assembleDebug
./gradlew assembleStaging
./gradlew assembleRelease
示例中的最终gitlab-ci配置应如下所示:
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屋!