android 新手, hello world 无法编译。。

353 天前
 chopin1998519

操作系统是 Linux

android studio 从空模板创建工程后, 均无法编译

显示 Gradle project sync failed. Basic functionality (e.g. editing, debugging) will not work properly

搜索过很多帖子, 装过 Android Studio Flamingo 和电摇, 清过 cache, 重新试过多个版本的 gradle , 还是一样。

还可能是什么错误呢?

9198 次点击
所在节点    Android
67 条回复
taken
353 天前
代理没配置好 baby
ExplodingFKL
353 天前
@chopin1998519 你把所有代理相关的配置清除或者在执行命令的时候加上 --no-daemon 参数再看看详细结果
mineralsalt
353 天前
网络问题, 鉴定完毕
skyinu
353 天前
可以用阿里云的 maven 仓库试一下,可以规避网络问题
BigMikey
353 天前
网络问题,我是全局的,所以没毛病,系统是 MacOS ,/Doge
iovekkk
353 天前
在~/.gradle 目录下创建 init.gradle 文件
文件内容如下:
allprojects {
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url "https://maven.aliyun.com/repository/gradle-plugin/"}
}
}

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url "https://maven.aliyun.com/repository/gradle-plugin/"}
}

println "${it.name}: Aliyun maven mirror injected"
}
创建完毕之后,重启 Android studio 即可
kaedea
353 天前
网络问题,不仅要开 HTTP 代理,还要开 socket 代理。
ExplodingFKL
353 天前
阿里云是个大坑 ... 而且在没有其他签名校验的情况下可能出现供应链攻击,啥时候 maven 每个文件带 gpg 签名就好了
superedlimited
353 天前
@chopin1998519 悄悄问一下,你的 proxy 是在这里设置的么?
chopin1998519
353 天前
@iovekkk 试过了, 还是一样的错误。。
chopin1998519
353 天前
@superedlimited 是的,

我也直接在 terminal 里用 http_proxy/ https_proxy 环境变量试过

这样设置好后,gradle 还会问我要不要把这个配置写在它自己的全局配置文件里(~/.gradle/)
chopin1998519
353 天前
@kaedea 什么意思? 我的是 clash, socks 和 http 混合端口, 都能用的
superedlimited
353 天前
@superedlimited btw ,eel 是鳗鱼,鳐是带翅膀的魔鬼鱼
superedlimited
353 天前
@chopin1998519 A new daemon was started but could not be connected to: pid=DaemonInfo{pid=261152, address=[0c0205be-28f5-4060-bc75-b4f2fefe660c port:43841, addresses:[/127.0.0.1]], state=Busy, lastBusy=1685421977961, context=DefaultDaemonContext[uid=bd402a2c-565c-485a-845d-febf686b1655

你点一下下面那个 check connection 按钮试一试
iovekkk
353 天前
@chopin1998519 我这种方法不需要配置代理,设置代理反而可能会有问题
chopin1998519
353 天前
@iovekkk 我知道, 用阿里云关闭代理了, 还是一样。。。哭死
chopin1998519
353 天前
chopin1998519
353 天前
[img][/img]
superedlimited
353 天前
你的网络设置,有设置 nat masquerade 吗?

We have observed this can occur when network address translation (NAT) masquerade is used. When NAT masquerade is enabled, connections that should be considered local to the machine are masked to appear from external IP addresses. Gradle refuses to connect to any external IP address as a security precaution.

The solution to this problem is to adjust your network configuration such that local connections are not modified to appear as from external addresses.

You can monitor the detected network setup and the connection requests in the daemon log file ($GRADLE_USER_HOME/daemon/<Gradle version>/daemon-<PID>.out.log).


@chopin1998519
CharmingCheung
353 天前
看日志像是 Gradle daemon 进程出问题。问 ChatGPT:

检查 Gradle 版本:确保你使用的 Gradle 版本与 Android Studio 和你的项目配置是兼容的。如果不确定,你可以尝试升级到最新版本。

增加内存分配:Gradle 守护进程可能需要更多的内存。你可以尝试在 gradle.properties 文件中增加分配给 Gradle 的内存,例如:org.gradle.jvmargs=-Xmx2048m 分配 2GB 内存给 Gradle 。

停止现有的 Gradle 守护进程:可能是已有的 Gradle 守护进程导致了问题。你可以试试运行./gradlew --stop 命令,以停止所有正在运行的 Gradle 守护进程,然后再次构建你的项目。

检查网络连接:你的错误日志中提到了一个关于网络连接的 URL 。如果你在一个有网络限制的环境下(如公司网络或者校园网),可能需要检查你的网络连接或者代理设置。

如果这些解决方案都没有解决你的问题,你可以尝试根据错误日志提示的运行--scan 命令(例如./gradlew build --scan )来获取更详细的错误信息,这可能会帮助你更准确地找到问题所在。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/944131

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX