How to add local .jar file dependency to build.gradle file?

前端 未结 17 2069
攒了一身酷
攒了一身酷 2020-11-21 23:23

So I have tried to add my local .jar file dependency to my build.gradle file:

apply plugin: \'java\'

sourceSets {
    main {
        java {
            srcD         


        
相关标签:
17条回答
  • 2020-11-21 23:36

    If you really need to take that .jar from a local directory,

    Add next to your module gradle (Not the app gradle file):

    repositories {
       flatDir {
           dirs 'libs'
       }
    }
    
    
    dependencies {
       implementation name: 'gson-2.2.4'
    }
    

    However, being a standard .jar in an actual maven repository, why don't you try this?

    repositories {
       mavenCentral()
    }
    dependencies {
       implementation 'com.google.code.gson:gson:2.2.4'
    }
    
    0 讨论(0)
  • 2020-11-21 23:36

    A simple way to do this is

    compile fileTree(include: ['*.jar'], dir: 'libs')
    

    it will compile all the .jar files in your libs directory in App.

    0 讨论(0)
  • 2020-11-21 23:41

    Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder

    This steps will automatically add your dependency to gralde

    Very Simple

    0 讨论(0)
  • 2020-11-21 23:42

    According to the documentation, use a relative path for a local jar dependency as follows:

    dependencies {
        implementation files('libs/something_local.jar')
    }
    
    0 讨论(0)
  • 2020-11-21 23:42

    A solution for those using Kotlin DSL

    The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:

    dependencies {
        compile(files("/path/to/file.jar"))
        testCompile(files("/path/to/file.jar"))
        testCompile("junit", "junit", "4.12")
    }
    

    Remember that if you're using Windows, your backslashes will have to be escaped:

    ...
    compile(files("C:\\path\\to\\file.jar"))
    ...
    

    And also remember that quotation marks have to be double quotes, not single quotes.


    Edit for 2020:

    Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:

    dependencies {
        implementation(files("/path/to/file.jar"))
        testImplementation(files("/path/to/file.jar"))
        testImplementation("junit", "junit", "4.12")
    }
    
    0 讨论(0)
  • 2020-11-21 23:42

    Shorter version:

    dependencies {
        implementation fileTree('lib')
    }
    
    0 讨论(0)
提交回复
热议问题