使用sdkmanager一键安装安卓编译环境

install-android-build-environment

这个故事是这样开始的,在一个阳光明媚的下午,我被叫到了昏暗的会议室持续交付系统对接安卓和IOS发布的需求,为了尽量适应所有平台的编译,于是我就又一次开始了各种环境的部署之旅。
遇事不决问谷歌,实在不行上Stack Overflow。可是官网已经没有搜索到的很多教程里提供的sdk地址,反而要求我下载Android Studio。我一台编译机装什么图形界面啊喂!于是就是开始了sdkmanager的踩坑之旅。

我写脚本的水平还是比较菜的,所以为了不丢人现眼,自己写的一键安装脚本放在了文末,CentOS7测试通过,需要的可以直接拉到底部。

概述

本文共分为三个主要部分,分别为安装仍然免费的JDK、安装sdkmanager以及sdk、ndk的安装。
文中使用的环境为:

* CentOS Linux release 7.5.1804 (Core)
* jdk-8u191-linux-x64
* sdk-tools-linux-4333796

然后一如既往,提供官方网站的地址:
JDK 8 官方网站:Java SE Development Kit 8 Downloads
安卓开发者官方网站:Android Studio

安装JDK

我们都知道,Oracle爸爸现在开始到处发“威胁邮件”收取使用费了,于是我们需要使用一个Oracle爸爸说还能免费使用的版本,据公司领导们决策,我们使用Java 8最后一个免费版本,即jdk-8u191。然而抠门的Oracle爸爸已经让我们拼了老命还要注册账号才能下载到这个版本,于是我花了几个小时刷了一遍他的下载路径,找到了直接可以下载的方法,本文中暂时不表,下次单独说。
事实上,安装jdk非常简单,仅需执行以下命令即可:

1
2
3
4
5
echo "install jdk 8u191"
wget -O /tmp/jdk-8u191-linux-x64.tar.gz http://javadl.oracle.com/webapps/download/AutoDL?BundleId=235704_2787e4a523244c269598db4e85c51e0c
tar -xvf /tmp/jdk-8u191-linux-x64.tar.gz -C /vdb/
echo 'export JAVA_HOME=/vdb/jdk1.8.0_191' >> /etc/profile
source /etc/profile

嗯,你们没猜错,最后的脚本就是把这些一段段拼起来。

安装sdkmanager

接下来就是安装这个很坑的东西了,一开始sdkmanager是安装sdk时附带的一个工具,但是后来我直接安装sdk就变成了 deprecated。行吧,那我就装上sdkmanager先:

1
2
3
4
5
6
7
echo "install sdkmanager"
wget -O /tmp/sdk-tools-linux-4333796.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
mkdir -p /vdb/android-sdk-linux
unzip /tmp/sdk-tools-linux-4333796.zip -d /vdb/android-sdk-linux/
echo 'export ANDROID_HOME=/vdb/android-sdk-linux' >> /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$PATH' >> /etc/profile
source /etc/profile

然后使用方法也很简单:

1
sdkmanager "{需要安装包的名字;版本}"

安装安卓sdk和ndk

很神奇的是,sdkmanager不能安装ndk,emmmm,可能因为不叫 sndkmanager ???

安装sdk

因为发布可能会涉及到各种版本,所以我是要安装所有的sdk以及相关其他依赖包,于是我安装时就写成了这样…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
echo "install sdk"
echo "you must manually input yes when you see license"
read -p "--- Press any key to continue ---"
sdkmanager \
"add-ons;addon-google_apis-google-15" \
"add-ons;addon-google_apis-google-16" \
"add-ons;addon-google_apis-google-17" \
"add-ons;addon-google_apis-google-18" \
"add-ons;addon-google_apis-google-19" \
"add-ons;addon-google_apis-google-21" \
"add-ons;addon-google_apis-google-22" \
"add-ons;addon-google_apis-google-23" \
"add-ons;addon-google_apis-google-24" \
"add-ons;addon-google_gdk-google-19" \
"build-tools;19.1.0" \
"build-tools;20.0.0" \
"build-tools;21.1.2" \
"build-tools;22.0.1" \
"build-tools;23.0.1" \
"build-tools;23.0.2" \
"build-tools;23.0.3" \
"build-tools;24.0.0" \
"build-tools;24.0.1" \
"build-tools;24.0.2" \
"build-tools;24.0.3" \
"build-tools;25.0.0" \
"build-tools;25.0.1" \
"build-tools;25.0.2" \
"build-tools;25.0.3" \
"build-tools;26.0.0" \
"build-tools;26.0.1" \
"build-tools;26.0.2" \
"build-tools;26.0.3" \
"build-tools;27.0.0" \
"build-tools;27.0.1" \
"build-tools;27.0.2" \
"build-tools;27.0.3" \
"build-tools;28.0.0" \
"build-tools;28.0.0-rc1" \
"build-tools;28.0.0-rc2" \
"build-tools;28.0.1" \
"build-tools;28.0.2" \
"build-tools;28.0.3" \
"build-tools;29.0.0-rc1" \
"cmake;3.10.2.4988404" \
"cmake;3.6.4111459" \
"extras;android;gapid;1" \
"extras;android;gapid;3" \
"extras;android;m2repository" \
"extras;google;auto" \
"extras;google;google_play_services" \
"extras;google;instantapps" \
"extras;google;m2repository" \
"extras;google;market_apk_expansion" \
"extras;google;market_licensing" \
"extras;google;simulators" \
"extras;google;webdriver" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha2" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha3" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha4" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha5" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha6" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha7" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha8" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha9" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta1" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta2" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta3" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta4" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta5" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.1" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha3" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha4" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha5" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha6" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha7" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha8" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha9" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta1" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta3" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta4" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta5" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.1" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2" \
"lldb;2.0" \
"lldb;2.1" \
"lldb;2.2" \
"lldb;2.3" \
"lldb;3.0" \
"lldb;3.1" \
"ndk-bundle" \
"patcher;v4" \
"platform-tools" \
"platforms;android-10" \
"platforms;android-11" \
"platforms;android-12" \
"platforms;android-13" \
"platforms;android-14" \
"platforms;android-15" \
"platforms;android-16" \
"platforms;android-17" \
"platforms;android-18" \
"platforms;android-19" \
"platforms;android-20" \
"platforms;android-21" \
"platforms;android-22" \
"platforms;android-23" \
"platforms;android-24" \
"platforms;android-25" \
"platforms;android-26" \
"platforms;android-27" \
"platforms;android-28" \
"platforms;android-7" \
"platforms;android-8" \
"platforms;android-9" \
"platforms;android-Q" \
"tools"

请注意,这部分的安装需要手动确认License,大家可不要不小心错过了,否则会无法安装哦!

安装ndk

首先提供ndk的地址:
ndk官方网站

我们可以在上面看到的很多的版本,请根据需要选择,我们需要的是ndk13,下文中也是下载安装的13哦!
另外,请注意,貌似环境变量中同时只能设置一个ndk版本,所以请自行做好版本管理!

我们使用以下命令下载并安装ndk-r13b:

1
2
3
4
5
6
7
echo "install ndk"
wget -O /tmp/android-ndk-r13b-linux-x86_64.zip https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip
mkdir -p /vdb/android-ndk-linux
unzip /tmp/android-ndk-r13b-linux-x86_64.zip -d /vdb/android-ndk-linux/
echo 'export ANDROID_NDK_HOME=/usr/local/android-ndk-linux/android-ndk-r13b' >> /etc/profile
echo 'export PATH=$ANDROID_NDK_HOME:$PATH' >> /etc/profile
source /etc/profile

完整shell文件

主要的安装命令在上文中基本都写了,完整版的shell文件中添加了清理安装包的命令,不需要可以自行删除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash

echo "init android build environment"

yum update -y
yum install -y wget unzip

echo "install jdk 8u191"
wget -O /tmp/jdk-8u191-linux-x64.tar.gz http://javadl.oracle.com/webapps/download/AutoDL?BundleId=235704_2787e4a523244c269598db4e85c51e0c
tar -xvf /tmp/jdk-8u191-linux-x64.tar.gz -C /vdb/
echo 'export JAVA_HOME=/vdb/jdk1.8.0_191' >> /etc/profile

echo "install sdkmanager"
wget -O /tmp/sdk-tools-linux-4333796.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
mkdir -p /vdb/android-sdk-linux
unzip /tmp/sdk-tools-linux-4333796.zip -d /vdb/android-sdk-linux/
echo 'export ANDROID_HOME=/vdb/android-sdk-linux' >> /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$PATH' >> /etc/profile
source /etc/profile

echo "install sdk"
echo "you must manually input yes when you see license"
read -p "--- Press any key to continue ---"
sdkmanager \
"add-ons;addon-google_apis-google-15" \
"add-ons;addon-google_apis-google-16" \
"add-ons;addon-google_apis-google-17" \
"add-ons;addon-google_apis-google-18" \
"add-ons;addon-google_apis-google-19" \
"add-ons;addon-google_apis-google-21" \
"add-ons;addon-google_apis-google-22" \
"add-ons;addon-google_apis-google-23" \
"add-ons;addon-google_apis-google-24" \
"add-ons;addon-google_gdk-google-19" \
"build-tools;19.1.0" \
"build-tools;20.0.0" \
"build-tools;21.1.2" \
"build-tools;22.0.1" \
"build-tools;23.0.1" \
"build-tools;23.0.2" \
"build-tools;23.0.3" \
"build-tools;24.0.0" \
"build-tools;24.0.1" \
"build-tools;24.0.2" \
"build-tools;24.0.3" \
"build-tools;25.0.0" \
"build-tools;25.0.1" \
"build-tools;25.0.2" \
"build-tools;25.0.3" \
"build-tools;26.0.0" \
"build-tools;26.0.1" \
"build-tools;26.0.2" \
"build-tools;26.0.3" \
"build-tools;27.0.0" \
"build-tools;27.0.1" \
"build-tools;27.0.2" \
"build-tools;27.0.3" \
"build-tools;28.0.0" \
"build-tools;28.0.0-rc1" \
"build-tools;28.0.0-rc2" \
"build-tools;28.0.1" \
"build-tools;28.0.2" \
"build-tools;28.0.3" \
"build-tools;29.0.0-rc1" \
"cmake;3.10.2.4988404" \
"cmake;3.6.4111459" \
"extras;android;gapid;1" \
"extras;android;gapid;3" \
"extras;android;m2repository" \
"extras;google;auto" \
"extras;google;google_play_services" \
"extras;google;instantapps" \
"extras;google;m2repository" \
"extras;google;market_apk_expansion" \
"extras;google;market_licensing" \
"extras;google;simulators" \
"extras;google;webdriver" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha2" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha3" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha4" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha5" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha6" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha7" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha8" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha9" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta1" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta2" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta3" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta4" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta5" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.1" \
"extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha3" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha4" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha5" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha6" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha7" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha8" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha9" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta1" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta2" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta3" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta4" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta5" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.1" \
"extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2" \
"lldb;2.0" \
"lldb;2.1" \
"lldb;2.2" \
"lldb;2.3" \
"lldb;3.0" \
"lldb;3.1" \
"ndk-bundle" \
"patcher;v4" \
"platform-tools" \
"platforms;android-10" \
"platforms;android-11" \
"platforms;android-12" \
"platforms;android-13" \
"platforms;android-14" \
"platforms;android-15" \
"platforms;android-16" \
"platforms;android-17" \
"platforms;android-18" \
"platforms;android-19" \
"platforms;android-20" \
"platforms;android-21" \
"platforms;android-22" \
"platforms;android-23" \
"platforms;android-24" \
"platforms;android-25" \
"platforms;android-26" \
"platforms;android-27" \
"platforms;android-28" \
"platforms;android-7" \
"platforms;android-8" \
"platforms;android-9" \
"platforms;android-Q" \
"tools"

echo "install ndk"
wget -O /tmp/android-ndk-r13b-linux-x86_64.zip https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip
mkdir -p /vdb/android-ndk-linux
unzip /tmp/android-ndk-r13b-linux-x86_64.zip -d /vdb/android-ndk-linux/
echo 'export ANDROID_NDK_HOME=/usr/local/android-ndk-linux/android-ndk-r13b' >> /etc/profile
echo 'export PATH=$ANDROID_NDK_HOME:$PATH' >> /etc/profile
source /etc/profile

echo "clean up"
rm -rf /tmp/*

echo "done!"
read -p "--- Press any key to continue ---"

总结

首先为自己好久没有写文感到愧疚一次;
然后为自己写了这样一篇笔记版文字感到愧疚一次;
希望以后能够赶紧把draft里面的文章都写完整,也希望赶紧把已经过时的文章更新一下。
最后,还是感谢大家的关注和评论~
撒花~✿✿ヽ(°▽°)ノ✿~

坚持原创!欢迎各位客官给我打赏买小饼干吖!✿✿ヽ(°▽°)ノ✿
0%