Android构建gradle太慢(依赖性解析)

Meh*_*esh 57 android gradle android-studio build.gradle android-gradle-plugin

我已经使用Android Studio(我目前的1.5版)2年了.一切都很好,但是当我下载Canary(2.1 p5)时,一切都出错了.每次我想创建一个新项目或打开一个项目或同步或导入一个新的lib或依赖项时,gradle都需要很长时间才能构建 - 将近20分钟.

我没有做任何事情,我刚刚下载了Canary版本并运行它.

症状:

  1. 它发生在我连接到互联网时
  2. 第一个延迟是Gradle:Resolve dependencies':app:_debugCompile'
  3. ...
  4. 建成25分钟后差不多完成了

注意: 当我断开互联网连接时,gradle将尽快完成


我试图通过以下方式解决这个问题:

  1. 我将gradle更改为脱机工作(它工作但我不想这样,因为我想导入libs或依赖项)
  2. 我已经创建了一个新文件(文件名是gradle.properties),C:\Users\username\.gradle然后将这些行写入其中

    org.gradle.parallel=true
    org.gradle.daemon=true
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我删除了那个版本,然后安装了我的旧版本工作正常但问题仍然存在:(

  4. 禁用/启用防火墙

  5. 禁用/启用AntiVirus(Nod32)

  6. 重新安装Windows操作系统(8.1)

  7. 我已经下载了所有版本(1.0.0,...,1.5.1,2.0.0,2.1)

  8. 我用过代理


系统信息:

  • CPU:Intel Core i5
  • 拉姆:6.00 GB
  • 操作系统:Windows 8.1 64位

的build.gradle(项目:应用程序名称)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

gradle.build(模块:APP)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.test.myapplication"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.3.0'
}
Run Code Online (Sandbox Code Playgroud)

建设后的Gradle报告

Dependencies                  Duration
All dependencies              14m3.43s
:app:_releaseCompile          3m30.96s
:app:_debugCompile            3m30.73s
:app:_debugApk                3m30.69s
:app:_releaseApk              3m30.62s
:classpath                    0.428s
:app:_debugAndroidTestCompile 0.001s
:app:_debugAndroidTestApk     0s
:app:_debugUnitTestApk        0s
:app:_debugUnitTestCompile    0s
:app:_releaseUnitTestApk      0s
:app:_releaseUnitTestCompile  0s
:app:releaseWearApp           0s
:app:wearApp                  0s
Run Code Online (Sandbox Code Playgroud)

安装android studio 2.0稳定版后

  • 下午7:23:47 Gradle sync开始====> 8:13:21 PM Gradle sync已完成

Meh*_*esh 77

问题已经解决了 !

经过2天的搜索,我得到了解决方案,所以我想与所有可能遇到同样问题的人分享.问题是gradle无法连接到某个国家/地区的中心存储库.当您创建新项目或导入时,jcenter()默认情况下您的中心存储库,无论何时您想要构建或同步或添加新的外部依赖项,gradle都将连接到https://bintray.com/但它不能和构建过程要等到连接jcenter(),所以这个过程可能需要很长时间(+30分钟),即使你不能添加新的依赖.

方案:

  1. 确保你有最新的稳定版本(当前2.0.0)
  2. 确保build.gradle(classpath 'com.android.tools.build:gradle:2.0.0')中的gradle版本是2.0.0
  3. 最后一步,最重要的一步是将你的jcenter()更改为mavenCentral()

因此,您可以在3秒内轻松添加新的依赖项和同步项目!

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

玩得开心

  • 但我仍然使用jcenter().我没有问题 (2认同)

Fáb*_*lho 5

您可以优化gradle构建过程...

尝试将gradle置于脱机模式下,以强制构建而无需与外部存储库建立任何连接,如下所示:

在此处输入图片说明 在此处输入图片说明

This helped me a lot, because in my work, all connections are behind a proxy and it increase the build time.

If this is not enough for you, take a time testing the options on the Gradle > Experimental ... Some options may improve you performance:

在此处输入图片说明