Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Dependencies with Android Studio / Gradle

I am using the Gradle build system bundled with Android Studio. So far, I am able to build multi-project setups using dependencies that are stored in my project structure. I would now like to use a maven dependency, to no avail. I have written a very simple build.gradle file that always fails :

    buildscript {         repositories {             maven { url 'http://repo1.maven.org/maven2' }         }         dependencies {             classpath 'com.android.tools.build:gradle:0.4'         }     }     apply plugin: 'android'      dependencies {         compile 'com.google.android:support-v4:r7'     }      android {         compileSdkVersion 17         buildToolsVersion "17.0.0"          defaultConfig {             minSdkVersion 7             targetSdkVersion 16         }     } 

with the following message :

    * What went wrong:         A problem occurred configuring project ':absabs'.         > Failed to notify project evaluation listener.            > Could not resolve all dependencies for configuration ':absabs:compile'.               > Could not find com.google.android:support-v4:r7.                 Required by:                     absabs:absabs:unspecified .... Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7. 

It happens with any artifact I have tried so far. Any idea of what's wrong ?

Thanks

like image 947
Guillaume Avatar asked May 21 '13 13:05

Guillaume


People also ask

Can Gradle use Maven dependency?

Gradle can consume dependencies available in the local Maven repository.

Can you use Maven with Android Studio?

configure Android Studio to also build using Maven: Now if you run the project in Android studio, Maven will be used.

How do I add a dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

How do I sync Gradle with dependencies?

Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies". This should resolve your issue.


1 Answers

The "repositories" block in the buildscript section only applies to the build environment. You also need to specify which repository to use when resolving dependencies for building your project, so you need to put something like the following in your build.gradle file:

repositories {     mavenCentral() } 

Your complete file would look something like this:

buildscript {     repositories {         maven { url 'http://repo1.maven.org/maven2' }     }     dependencies {         classpath 'com.android.tools.build:gradle:0.4'     } } apply plugin: 'android'  repositories {     mavenCentral() }  dependencies {     compile 'com.google.android:support-v4:r7' }  android {     compileSdkVersion 17     buildToolsVersion "17.0.0"      defaultConfig {         minSdkVersion 7         targetSdkVersion 16     } } 

Note the two instances of "repositories". You can read more about what this actually means in the gradle docs.

like image 156
Peter Niederwieser Avatar answered Oct 13 '22 20:10

Peter Niederwieser