Mac Android Studio - 打怪升级系列
历经三天断断续续的调试,第一个安卓项目跑成功啦,芜湖!
文章目录
前言
入职前终究还是要踩踩路子,所以于2022.4.10开始了安卓之旅。之前只是在Mac上安装了Android studio(AS),但是从来没有用过,第一次用就遇到了很多困难,将我对安卓的憧憬从天堂打到了负18层地狱,想卸载掉AS,甚至想。。。。但是路是自己选的,跪着也要走完啊,于是开始过关斩将、打怪升级,黄天不负有心人,终于跑通了第一个demo。
所以想着记录一下Android Studio中遇到的一些问题
一、Gradle sync failed: Read timed out

看到网上说要把proxy做修改,选择manual proxy configuration -> HTTP -> 主机名:mirrors.neusoft.edu.cn -> 端口:80。

改了代理之后,这个问题确实不存在了,但是又有了下面的问题。
二、Could not install Gradle distribution from…

于是乎,我把Gradle重新安装一下 brew install gradle。
然后按照网友教程把下图中的 Gradle user home更换成新安装的gradle的位置,并把Use Gradel from 修改为 Specified location,位置为安装的gradle目录下的libexec目录。
这个问题解决了,但是又出现了新的问题,如下。
三、Disable Gradle ‘offline mode’ and sync project

这个问题最让人头疼,搞的时间最久,网上的教程千篇一律都不能用在我这个版本的AS上。
终于在一篇博客上看到,说是Gradle 7.0.2版本要在JDK 11上运行,而我机器上用的是JDK 8,所以又重新装了JDK 11。并且在./bash_profile文件中配置了java环境、gradle环境。并在设置中,把下图中的gradle jdk修改成新安装的JDK 11

本来以为可以万事大吉了,没想到在build的时候,下载文件特别久,当下载到了12min再次出错。
四、下载超时

4.1 配置镜像
网友说要配置一下镜像,还要修改一些配置,于是乎我的bulid.gradle文件修改成下面那样
// Top-level build file where you can add configuration options common to all sub-projects/modules.
allprojects {
repositories {
jcenter()
maven { url 'https://maven.aliyun.com/repository/public/' }
}
}
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
4.2 镜像不安全
结果又报错,这次报错说我用了不安全的maven库,需要用一个参数声明一下,于是乎有了下面的bulid.gradle文件:
allprojects {
repositories {
jcenter()
maven { url 'https://maven.aliyun.com/repository/public/' }
}
}
buildscript {
repositories {
maven { allowInsecureProtocol = true
url 'https://maven.aliyun.com/repository/public/' }
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
4.3 仓库配置文件的优先级

Build was configured to prefer settings repositories over project repositories but repository ‘BintrayJCenter’ was added by build file ‘build.gradle’。
这个错误是说设置中的仓库的优先级大于build.gradle中的仓库,但是我在这个build.gradle中添加了jcenter这个仓库,于是乎把build.gradle中的仓库删掉。
buildscript {
repositories {
maven { allowInsecureProtocol = true
url 'https://maven.aliyun.com/repository/public/' }
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
并在setting.gradle文件中添加了配置,添加之后如下:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
//jcenter() // Warning: this repository is going to shut down soon
//下面三行是新加的
maven {
allowInsecureProtocol = true
url 'https://maven.aliyun.com/repository/public/' }
}
}
rootProject.name = "Demo"
include ':app'
五、其他错误
【错误1】If the artifact you are trying to retrieve can be found in the repository but without metadata in ‘Maven POM’ format, you need to adjust the ‘metadataSources { … }’ of the repository declaration. Required by:…
【 解决1】
阿里云镜像不要使用'http://maven.aliyun.com/nexus/content/groups/public/',
阿里云镜像要使用'https://maven.aliyun.com/repository/public/'
【错误2】不记得了,好像也是gradle下载的问题
【解决2】把gradle-wrapper.properties文件中的distributionUrl修改成安装好的gradle目录,而不是从网上下载。修改后的文件如下:
#Mon Apr 11 20:21:56 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=file:///Users/zhangmenghan/.gradle/wrapper/dists/gradle-7.0.2-bin/857tjihv64xamwrf0h14cai3r/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
【错误3】不记得了,好像是网络问题
【解决3】把proxy代理设置成无。。。。来回绕,快晕了。

终于改完了,起飞🛫️!!!

模拟器:我呢?
在这里先开心一会,还要忙着去调试模拟器呢??????

待续…

本文记录了在Mac上使用Android Studio时遇到的Gradle同步失败、安装Gradle、离线模式禁用、下载超时等问题及解决步骤,包括配置镜像、环境变量等,最终成功运行首个Android项目。

1395

被折叠的 条评论
为什么被折叠?



