每次用Android Studio 新建项目时,默认的依赖库都是google()、jcenter(),在国内速度非常慢,有时候慢得不能忍受。多次百度、谷歌后,终于找到一个办法,贴出来和大家共享:
1.找到Android Studio的安装目录下的 build.gradle.ftl 文件 我的是这个:C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidProject\root\build.gradle.ftl
2.修改文件中的仓库地址,把aliyun的仓库放在第1位,记得要修改2处:
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
}
复制代码
allprojects {
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
}
}
复制代码
修改后的文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
<#macro loadProperties>
<#if useOfflineRepo!false>
Properties properties = new Properties()
properties.load(project.rootProject.file("local.properties").newDataInputStream())
</#if>
</#macro>
<#macro useProperties>
<#if useOfflineRepo!false>
properties.getProperty("offline.repo").split(",").each { repo ->
maven { url repo }
}
</#if>
</#macro>
buildscript {<#if includeKotlinSupport!false>
ext.kotlin_version = '${kotlinVersion}'</#if>
<@loadProperties/>
repositories {
<@useProperties/>
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
<#if includeKotlinEapRepo!false>maven { url '${kotlinEapRepoUrl}' }</#if>
}
dependencies {
classpath 'com.android.tools.build:gradle:${gradlePluginVersion}'
<#if includeKotlinSupport!false>classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"</#if>
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
<@loadProperties/>
repositories {
<@useProperties/>
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
jcenter()
<#if includeKotlinEapRepo!false>maven { url '${kotlinEapRepoUrl}' }</#if>
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
复制代码
3.保存后,重新启动Android Studio
这时新建项目后就默认从Aliyun上下载依赖,新测速度非常快,很节约每次新建项目的时间。