Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle returns package does not exists [duplicate]

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 
like image 714
Fale Avatar asked Jun 04 '13 12:06

Fale


People also ask

How do I refresh the Gradle cache?

Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies. You can also delete the cached files under ~/. gradle/caches . With the next build Gradle would attempt to download them again.

What is flatDir in Gradle?

flatDir(configureClosure) Adds an configures a repository which will look for dependencies in a number of local directories. flatDir(args) Adds a resolver that looks into a number of directories for artifacts. The artifacts are expected to be located in the root of the specified directories.

How do I force Gradle to use specific dependency?

If the project requires a specific version of a dependency on a configuration-level then it can be achieved by calling the method ResolutionStrategy. force(java. lang. Object[]).

What is dependsOn in Gradle?

dependsOn(jar) means that if you run assemble , then the jar task must be executed before. the task transitive dependencies, in which case we're not talking about tasks, but "publications". For example, when you need to compile project A , you need on classpath project B , which implies running some tasks of B .


2 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')

like image 58
David M Avatar answered Sep 20 '22 20:09

David M


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

Tools -> Android -> Sync Project with Gradle Files

like image 22
Hamid Behnam Avatar answered Sep 21 '22 20:09

Hamid Behnam