How To Add Maven Dependency (Android Studio)
Asked Answered
A

3

9

I have this code below..

<dependency>
    <groupId>me.gujun.android.taggroup</groupId>
    <artifactId>library</artifactId>
    <version>1.4</version>
    <type>apklib</type>
</dependency>

But where do you put this in Android Studio? I've already placed a dependency in build.gradle which is compile 'me.gujun.android.taggroup:library:1.4@aar'

Allix answered 6/1, 2017 at 13:39 Comment(1)
did you try this answer on stackoverflow https://mcmap.net/q/620347/-how-to-import-a-maven-module-to-an-android-studio-project??Equanimity
U
27

In Android Studio 3.0 and above, you can just copy

<dependency>
    <groupId>me.gujun.android.taggroup</groupId>
    <artifactId>library</artifactId>
    <version>1.4</version>
    <type>apklib</type>
</dependency>

into build.gradle, and it automatic converts it to:

implementation 'me.gujun.android.taggroup:library:1.4'
Upward answered 29/4, 2018 at 8:58 Comment(0)
J
2

I had a similar issue just now. I was trying to use khttp in an Android app. Here's the XML that library gave me:

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

...and

<dependency>
    <groupId>com.github.jkcclemens</groupId>
    <artifactId>khttp</artifactId>
    <version>-SNAPSHOT</version>
</dependency>

What I ended up doing was editing the root build.gradle file and adding this:

allprojects {
    repositories {
        maven {
            url "https://jitpack.io"
        }
    }
}

Then, I edited the app's gradle file, and added this line to the existing dependencies { .... } section:

compile 'com.github.jkcclemens:khttp:master-SNAPSHOT'

Once done, I hit the "try again" or "sync" button on the yellow bar that appeared to get Android Studio 3.0.1 to recognise the changes I'd made.

I followed the instructions on the JitPack homepage.

Jornada answered 26/3, 2018 at 13:8 Comment(3)
This was helpful, but did you ever try in a Kotlin Multiplatform project. I tried and it worked for the Android target, but not for common or iOS. Is there a way to make that work?Anuradhapura
@ArjunKalidas Unfortunately, I have no idea what you mean by a "Kotlin Multiplatform project", so I can't help you there :-/ I'm still a beginner when it comes to writing Android apps. I've only tested this using Android Studio targeting a virtual Android device in a regular Android app project. Feel free to contribute your own answer if you manage to figure it out.Jornada
Sure, will do. I am trying to figure out.Anuradhapura
E
1

Android Studio 2020.3.1 (maybe before), they added dependencyResolutionManagement in the settings.gradle. So if you put something in build.gradle the default was to not search the added repository and fail.

One solution is to add your additional repository to the settings.gradle file, and change the RepositoriesMode to PREFER_SETTINGS.

settings.gradle:

import org.gradle.api.initialization.resolve.RepositoriesMode
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS) // CHANGED THIS LINE
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots'} // ADDED REPO
    }
}
rootProject.name = "My Application"
include ':app'
Entremets answered 9/1, 2022 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.