兩行配置完全解放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
    }
    
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM