Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio, gradle never uses my local maven repo

How can I get gradle 1.10 to use my local maven repository? When I run my gradle script it always looks for the artifact in /Applications/Android Studio.app/sdk/extras/android/m2repository/ instead of ${HOME}/.m2/repository/

In the build.gradle I have tried:

repositories {
    mavenCentral()
    mavenLocal()
 }

and tried this:

repositories {
    mavenCentral()
    maven {
        url '${HOME}/.m2/repository/'
    }
 }

I then have a dependency that is located in my local .m2 repository:

dependencies {
    compile 'com.android.support:support-v4:20.0.+'
    compile 'com.android.support:appcompat-v7:20.0.+'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile 'com.profferapp:proffer_dto_customer_android:1.0'
    compile 'com.test:artifact:1.0' // this is in my .m2 repository
}

None of these configurations point to my local maven repository.

like image 552
S-K' Avatar asked Jul 29 '14 12:07

S-K'


People also ask

Why is Gradle not picking up my local Maven repository?

Because of the misquoted string, it's probably not seeing your local repository. Also, the other question about the plugin implicitly adding the Maven repo in your SDK is also correct. This will pick up Android support dependencies. Gradle Single vs Double Quotes has a little more information and a link you can follow. Show activity on this post.

Where does Gradle use the default repository location?

If no settings.xml is available, Gradle uses the default location USER_HOME /.m2/repository. As a general advice, you should avoid adding mavenLocal () as a repository. There are different issues with using mavenLocal () that you should be aware of: Maven uses it as a cache, not a repository, meaning it can contain partial modules.

How to publish Maven artifact in Gradle?

The artifact was published using the maven-publish plugin. In the local maven repository metadata was published as maven-metadata-local.xml (as before). However Gradle 6.0 seems to be exclusively looking for a file named maven-metadata.xml - which isn't there.

What does the Android plugin add to my Maven repository?

the android plugin adds another repository - the one under your SDK. It contains those support and extension libraries. com.test:artifact:1.0 is likely taken form you local Maven repository. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.


2 Answers

If you want to do ${...} variable substitution, you need to use double quotes (") instead of single quotes ('). Double quotes denote GStrings, which can do that, but strings with single quotes are always interpreted literally.

So:

repositories {
    mavenCentral()
    maven {
        url "${HOME}/.m2/repository/"
    }
 }

Make sure you put this block in your module's build file, not the buildscript block of the top-level build file; the latter only determines where to find the Android plugin, not your module-specific dependencies

Because of the misquoted string, it's probably not seeing your local repository. Also, the other question about the plugin implicitly adding the Maven repo in your SDK is also correct. This will pick up Android support dependencies.

Gradle Single vs Double Quotes has a little more information and a link you can follow.

like image 156
Scott Barta Avatar answered Dec 21 '22 23:12

Scott Barta


I want to comment Scott Barta's answer but I have not enough reputation.

  1. $HOME does not works for me (in CLI, not Android Studio), System.getenv('HOME') does
  2. it's not necessary to put the repositories block into EVERY modules' build.gradle file, just put the block inside a "allprojects" block in the top-level build.gradle (I learned it here)

So, the final solution I use is:

// in the top-level build.gradle:
buildscript {
    ...
}

allprojects {
    repositories {
        mavenCentral()
        maven { url System.getenv('HOME') + "/.m2/repository/" }
    }
}
like image 32
Zhiyong Avatar answered Dec 21 '22 23:12

Zhiyong