简体   繁体   中英

How do I include a local jar dependency in Gradle?

This must be fairly simple to accomplish, but I can't seem to get it right.

I have a gradle task that creates a jar from some external classes, and my code is heavily dependent on those classes. When I try to build, I get errors from compileJava saying package <com.etc...> does not exist for the import lines in my classes.

Here's the relevant code

project.ext.set("myVersion", "v1")

dependencies {
  // tried this, but it gives  me a circular dependency error for my compile & zip tasks
  compile files('${buildDir}/dist/my-jar-${project.myVersion}.jar') {
      builtBy 'zipExternalClasses'
  }
  // tried either of these, but still get package does not exist
  compile files('${buildDir}/dist/my-jar-${project.myVersion}.jar') 
  runtime files('${buildDir}/dist/my-jar-${project.myVersion}.jar')
}


// The dependent task compileExternalClasses compiles the classes from a source folder
// I can see that the jar is successfully created in 'build/dist'
task zipExternalClasses(dependsOn: 'compileExternalClasses', type: Jar) {
    // code for zipping compiled external classes
}

This is what i do to include local jars.

I place them in a app/libs folder.

Then in the build.gradle (Module: app) looks like this:

dependencies {

    compile project(':protobuf')
    compile files('libs/android-support-v13.jar')

}

Where "android-support-v13.jar" is the jar file i previously placed in libs folder.

I think the problem might be that you are using single quote in your file path. Single quoted String in Groovy does not do String interpolation so what you essentially get as your path is just ${buildDir}/dist/my-jar-${project.myVersion}.jar itself, which is clearly not right.

Just try double quotes like below:

dependencies {
  compile files("${buildDir}/dist/my-jar-${project.myVersion}.jar") 
}

The variables, 'buildDir' and 'project.myVersion', will be substituted with the real value when the String is evaluated.

Take a look at the Groovy documentation about String and GString and I'm sure it'll be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM