本人在使用AndroidStudio过程中经常会遇到报错:unable to find valid certification path to requested target(找不到所请求目标的有效证书路径),后来就连新建hello world程序都会报错,在卸载重装降级都试过仍旧没有解决后去网上寻求解决方法。
在多次尝试之后总结出了以下几条解决方法
解决方法
方法一 从chrome浏览器获取证书
根据提示导入证书,去搜索了一下导入证书到AndroidStudio的步骤,首先找到错误的具体位置,往下找会找到一个或两个网址
将这段网址复制到chrome浏览器中,点击
选择证书
选择详细信息
点击复制到文件
选择保存位置后保存
之后做的就是将cer证书导入到cacerts证书库里
使用cmd进入dos命令行
默认密码是changeit,这里我先前已经添加过了
第一次添加会提示是否信任,这里选择 Y
添加信任后导入成功
这种方法较为简单,也有部分人能成功,但是没能解决我的问题
方法二 更换maven地址
更换maven地址
将原先的
google()
jcenter()
修改为
mavenCentral()
maven {
url 'http://xxx' }
这里的地址为方法一中提到的错误位置的具体访问路径
仍然没有解决我的问题
方法三 手动新建并添加证书
第三种方法较为麻烦,本人也是在网上找了好久才找到,但是成功解决了我的AndroidStudio的问题
首先安装证书
1.新建一个java文件,然后将下面代码复制进去
/*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.*;
import java.security.*;
import java.security.cert.*;
import javax.net.ssl.*;
public class InstallCert {
public static void main(String[] args) throws Exception {
String host = "10.0.31.32"; //输入服务端地址
int port = 443; //输入服务端端口 一般默认443
String p = "changeit"; //一般默认changeit
char[] passphrase = p.toCharArray();
File file = new File("jssecacerts");
if (file.isFile() == false) {
char SEP = File.separatorChar;
File dir = new File(System.getProperty("java.home")