Home » Android » java – Gradle returns package does not exists

java – Gradle returns package does not exists

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to add the gson library to my android project (I’m devloping using the Andrdoid-studio).

To add the library, I changed the AppProject/AppName/build.gradle file in this way:

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

repositories {
    mavenCentral()
}

dependencies {
    compile files('libs/android-support-v4.jar')
    compile 'com.google.code.gson:gson:2.2.4'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 4
        targetSdkVersion 16
    }
}

It seems to work until I try to use it.

When I try to include it with:

import com.google.code.gson;

Gradle complains affirming:

Gradle: error: package com.google does not exist
How to&Answers:

The accepted answer did not work for me, but this did:

  • Download the GSON JAR file and copy it to your /libs/ folder inside your application project.
  • Open the build.gradle file at the root level of your project and edit your dependencies to include the new .jar file:

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
    }
    
  • Build -> Rebuild Project

Optionally, you can specify one or more specific JAR files with files rather than fileTree, such as:
compile files('libs/google-gson-1.7.1/gson-1.7.1.jar')

Answer:

One solution that might be helpful is to try Syncing Project with Gradle Files

Tools -> Android -> Sync Project with Gradle Files

Answer:

In my projects this happens sometimes in the :javadoc task.
If you do not need the :javadoc task you can ignore it with:

./gradlew :install -x :javadoc