两行配置完全解放gradle编译慢问题

  • Android Studio编译经常出现gradle编译缓慢甚至超时问题,抛开电脑硬件配置不说,主要问题还是国内网络环境的因素影响,可以通过修改项目根目录下的build.gradle文件如下:
buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        //jcenter()
        maven { url "https://jitpack.io" }
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        //jcenter()
        maven { url "https://jitpack.io" }
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
    }
}
  • 但是上面这种方式,每次创建一个新的项目的时候都得修改文件非常的不方便,程序员提倡的能做到有多懒就做到有多懒(CV大法好。。。。。。)

  • 下面介绍一种新的方式:通过修改项目根目录下的build.gradle模版的方式,这样每次创建新的项目自动就添加以上阿里云的maven镜像。

    修改Android Studio安装目录

    盘符:\Android Studio3.5\plugins\android\lib\templates\gradle-projects\NewAndroidProject\root\build.gradle.ftl

    修改为:

    // 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/>
            google()
            //注释了这一行
            //jcenter()
            //添加了这一行
          maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
            <#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/>
            google()
            //注释了这一行
            //jcenter()
            //添加了这一行
          maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
            <#if includeKotlinEapRepo!false>maven { url '${kotlinEapRepoUrl}' }</#if>
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

猜你喜欢

转载自www.cnblogs.com/lspkenney/p/12361903.html