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

前端 未结 17 1930
攒了一身酷
攒了一身酷 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:47

    You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }
    
    0 讨论(0)
  • 2020-11-21 23:50

    The Question already has been answered in detail. I still want to add something that seems very surprising to me:

    The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..

    So don't rely on the output of this to check whether your referenced local lib files are working correctly.

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

    The best way to do it is to add this in your build.gradle file and hit the sync option

    dependency{
        compile files('path.jar')
    }
    
    0 讨论(0)
  • 2020-11-21 23:51

    You can add jar doing:

    For gradle just put following code in build.gradle:

    dependencies {
    ...
    compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
    ...
    }
    

    and for maven just follow steps:

    For Intellij: File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok

    Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.

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

    I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:

    repositories {
        mavenCentral()
        // Not everything is available in a Maven/Gradle repository.  Use a local 'libs/' directory for these.
        flatDir {
           dirs 'libs'
       }
    }
    
    ...
    
    compile name: 'secondstring-20030401'
    
    0 讨论(0)
提交回复
热议问题