Android build gradle is too slow (Dependency Resolution)
Asked Answered
R

12

65

I've been working with Android Studio (my current version 1.5) for 2 years. Everything was OK, but when I downloaded Canary (2.1 p5), everything went wrong. Every time I want to create a new project or open a project or sync or import a new lib or dependency, gradle is taking too long to build — nearly 20 min.

I did not do anything, I just downloaded the Canary version and ran it.

Symptoms :

  1. It happened when I connected to Internet
  2. The first delay is on Gradle: Resolve dependencies ':app:_debugCompile'
  3. ...
  4. After 25 min building almost done

Note: When I disconnect my Internet, gradle will finish as fast as possible


I tried to fix this by these ways:

  1. I changed the gradle to offline work (it worked but I don't want this way, because I want to import libs or dependencies)
  2. I've created a new file (file name is gradle.properties) in C:\Users\username\.gradle then wrote these lines into it

    org.gradle.parallel=true
    org.gradle.daemon=true
    
  3. I removed that version then installed my old version which worked OK but the problem was still there :(

  4. Disable / Enable firewall

  5. Disable / Enable AntiVirus (Nod32)

  6. Reinstall Windows OS (8.1)

  7. I've downloaded all versions (1.0.0, ..., 1.5.1, 2.0.0, 2.1)

  8. I've used a proxy


System info:

  • CPU: Intel Core i5
  • Ram: 6.00 GB
  • OS: Windows 8.1 64 bit

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 report after building

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

After installing android studio 2.0 stable version

  • 7:23:47 PM Gradle sync started ====> 8:13:21 PM Gradle sync completed
Repent answered 10/4, 2016 at 11:47 Comment(5)
The fully released Android Studio 2.0 is available now, so no need to use a canary version. With that, you should also update your android gradle plugin version to 2.0.0 (not 1.5.0).Corrugate
Did not work for me .Repent
Ok , I got it , but what can I do ? What is the solution to pass this problem?Repent
where is that .gradle file in linux?Randolf
Related: you can also enable offline mode for Gradle in Android Studio: see https://mcmap.net/q/110196/-gradle-dependencies-resolving-suddenly-extremely-slowCheviot
R
87

The problem is solved !

After 2 days searching , I got the solution , so I would like to share with all people who may have the same problem. The problem is gradlecan not connect to center repository in some country. When you create a new project or import , your center repository is jcenter() by default and whenever you want to build or sync or add new external dependency, gradle is going to connect to https://bintray.com/ but it can not and the building process is going to wait till connect to jcenter(), so this process might take long time ( +30 min ) , even you can not add new dependency .

Solution :

  1. Make sure you have latest stable version ( current 2.0.0 )
  2. Make sure your gradle version is 2.0.0 in build.gradle (classpath 'com.android.tools.build:gradle:2.0.0')
  3. Final step and most important one is change your jcenter() to mavenCentral()

So you can easily add new dependency and sync project under 3sec !

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
}

Have Fun

Repent answered 10/4, 2016 at 18:40 Comment(11)
but i still use jcenter(). i have not problemPinochle
In my experience step1 and step2 are not necessary.Flounce
It works...better than it was...now it builds lesser than 30secs.Phosphorylase
Funny jcenter doesn't connect even with vpn and proxy server. I'm not sure if they blocking some countries or it's the ISPs. Eyval dadaTymbal
Anytime bro ... @PedramRepent
But I still have this problem when creating new project! Is there any solution to change that jcenter even in start up? zemnan dadash namoosan damet garm :DPreoccupancy
beside creating new project I have problem with building project either even though after do the changes and in my case Gradle take a long time on app:_debugUnitTestCompile my Gradle version is 2.2.3Preoccupancy
jcenter gives you more libraries than mavenCentral. It's a supersetSamuels
Could not find com.novoda:bintray-release:0.8.0. after change jcenter() to mavenCentral()Indulge
Nowadays , all dependencies have been found in jcenter . mavenCentral() is not a common repo . Try to use jcenter() but first make sure you are using VPN and fast Internet connection . @ImanMarashiRepent
This gives Could not find org.jetbrains.trove4j:trove4j:20160824.Expenditure
C
6

You could optimize the gradle build process ...

Try to put the gradle on offline mode forcing to build without any connection to external repositories, like this:

enter image description here enter image description here

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:

enter image description here

Note: In newer version of Android studio, View->Tool Windows->Gradle->Toggle button of online/offline

Charkha answered 6/2, 2018 at 19:50 Comment(0)
G
3

Once the initial build has completed try enabling offline Mode from the gradle settings. So that every consecutive builds will not start resolving dependencies, instead it gets from the cache. If you add a new dependency it will prompt you to disable offline mode and rebuild it. image of the gradle preview

Grower answered 12/9, 2017 at 6:8 Comment(0)
M
2

In addition to @Mehdi Jahed Manesh answer above.

Increase the amount of memory allocated to the Gradle Daemon VM by 1 Gb, to a minimum of 2 Gb, using the org.gradle.jvmargs property like this:

org.gradle.jvmargs=-Xmx2048m
Meeks answered 25/3, 2017 at 18:48 Comment(0)
V
2

Like Ismail Iqbal's answer said, put gradle under offline mode works for me.

  1. If you're using android studio, follow Fabio Filho's answer

  2. If you're using command line like me, you can try

gradlew assembleReleaseStaging(your task name) --offline
Verity answered 26/2, 2019 at 6:50 Comment(0)
A
1

For those who use repositories that require authentication, this issue may be solved by configuring gradle to use only a single auth scheme:

repositories {
    maven {
        credentials {
            username INTERNAL_REPO_USERNAME
            password INTERNAL_REPO_PASSWORD
        }
        // here comes the important part
        authentication {
            basic(BasicAuthentication)
        }
        url INTERNAL_REPO_URL
    }
}

It turned out that upon authenticating against repositories, gradle attempts authentication using all kind of schemes. The above config makes gradle only use basic auth. Here is the full list of available schemes.

Accordingly answered 8/3, 2019 at 13:41 Comment(0)
G
1

Please check this story. It might not related to gradle but it can speed up your build without too much changes:

https://android.jlelse.eu/is-your-android-studio-always-slow-heres-how-to-speed-up-immediately-326ef9238024

Giacomo answered 9/1, 2020 at 11:12 Comment(0)
A
0

In my case, I solved it by installing a new version of the Java JDK. After installing the new JDK, open AS, go to File→Project Structure→SDK Location, and change JDK location to that of the new version of the Java JDK. Then you will be asked for firewall permission; check Public and apply. Let it resolve gradle.

Altigraph answered 16/4, 2017 at 4:12 Comment(0)
T
0

stop the gradle download and click file -> "Sync project with Gradle Files"

Tunisia answered 7/7, 2020 at 13:51 Comment(0)
V
0

The crazy way is using WARP 1.1.1.1. I already wasted my half day waiting for 10 MB downloaded :(.

Vernice answered 27/1, 2023 at 14:32 Comment(0)
M
-1

I have seen another issue. The solution might help someone

Recently i have set http_proxy and https_proxy under system environment variables. I had to remove both of them and then set the proxy in studio settings.

Matthewmatthews answered 25/1, 2017 at 12:3 Comment(0)
P
-3

change classpath 'com.android.tools.build:gradle:1.5.0' to

classpath 'com.android.tools.build:gradle:2.0.0'

and change buildToolsVersion "23.0.3" to

buildToolsVersion "23.0.2"

and use android studio 2.0 stable version. let me know you want more details or this solution did not work to you.

Pinochle answered 10/4, 2016 at 12:18 Comment(2)
Sure , let me test it .Repent
I`ve downloaded 2.0 stable version and those steps , but nothing has not changed yet.Repent

© 2022 - 2024 — McMap. All rights reserved.