简体   繁体   English

如何在 Gradle 中包含本地 jar 依赖项?

[英]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.我有一个 gradle 任务,它从一些外部类创建一个 jar,我的代码在很大程度上依赖于这些类。 When I try to build, I get errors from compileJava saying package <com.etc...> does not exist for the import lines in my classes.当我尝试构建时,我从 compileJava 得到错误,说我的类中的导入行package <com.etc...> does not exist

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.我将它们放在 app/libs 文件夹中。

Then in the build.gradle (Module: app) looks like this:然后在 build.gradle(Module:app) 中看起来是这样的:

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.其中“android-support-v13.jar”是我之前放在 libs 文件夹中的 jar 文件。

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. Groovy 中的单引号字符串不进行字符串插值,因此您基本上得到的路径只是${buildDir}/dist/my-jar-${project.myVersion}.jar本身,这显然是不正确的。

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.评估字符串时,变量“buildDir”和“project.myVersion”将被替换为实际值。

Take a look at the Groovy documentation about String and GString and I'm sure it'll be useful.查看有关String 和 GString的 Groovy 文档,我相信它会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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