Ionic 2是一个基于Angular 2的混合移动应用开发框架。最近由于项目需要,开始接触Ionic 2,但过程并不顺利,尤其是Android开发环境的搭建,官方文档里寥寥数语,“简洁”得不行。反观Ionic 2的隔壁React Native,这部分的文档写得非常清楚,但也有改进之处,比如可以不安装Android Studio,SDK、Gradle的下载和Maven仓库需要切换到国内镜像源...
首先通过终端命令安装JDK:
sudo apt-get install default-jdk
然后通过腾讯Bugly镜像下载SDK Tools,详见使用指南,在SDK Tools中设置好Bugly镜像站点的代理,就可以愉快地下载SDK了。参考React Native的文档,SDK建议勾选:
- tools/
Android SDK Platform-tools - tools/
Android SDK Build-tools23.03 - Android 6.0 (API 23)/
SDK Platform - Android 6.0 (API 23)/
Intel x86 Atom_64 System Image - Android 6.0 (API 23)/
Intel x86 Atom System Image - Android 6.0 (API 23)/
Google APIs Intel x86 Atom_64 System Image - Android 6.0 (API 23)/
Google APIs Intel x86 Atom System Image - Android 6.0 (API 23)/
Google APIs - Extras/
Android Support Repository
接着,设置环境变量,如果tools文件夹在文档/Android路径下,可以添加以下语句到~/.bashrc文件中:
export ANDROID_HOME=${HOME}/文档/Android
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
到此,Android开发环境搭建基本完成。
为了加快ionic build android的速度,还需要优化网络下载。
首先,打开/your-ionic-project/platforms/android/cordova/lib/builders/GradleBuilder.js,将Gradle的下载链接distributionUrl替换为Bugly的:
var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'http://your-bugly-id.repository.bugly.qq.com/gradle/gradle-2.14.1-all.zip';
其中,your-ionic-project为Ionic项目名称,your-bugly-id可以在登录Bugly镜像站点后查看“配置信息”获得。
然后,在Ionic项目中全局搜索mavenCentral(),将Maven的仓库地址指向阿里云的镜像源:
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
}
这样,就可以顺利执行ionic build android命令了。
如果出现以下错误:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'android'.
> You have not accepted the license agreements of the following SDK components:
[Android SDK Platform 25].
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.
可以通过手动创建license文件解决:
mkdir "$ANDROID_HOME/licenses"
echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"
个人技术博客 biebu.xin,原文链接——在Ubuntu上为Ionic 2搭建Android开发环境