【Andorid学习1——基于Android Studio的第一个App程序】

一、搭建安卓环境:Android Studio

方法一:

1、下载和安装JetBrains 管理工具“JetBrains TOOLBOXjetbrains-toolbox-1.21.9547.exe
2、安装Android Studio: 打开JetBrains TOOLBOX,找到“Android Studio”,点击install,即可安装成功

方法二:

参考该教程安装Android Studio 安装 | 菜鸟教程

二、创建第一个 Android App

1、新建工程

新建工程:打开软件,在菜单中选择file-》new project,根据需要选择

2、执行工具按钮“Sync Project with Gradle Files”(下载依赖包,更新Android Studio项目模型以匹配Gradle构建文件的内容)

(1)报错1:执行工具按钮“Sync Project with Gradle Files”,报错截图:

“Cause: unable to find valid certification path to requested target ” 

“Could not resolve com.android.tools.build:gradle:7.0.4”

   解决办法:在文件“build.gradle”中添加如下代码

参考链接:

https://www.jb51.net/article/182527.htm

Could not resolve com.android.tools.build:gradle:7.0.2_autumn.ㄙ的博客-CSDN博客_com.android.tools.build:gradle

(2)报错2:再次执行工具按钮“Sync Project with Gradle Files”,报错截图的错误“gradle高版本会出现maven仓库地址不是https安全连接便会报错的问题”

Could not resolve all dependencies for configuration ':app:debugRuntimeClasspath'.
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven2(http://mvn.xxxxxx.com/content/groups/public/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details. 

 解决办法:添加代码  allowInsecureProtocol = true

maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/repositories/google'

}
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'

}

解决参考连接:使用 allowInsecureProtocol 属性解决 gradle 的仓库地址HTTPS警告_何33512336的博客-CSDN博客

(3)最后再执行工具按钮“Sync Project with Gradle Files”,构建 成功

3、运行程序,执行工具按钮“Run app”,运行后如下:

AndroidStudio本地编译apk仍有错——unable to find valid certification path to requested target

问题原因:对Google、GitHub等国外网站使用了私有CA(adc-CA-CA)颁发的SSL证书,不在受信任的范围内,因此Windows下多种开发工具在拉取上述来源的内容时会出现证书相关问题:
解决办法:导出adc-CA-CA根证书给Android Studio使用,并设置Android Studio信任该根证书。
(1)打开cmd窗口,输入certmgr.msc,打开Windows证书管理器  ;
点击菜单栏“操作”,选择“查找证书” ;
在“查找证书”对话框输入“adc-CA-CA”并点击“立即查找”,在结果中找到一个“受信任的根证书颁发机构”且未过期的证书;
右键该证书,选择“导出”,一步步按默认操作,最后选择保存的路径和文件名adc.cer如:D:\Users\Desktop\adc.cer。

(2)打开cmd窗口,输入命令(你安装Android Studio的实际路径):
"C:\Program Files\Android\Android Studio\jre\bin\keytool.exe" -import -alias adc -keystore "C:\Program Files\Android\Android Studio\jre\lib\security\cacerts" -file D:\Users\Desktop\adc.cer
回车后提示输入密钥库口令时,输入默认值changeit,并输入“是”确认。注意,一定要输入中文的“是”,然后回车。

 (3)打开Android Studio,进入以下页面勾选“Accept non-trusted certificates automatically”,然后重启软件即可在本地编译apk

具体命令:File--Settings--Tools--Server Certificates 勾选“Accecpt non-trusted certificates automatically”

最终,点击运行按钮,运行成功,不再有报错。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值