mrjasmin
mrjasmin

Reputation: 1270

cannot resolve symbol gson

I have big troubles using the gson library. When building the project i get:

"Cannot resolve symbol gson"

I have googled for hours but no solution seems to work. Here is my build.gradle file:

android {
    compileSdkVersion 24
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.jasminkrhan.vaktija"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.8.2'

}

And also my build.gradle file for the project:

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

buildscript {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Does someone see something wrong with my code? Why does not android studio find the library? I have already tried to restart Android Studio but that doesn't help.

Upvotes: 2

Views: 13716

Answers (2)

P Fuster
P Fuster

Reputation: 2334

Try to write the repositories all in one, and use the functions included for Google dependencies and jcenter already, instead of providing the URL. Like so:

buildscript {
...
    repositories {
        google()
        jcenter()
    }
}

And also:

allprojects {
    repositories {
        google()
        jcenter()
    }
}

It's also important that you start using the implementation tag instead of compile in gradle, as its being deprecated:

implementation 'com.google.code.gson:gson:2.8.5'

Upvotes: 1

jbmcle
jbmcle

Reputation: 811

compile is outdated. Please use implementation

implementation 'com.google.code.gson:gson:2.8.5'

You need these in Project Level build.gradle

buildscript {

repositories {
    google()
    jcenter()
}

.....
allprojects {
    repositories {
        google()
        jcenter()
    }
}

Upvotes: 7

Related Questions