前提
最近服务器配置jenkins 安卓自动打包APK,需要安卓环境,网上介绍挺多实际遇到一些问题,解决方法思路差不多,都说有坑但是没有细节
步骤
1.首先下载linux安装压缩包
wget http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar xvzf android-sdk_r24.4.1-linux.tgz
解压后有有一个android-sdk-linux目录内容如下,这是不完整的,tools目录下是缺少东西的
2.补全tools目录的内容 解压,文件目录就一个tools
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip.zip
3.拷贝tools内容到第一步sdk下tools目录
cp -r tools/ /android/android-sdk-linux/
4.设置环境变量ANDROID_HOME
这里主要通过修改两种文件实现/etc/profile或者~/.bashrc ,通过vi编辑添加环境变量,source 修改的文件就行加载永久化【重启依然有效】
在文件末尾追加
vi /etc/profile
export ANDROID_HOME=/data/android-sdk-linux
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
ESC #退出编辑
:wq #保存
source /etc/profile #环境变量生效
#一版系统是自带java环境的 当然有可以自己从oracle官网下载配置环境变量,思路一致,附JDK环境变量配置如下:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_60/
export JRE_HOME=/usr/lib/jvm/jdk1.8.0_60/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH
5.验证android是否按照成功在命令行 输入android,出现如下即成功
安装platform和build-tools
1.android list sdk --all
列出所有的 sdk 资源包括 Sdk buildTools,SDK platform 等等
选中所需呀资源的序号,比如3 是 buildtoolsVersion 28.0.3,47是 platform 8.1.0
android update sdk -u -a -t 3,47(需要资源的序号)... 下载
或者执行命令
#sdkmanager是安卓sdk环境tools/bin下,所以要去这个目录
./sdkmanager --install “platforms;android-28”
#因为配置了环境变量也可以
$ANDROID_HOME/tools/bin/sdkmanager --install “platforms;android-28”
#可以通过sdkmanager --install命令来进行packages组件的安装
sdkmanager --install “platforms;android-26”
sdkmanager --install “build-tools;25.0.3”
sdkmanager --install “extras;google;m2repository”
sdkmanager --install “extras;android;m2repository”
#sdkmanager --help 帮助
#配置网络连接
#Force all connections to use http rather than https.
sdkmanager --update --no_https
#sdk
sdkmanager --list
#报错 Error: Unknown argument --licenses
#该问题是sdkmanager 版本较低,更新sdkmanager :
sdkmanager --update
注意: 目前一般的android project 都会自带 wrapper 目录,里面带有gradle,项目根目录还有脚本,所以一般运行的时候不需要去下载gradle。android studio 配置gradle 插件即可。如果项目中没有gradle,也没有as 则需要下载gradle。步骤如下:
- 下载gradle wget https://services.gradle.org/distributions/gradle-2.12-bin.zip
- 解压 unzip gradle-2.12-bin.zip
- 配置环境变量 GRADLE_HOME=PATH:$GRADLE_HOME/bin
- source /etc/profile 5. 输入gradle 检测是否安装成功
2.授权执行sdkmanager --licenses 通过【推荐】
安装完对应的版本需要进行授权,否则就会出现如下情况(jenkins打包)
| sdkmanager --licenses
参考:https://stackoverflow.com/questions/38096225/automatically-accept-all-sdk-licences
sdkmanager --update,如果报warning: File /Users/***/.android/repositories.cfg could not be loaded.
创建一个就可以,在执行sdkmanager --update,是个警告没什么影响
另一种通android update解决
android update sdk --no-ui --filter build-tools-24.0.0,android-24,extra-android-m2repository
问题
1.在./gradlew assembleRelease 编译到时候,出现
You have not accepted the license agreements of the following SDK components: [Android SDK Build-Tools 24, Android SDK Platform 24]. Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager. Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html
这是因为缺少组件导致的,这里是缺少 platform android-28。该安装安装
2. ./gradlew: Permission denied
没有权限管理员授权即可chmod +x ./gradlew
附:安卓版本表
使用sdkmanager --list命令显示所有已经安装的和未安装的packages,如下:
Installed packages:
Path | Version | Description | Location |
---|---|---|---|
build-tools;24.0.3 | 24.0.3 | Android SDK Build-Tools 24.0.3 | build-tools/24.0.3/ |
build-tools;25.0.3 | 25.0.3 | Android SDK Build-Tools 25.0.3 | build-tools/25.0.3/ |
emulator | 26.1.3 | Android Emulator | emulator/ |
extras;android;m2repository | 47.0.0 | Android Support Repository | extras/android/m2repository/ |
extras;google;m2repository | 57 | Google Repository | extras/google/m2repository/ |
extras;m2reposi…ut-solver;1.0.2 | 1 | Solver for ConstraintLayout 1.0.2 | extras/m2reposi…t-solver/1.0.2/ |
extras;m2reposi…nt-layout;1.0.2 | 1 | ConstraintLayout for Android 1… | extras/m2reposi…t-layout/1.0.2/ |
patcher;v4 | 1 | SDK Patch Applier v4 | patcher/v4/ |
platform-tools | 26.0.0 | Android SDK Platform-Tools 26 | platform-tools/ |
platforms;android-24 | 2 | Android SDK Platform 24 | platforms/android-24/ |
platforms;android-25 | 3 | Android SDK Platform 25 | platforms/android-25/ |
tools | 26.0.2 | Android SDK Tools | tools/ |
Available Packages:
Path | Version | Description |
---|---|---|
add-ons;addon-g…_apis-google-15 | 3 | Google APIs |
add-ons;addon-g…_apis-google-16 | 4 | Google APIs |
add-ons;addon-g…_apis-google-17 | 4 | Google APIs |
add-ons;addon-g…_apis-google-18 | 4 | Google APIs |
add-ons;addon-g…_apis-google-19 | 20 | Google APIs |
add-ons;addon-g…_apis-google-21 | 1 | Google APIs |
add-ons;addon-g…_apis-google-22 | 1 | Google APIs |
add-ons;addon-g…_apis-google-23 | 1 | Google APIs |
add-ons;addon-g…_apis-google-24 | 1 | Google APIs |
add-ons;addon-g…e_gdk-google-19 | 11 | Glass Development Kit Preview |
build-tools;19.1.0 | 19.1.0 | Android SDK Build-Tools 19.1 |
build-tools;20.0.0 | 20.0.0 | Android SDK Build-Tools 20 |
build-tools;21.1.2 | 21.1.2 | Android SDK Build-Tools 21.1.2 |
build-tools;22.0.1 | 22.0.1 | Android SDK Build-Tools 22.0.1 |
build-tools;23.0.1 | 23.0.1 | Android SDK Build-Tools 23.0.1 |
build-tools;23.0.2 | 23.0.2 | Android SDK Build-Tools 23.0.2 |
build-tools;23.0.3 | 23.0.3 | Android SDK Build-Tools 23.0.3 |
build-tools;24.0.0 | 24.0.0 | Android SDK Build-Tools 24 |
build-tools;24.0.1 | 24.0.1 | Android SDK Build-Tools 24.0.1 |
build-tools;24.0.2 | 24.0.2 | Android SDK Build-Tools 24.0.2 |
build-tools;24.0.3 | 24.0.3 | Android SDK Build-Tools 24.0.3 |
build-tools;25.0.0 | 25.0.0 | Android SDK Build-Tools 25 |
build-tools;25.0.1 | 25.0.1 | Android SDK Build-Tools 25.0.1 |
build-tools;25.0.2 | 25.0.2 | Android SDK Build-Tools 25.0.2 |
build-tools;25.0.3 | 25.0.3 | Android SDK Build-Tools 25.0.3 |
build-tools;26.0.0 | 26.0.0 | Android SDK Build-Tools 26 |
build-tools;26.0.1 | 26.0.1 | Android SDK Build-Tools 26.0.1 |
cmake;3.6.4111459 | 3.6.4111459 | CMake 3.6.4111459 |
docs | 1 | Documentation for Android SDK |
emulator | 26.1.3 | Android Emulator |
extras;android;gapid;1 | 1.0.3 | GPU Debugging tools |
extras;android;gapid;3 | 3.1.0 | GPU Debugging tools |
extras;android;m2repository | 47.0.0 | Android Support Repository |
extras;google;auto | 1.1 | Android Auto Desktop Head Unit… |
extras;google;g…e_play_services | 43 | Google Play services |