Android build gradle 太慢(依赖解析)

标签 android android-studio gradle android-gradle-plugin build.gradle

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

我什么都没做,我只是下载了 Canary 版本并运行它。

症状:

  1. 当我连接到互联网时发生这种情况
  2. 第一个延迟在 Gradle: Resolve dependencies ':app:_debugCompile'
  3. ...
  4. 经过 25 分钟的 build ,几乎完成了

注意: 当我断开网络连接时,gradle 会尽快完成


我尝试通过以下方式解决此问题:

  1. 我将 gradle 更改为离线工作(它可以工作,但我不想这样,因为我想导入库或依赖项)
  2. 我在 C:\Users\username\.gradle 中创建了一个新文件(文件名为 gradle.properties),然后将这些行写入它

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

  4. 禁用/启用防火墙

  5. 禁用/启用防病毒 (Nod32)

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

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

  8. 我使用了代理


系统信息:

  • CPU:英特尔酷睿 i5
  • 内存:6.00 GB
  • 操作系统:Windows 8.1 64 位

build.gradle(Project:appName)

// 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
}

gradle.build(Module: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'
}

构建后的 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

安装android studio 2.0稳定版后

  • 7:23:47 PM Gradle 同步开始 ====> 晚上 8:13:21 Gradle 同步完成

最佳答案

问题解决了!

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

解决方案:

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

所以你可以在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
}

玩得开心

关于Android build gradle 太慢(依赖解析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529301/

相关文章:

android - Android Studio 未找到 TestCase 类

android - 从 Android Studio 中移除项目

android - 如何在Android Studio终端中运行gradle命令?

gradle - 声明运行测试而无需编译源代码

android - Mockito:不调用传递给方法的 Runnable 参数的 Runnable.run()

android - 无法运行从 Github 导入的项目

android - 在 ListView 中(快速)滚动时,列表项( View )顺序发生意外变化

grails - 从 Grails 3 中的 WAR 打包中排除文件/目录

android - 为什么listview的scrollTo(x,y)方法没有转到listview的末尾

Android - 如何为 RecyclerView 添加虚线/点线分隔线?