摸鱼学Android 一 (环境)
1 安装开发环境
- 安装IDEA过程略过
- IDEA安装Android组件,打开IDEA,欢迎界面右下角Configure->Setting,搜索Android,SDK路径选择一个空目录,等待安装(我这里安装了Android API 30)
PS:若安装组件过程出现某些包下载超时
访问http://ping.chinaz.com/输入dl.google.com进行检测
选择一个延迟最低的
在本机上ping,若能ping通,用记事本打开hosts文件,写入203.208.50.161 dl.google.com
(文件路径:C:\Windows\System32\drivers\etc\hosts)
保存退出,组件安装页面点击Retry
-
组件安装完成后,回到欢迎页面,Configure->Project Defaults->Project Structure-> SDKs,选择添加
-
接下来就可以创建Android工程了
2 配置构建工具
能fq的话,可以选择IDEA默认方式安装(自动)
这里介绍下载压缩包的方式
压缩包资源
https://pan.baidu.com/s/1Qvcs8gvJCW-PgBAsNixfxQ 提取码:5u6n
配置步骤
- 下载压缩包后,解压到自定义目录
- windows配置环境变量,新增环境变量 GRADLE_HOME,路径为解压的路径,之后将 %GRADLE_HOME%\bin; 添加到Path
- cmd运行 gradle -v,有信息则配置成功
- IDEA菜单栏File->Settings,搜索gradle,选择你的解压路径,OK
- 后续构建工程,自动下载jar包
3 构建过程报错
1)下载过程中卡在 jcenter.bintray.com/
使用开源中国的maven库:打开工程根目录的build.gradle文件,分别在buildscript和allprojects下的repositories添加一行 maven{url ‘http://maven.aliyun.com/nexus/content/groups/public/’}
repositories {
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
}
2)版本不匹配
打开app目录下build.gradle文件,将dependencies的 ** com.android.support:appcompat-v7:30+** 改为 com.android.support:appcompat-v7:+
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:+'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
3)布局预览报错
打开app/main/res/values/styles.xml,修改parent值
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
4 大功告成,Hello World!
5 连接模拟器
- IDEA调试工程,提醒要设置安卓虚拟设备,前往设置,根据提示,下载系统镜像和HAXM(硬件辅助虚拟化引擎)
- 安装完成后,运行