如何在Android Studio/IntelliJ中导入Maven依赖?

mun*_*kay 89 android intellij-idea android-studio

我使用Android Studio中的默认向导创建了一个新的Android项目.编译并将应用程序部署到我的设备.一切都很好.

现在我想导入Maven上可用的外部库.(http://square.github.io/picasso/).我去了模块属性,并添加了一个Maven库.它在依赖项列表中正确显示.此外,它显示在编辑器中,我可以在代码中正确使用它.

但是,在编译时,我收到Gradle错误:无法找到类

有任何想法吗?

use*_*967 80

我使用springframework android工件作为示例

打开build.gradle

然后在与apply plugin相同的级别添加以下内容:'android'

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
   compile group: 'org.springframework.android', name: 'spring-android-rest-template', version: '1.0.1.RELEASE'
}
Run Code Online (Sandbox Code Playgroud)

你也可以将这种表示法用于maven工件

compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
Run Code Online (Sandbox Code Playgroud)

您的IDE应该在"外部库"下显示jar及其依赖项,如果它没有显示尝试重新启动IDE(这在我身上发生了很多)

这是您提供的有效的示例

buildscript { 
    repositories { 
        maven { 
            url 'repo1.maven.org/maven2'; 
        } 
    } 
    dependencies { 
        classpath 'com.android.tools.build:gradle:0.4' 
    } 
} 
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies { 
    compile files('libs/android-support-v4.jar') 
    compile group:'com.squareup.picasso', name:'picasso', version:'1.0.1' 
} 
android { 
    compileSdkVersion 17 
    buildToolsVersion "17.0.0" 
    defaultConfig { 
        minSdkVersion 14 
        targetSdkVersion 17 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

  • 关键部分是重新启动IDE.此版本的IntelliJ没有发现变化. (18认同)

dod*_*der 69

从版本0.8.9开始,Android Studio默认支持Maven Central Repository.因此,要添加外部maven依赖项,您需要做的就是编辑模块的build.gradle文件,并在依赖项部分中插入一行,如下所示:

dependencies {

    // Remote binary dependency
    compile 'net.schmizz:sshj:0.10.0'

}
Run Code Online (Sandbox Code Playgroud)

您将看到一条消息显示为"立即同步..." - 单击它并等待maven repo与其所有依赖项一起下载.底部的状态栏中会显示一些消息,告诉您下载的情况.完成此操作后,导入的JAR文件及其依赖项将列在项目浏览器窗口的外部存储库树中,如下所示.

在此输入图像描述

这里有一些进一步的解释:http: //developer.android.com/sdk/installing/studio-build.html

  • 如果没有:存储库{mavenCentral()} (6认同)

Pha*_*inh 9

在 中Android Studio Bumblebee,maven 声明在settings.gradle文件中(而不是build.gradle像以前的 AS 版本那样文件)

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
           ....
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Sur*_*gch 6

Android Studio 3

由于Android Studio现在使用JCenter作为默认存储库中心,因此谈论Maven Central的答案已过时.你的项目的build.gradle文件应该是这样的:

repositories {
    google()
    jcenter()
}
Run Code Online (Sandbox Code Playgroud)

因此,只要开发人员拥有他们的Maven存储库(Picasso所做的那样),那么您只需要在应用程序的build.gradle文件的依赖项部分添加一行.

dependencies {
    // ...
    implementation 'com.squareup.picasso:picasso:2.5.2'
}
Run Code Online (Sandbox Code Playgroud)


rin*_*esh 5

  1. 在“文件”>“设置”>“Gradle”>“全局 Gradle 设置”中取消选中“离线工作”
  2. 重新同步项目,例如通过重新启动 Android Studio
  3. 同步后,您可以再次选中该选项以离线工作。