cordova在打包时出现以下错误:
FAILURE: Build failed with an exception.
1. What went wrong:
Could not resolve all files for configuration ':app:debugCompileClasspath'.
> Could not resolve com.android.support:support-v4:27.+.
Required by:
project :app
> Could not resolve com.android.support:support-v4:27.+.
> Failed to list versions for com.android.support:support-v4.
> Unable to load Maven meta-data from https://maven.google.com/com/android/support/support-v4/maven-metadata.xml.
> Could not HEAD 'https://maven.google.com/com/android/support/support-v4/maven-metadata.xml'.
11 actionable tasks: 11 executed
> Connect to maven.google.com:443 [maven.google.com/172.217.160.78] failed: Connection timed out: connect
2. Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
3. Get more help at https://help.gradle.org
问题:
cordova platform add android 添加的platform里gradle默认的mvn配置是 https://maven.google.com,国内访问有问题。
思路:修改maven使用阿里源。
方案1
在电脑的USER_HOME/.gradle/下创建init.gradle。该方法对所有项目生效。
init.gradle内容如下:
allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://maven.google.com')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url ALIYUN_REPOSITORY_URL
url ALIYUN_JCENTER_URL
}
}
}
方案2
修改platforms/android/build.gradle,将其中maven的url 从 “https://maven.google.com” 修改为 “http://maven.aliyun.com/nexus/content/groups/public”。该方法只对当前项目有效。
注意:
app/build.gradle 、 CordovaLib/build.gradle 中也有引用 maven.google,也需要修改;
建议直接用AS的find in path 搜索 maven.google.com ,逐个进行确认和替换