在 Android Studio 中调试时出现证书验证路径错误

Posted

技术标签:

【中文标题】在 Android Studio 中调试时出现证书验证路径错误【英文标题】:Cert validation path error when debugging in Android Studio 【发布时间】:2020-04-15 12:39:50 【问题描述】:

我刚刚从https://flutter.dev/docs/get-started/codelab 生成了示例应用

当我在 android Studio 中点击调试按钮时,我收到以下错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:316)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:310)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1639)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:223)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1037)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:965)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1064)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1564)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
at org.gradle.wrapper.Download.downloadInternal(Download.java:58)
at org.gradle.wrapper.Download.download(Download.java:44)
at org.gradle.wrapper.Install$1.call(Install.java:61)
at org.gradle.wrapper.Install$1.call(Install.java:48)
at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:65)
at org.gradle.wrapper.Install.createDist(Install.java:48)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)
... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)
... 26 more
Finished with error: Gradle task assembleDebug failed with exit code 1

我在企业代理后面,并且已经将其证书添加到 cacerts 密钥库,但我仍然收到错误。

关于如何解决这个问题的任何想法?

【问题讨论】:

我遇到了同样的错误。你有这个解决方案吗? 【参考方案1】:

我发现的唯一解决方法是将 build.gradle 中的 jcenter() 更改为 ma​​venCentral()

repositories 
    mavenCentral()
    google()

【讨论】:

【参考方案2】:

首先你需要知道,Android Studio 有自己的 jre。

所以你需要给它添加证书。

    找到您的 android studio 路径和 jre/lib/security/cacerts。

    在我的情况下(Windows):C:\Program Files\Android\Android Studio\jre\jre\lib\security

    添加证书

    keytool -import -alias mycertificate -keystore path_to_keystore -file android studio certificate_file

【讨论】:

【参考方案3】:

我在构建 Flutter 示例应用时遇到了同样的错误。

这是一个 gradle 同步问题。只需尝试从工作室设置中使缓存无效并更新 gradlewrraper.properties 文件中的分发 url 并重新同步。

【讨论】:

以上是关于在 Android Studio 中调试时出现证书验证路径错误的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 3.2 上的 AndroidX - 运行应用程序时出现验证错误

在 android studio 虚拟设备中调试时出错

在 Visual Studio 2010 中调试时出现致命错误 HRESULT=0x80131c08

使用 charles web 调试代理时出现“证书文件不存在”错误

Android Studio 在调试器中找不到局部变量

在 android Nougat 中使用自签名证书通过 https 连接时出现 SSL 握手异常