一、构建环境:gradle5、安卓环境
1⃣️ 登陆服务器,安装gradle5
cd /data/server
wget https://downloads.gradle-dn.com/distributions/gradle-5.6.4-bin.zip
unzip gradle-5.6.4-bin.zip
mv gradle-5.6.4 gradle-5
[root@localhost sdk]# vim /etc/profile
export PATH=$PATH:/data/server/gradle-5/bin
2⃣️ 安卓环境
安装jdk环境:
[root@localhost linux]# ls /data/jdk/
bin COPYRIGHT db include javafx-src.zip jre lib LICENSE man README.html release src.zip THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt
安装下载SDK环境
[root@localhost server]# cd /data/server/
[root@localhost server]# mkdir sdk
[root@localhost server]# cd sdk/
[root@localhost sdk]# wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
--2020-04-27 14:38:17-- http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
正在解析主机 dl.google.com (dl.google.com)... 203.208.40.98, 203.208.40.104, 203.208.40.100
正在连接 dl.google.com (dl.google.com)|203.208.40.98|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:326412652 (311M) [application/x-tar]
正在保存至: “android-sdk_r24.4.1-linux.tgz”
100%[============================================================================================================================================================>] 326,412,652 2.37MB/s 用时 2m 41s
2020-04-27 14:40:57 (1.94 MB/s) - 已保存 “android-sdk_r24.4.1-linux.tgz” [326412652/326412652])
[root@localhost sdk]# tar -xf android-sdk_r24.4.1-linux.tgz
[root@localhost sdk]# ls android-sdk-linux/
add-ons platforms SDK Readme.txt tools
[root@localhost android-sdk-linux]# ./tools/android update sdk —no-ui
Refresh Sources:
Fetching https://dl.google.com/android/repository/addons_list-2.xml
Validate XML
Parse XML
Fetched Add-ons List successfully
Refresh Sources
Validate XML: https://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml
Parse XML: https://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml
Fetching URL: https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
Validate XML: https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
Parse XML: https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
Fetching URL: https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
Validate XML: https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
Parse XML: https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
Fetching URL: https://dl.google.com/android/repository/sys-img/google_apis_playstore/sys-img.xml
Validate XML: https://dl.google.com/android/repository/sys-img/google_apis_playstore/sys-img.xml
Parse XML: https://dl.google.com/android/repository/sys-img/google_apis_playstore/sys-img.xml
-------------------------------
License id: android-sdk-license-e6a904e8
Used by:
- Android SDK Platform-tools, revision 29.0.6
- Documentation for Android SDK, API 24, revision 1
- SDK Platform Android 10, API 29, revision 4
- SDK Platform Android R Preview, revision 2
- SDK Platform Android 9, API 28, revision 6
- SDK Platform Android 8.1.0, API 27, revision 3
- SDK Platform Android 8.0.0, API 26, revision 2
- SDK Platform Android 7.1.1, API 25, revision 3
- SDK Platform Android 7.0, API 24, revision 2
- SDK Platform Android 6.0, API 23, revision 3
- SDK Platform Android 5.1.1, API 22, revision 2
- SDK Platform Android 5.0.1, API 21, revision 2
- SDK Platform Android 4.4W.2, API 20, revision 2
- SDK Platform Android 4.4.2, API 19, revision 4
- SDK Platform Android 4.3.1, API 18, revision 3
- SDK Platform Android 4.2.2, API 17, revision 3
- SDK Platform Android 4.1.2, API 16, revision 5
- SDK Platform Android 4.0.3, API 15, revision 5
- SDK Platform Android 4.0, API 14, revision 4
- SDK Platform Android 3.2, API 13, revision 1
- SDK Platform Android 3.1, API 12, revision 3
- SDK Platform Android 3.0, API 11, revision 2
- SDK Platform Android 2.3.3, API 10, revision 2
- SDK Platform Android 2.3.1, API 9, revision 2
- Google APIs, Android API 19, revision 20
- Google APIs, Android API 18, revision 4
- Google APIs, Android API 17, revision 4
- Google APIs, Android API 16, revision 4
- Google APIs, Android API 15, revision 3
- Android Support Repository, revision 47
- Android Auto Desktop Head Unit emulator, revision 1.1
- Google Play services, revision 49
- Google Play Instant Development SDK, revision 1.9
- Google Repository, revision 58
- Google Play APK Expansion library, revision 1
- Google Play Licensing Library, revision 1
- Android Auto API Simulators, revision 1
- Google Web Driver, revision 2
………
………..
…………
January 16, 2019
Do you accept the license 'android-sdk-license-e6a904e8' [y/n]: y
-------------------------------
License id: android-sdk-preview-license-d099d938
Used by:
- Android SDK Build-tools, revision 30 rc1
-------------------------------
June 2014.
Do you accept the license 'android-sdk-preview-license-d099d938' [y/n]: y
Installing Archives:
Preparing to install archives
Downloading Android SDK Platform-tools, revision 29.0.6
Installing Android SDK Platform-tools, revision 29.0.6
Stopping ADB server failed (code -1).
………….
…………..
Installed Google APIs, Android API 17, revision 499%)
Downloading Google APIs, Android API 16, revision 4
Installing Google APIs, Android API 16, revision 4
Installed Google APIs, Android API 16, revision 499%)
Downloading Google APIs, Android API 15, revision 3
Installing Google APIs, Android API 15, revision 3
Installed Google APIs, Android API 15, revision 399%)
Updated ADB to support the USB devices declared in the SDK add-ons.
Stopping ADB server succeeded.
Starting ADB server succeeded.
Done. 45 packages installed.
[root@localhost sdk]# ls android-sdk-linux/
add-ons build-tools docs extras platforms platform-tools SDK Readme.txt temp tools
tailf -10 /etc/profile
export JAVA_HOME=/data/jdk
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:/data/server/gradle-5/bin
export ANDROID_HOME=/data/server/sdk/android-sdk-linux
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH:$ANDROID_HOME
3⃣️ Jenkins 构建配置
Jenkins 全局配置jdk、gradle:
添加sdk环境变量、远程服务器、添加pipeline构建:
需要做到如下需求:
配置如下:
pipeline {
agent any
tools{
gradle 'gradle-5'
}
options {
buildDiscarder(logRotator(numToKeepStr:'10', daysToKeepStr: '5'))
}
environment{
__git_url = "ssh://[email protected]:22222/xxx/androidofficialapp.git"
// __app_dir = "Android"
__artifact1 = "/data/server/apps.xxx.com/Android/androidapp"
__ssh_config_name1 = "app-124"
__ssh_config_name2 = "app-bak-239"
}
parameters {
choice(name:'IS_JENKINS', choices:'true\nfalse', description:'是否通过jenkins打包')
booleanParam(name: 'PUBLISH', defaultValue: false, description: '勾选自动发布到线上服务器')
choice(name:'TINKER_ENABLE', choices:'false\ntrue', description:'热修复框架的集成开关')
choice(name:'BUILD_TYPE', choices:'Debug\nRelease', description:'构建包类型')
string(name:'VERSION_NAME', defaultValue: '1.0.0', description:'版本')
string(name:'VERSION_CODE', defaultValue: '202004150', description:'版本号')
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH', type: 'BRANCH or Tag',description: '选择打包的分支', sortMode:'DESCENDING_SMART'
}
stages {
stage ('环境准备'){
steps{
cleanWs()
}
}
stage('源码') {
steps {
git branch: "${params.BRANCH}", url: "${__git_url}"
}
}
stage('打包') {
steps {
sh "gradle clean assemble$BUILD_TYPE --stacktrace "
}
}
stage('归档') {
steps {
archiveArtifacts 'app/build/outputs/apk/*/*.apk'
}
}
stage('发布到生产服务器'){
// when { environment name: 'PUBLISH', value: 'true' }
when {
allOf {
environment name: 'PUBLISH', value: 'true'
environment name: 'BUILD_TYPE', value:'Release' }
}
steps{
sshPublisher(
continueOnError: false,
failOnError: true,
publishers:[
sshPublisherDesc(
configName: "${__ssh_config_name1}",
verbose: true,
transfers: [sshTransfer(
execCommand: "ls ${__artifact1} && python3 /data/ci/update/ops_tools/AliyunCdn/aliyuncdn.py https://apps.xxx.com/ Directory",
execTimeout: 720000,
remoteDirectory: "ls -lrt ${__artifact1}",
removePrefix: "app/build/outputs/apk/release/",
sourceFiles: "app/build/outputs/apk/release/app-release.apk")]),
sshPublisherDesc(
configName: "${__ssh_config_name2}",
verbose: true,
transfers: [
sshTransfer(
execCommand: "ls -lrt ${__artifact1}",
execTimeout: 720000,
remoteDirectory: "${__artifact1}",
removePrefix: "app/build/outputs/apk/release/",
sourceFiles: "app/build/outputs/apk/release/app-release.apk"),
]
),
])
}
}
}
}
构建后结果如下: