How to import Maven dependency in Android Studio/IntelliJ?

后端 未结 5 406
后悔当初
后悔当初 2020-11-29 18:54

I\'ve created a new Android project using the default wizard in Android Studio. Compiled, and deployed the app to my device. All is well.

Now I want to import an ext

相关标签:
5条回答
  • 2020-11-29 19:23

    As of version 0.8.9, Android Studio supports the Maven Central Repository by default. So to add an external maven dependency all you need to do is edit the module's build.gradle file and insert a line into the dependencies section like this:

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

    You will see a message appear like 'Sync now...' - click it and wait for the maven repo to be downloaded along with all of its dependencies. There will be some messages in the status bar at the bottom telling you what's happening regarding the download. After it finishes this, the imported JAR file along with its dependencies will be listed in the External Repositories tree in the Project Browser window, as shown below.

    enter image description here

    Some further explanations here: http://developer.android.com/sdk/installing/studio-build.html

    0 讨论(0)
  • 2020-11-29 19:23

    Android Studio 3

    The answers that talk about Maven Central are dated since Android Studio uses JCenter as the default repository center now. Your project's build.gradle file should have something like this:

    repositories {
        google()
        jcenter()
    }
    

    So as long as the developer has their Maven repository there (which Picasso does), then all you would have to do is add a single line to the dependencies section of your app's build.gradle file.

    dependencies {
        // ...
        implementation 'com.squareup.picasso:picasso:2.5.2'
    }
    
    0 讨论(0)
  • 2020-11-29 19:25

    Try itext. Add dependency to your build.gradle for latest as of this post

    Note: special version for android, trailing "g":

    dependencies {
        compile 'com.itextpdf:itextg:5.5.9'
    }
    
    0 讨论(0)
  • 2020-11-29 19:31

    I am using the springframework android artifact as an example

    open build.gradle

    Then add the following at the same level as apply plugin: 'android'

    apply plugin: 'android'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
       compile group: 'org.springframework.android', name: 'spring-android-rest-template', version: '1.0.1.RELEASE'
    }
    

    you can also use this notation for maven artifacts

    compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    

    Your IDE should show the jar and its dependencies under 'External Libraries' if it doesn't show up try to restart the IDE (this happened to me quite a bit)

    here is the example that you provided that works

    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 
        } 
    } 
    
    0 讨论(0)
  • 2020-11-29 19:38
    1. Uncheck "Offline work" in File>Settings>Gradle>Global Gradle Settings
    2. Resync the project, for example by restarting the Android Studio
    3. Once synced, you can check the option again to work offline.
    0 讨论(0)
提交回复
热议问题