androidstudio gradle sync not correctly works

2019-09-08 07:50发布

问题:

I have not included gson-2.3.1.jar to my dependencies on gradle.build file. But it gives me below error when going to sync it.

Error:A problem occurred configuring project ':app'.

Could not download gson.jar (com.google.code.gson:gson:2.3.1) Could not get resource 'https://jcenter.bintray.com/com/google/code/gson/gson/2.3.1/gson-2.3.1.jar'. Could not GET 'https://jcenter.bintray.com/com/google/code/gson/gson/2.3.1/gson-2.3.1.jar'. Connection to https://jcenter.bintray.com refused

Why it gives me such error and why it looks for gson. Yes before i have added it. but then excluded it from dependencies list. Here is my gradle files:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

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


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
}

What i noticed, if i am going to add retrofit.jar to lib and then adding it to dependencies on build.gradle file such way => compile files('libs/retrofit-1.9.0.jar') it works, but direct adding with compile 'com.squareup.retrofit:retrofit:1.9.0' not works

回答1:

You don't need to add gson.jar in your libs folder ,just add the below in your dependencies to use gson

compile 'com.google.code.gson:gson:2.3.1'

Remove jar file from libs and compile this again



回答2:

Try adding this block before your dependencies section in build.gradle

repositories {
        jcenter()
    }

Theoretically this should not be required. But I have faced similar problems without specifying repository.



回答3:

get data from jcenter() need some filtering Tunnel ! in Iran for example. use mavenCentral for get retrofit or somethings like this...

add this line in graddle

buildscript {
repositories {

   mavenCentral()

}
dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'
}
}

allprojects {
repositories {
    mavenCentral()
}
}