Java Gradle 绝对/相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32079022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:01:31  来源:igfitidea点击:

Gradle absolute / relative path

javaandroidgradle

提问by fighter-ii

I have the following problem. When I want to use a local jar in my Gradle dependencies.

我有以下问题。当我想在 Gradle 依赖项中使用本地 jar 时。

If I just add:

如果我只是添加:

compile files('libs/PushLinkAndroid-5.3.0.jar')

It doesn't work.

它不起作用。

C:\Users\Kai\AndroidStudioProjects\ProjectA\app\src\main\java\de\xyz\xxx\projecta\BaseActivity.java:21: error: package com.pushlink.android does not exist import com.pushlink.android.PushLink;

Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. 4 errors

FAILED

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileDebugJava'.

    Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or >--debug option to get more log output.

BUILD FAILED

Total time: 6.543 secs

C:\Users\Kai\AndroidStudioProjects\ProjectA\app\src\main\java\de\xyz\xxx\projecta\BaseActivity.java:21: 错误:包 com.pushlink.android 不存在 import com.pushlink.android .PushLink;

注意:某些输入文件使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。4 错误

失败的

FAILURE:构建失败,出现异常。

  • 出了什么问题:任务“:app:compileDebugJava”的执行失败。

    编译失败;有关详细信息,请参阅编译器错误输出。

  • 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 >--debug 选项运行以获得更多日志输出。

构建失败

总时间:6.543 秒

If i add the absolute path to the Jar, the project compiles and everything works finde:

如果我将绝对路径添加到 Jar,项目将编译并且一切正常:

compile files('C:/Users/Kai/AndroidStudioProjects/ProjectA/libs/PushLinkAndroid-5.3.0.jar')

Looks like there is something wrong with the Path but how could this be when the absolute Path works fine?

看起来路径有问题,但是当绝对路径工作正常时怎么会这样?

EDIT:Build.gradle

编辑:Build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "de.xyz.kai.migraenebook"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile project(':volley')
    debugCompile 'im.dino:dbinspector:3.2.1@aar'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.support:recyclerview-v7:+'
    //compile files('C:/Users/Kai/AndroidStudioProjects/MigraeneBook/libs/PushLinkAndroid-5.3.0.jar')
    compile files('libs/PushLinkAndroid-5.3.0.jar')
    //compile files('libraries/PushLinkAndroid-5.3.0.jar')
    //compile files('C:/Users/Kai/AndroidStudioProjects/MigraeneBook/libraries/PushLinkAndroid-5.3.0.jar')
}

NOTE: I tried different folders, the PushLink.jar is in "libs" and "libraries". It doesn't make a difference which command I use or comment out - absolute path is working, relative not.

注意:我尝试了不同的文件夹,PushLink.jar 位于“libs”和“libraries”中。我使用或注释掉哪个命令没有区别 - 绝对路径有效,相对路径无效。

回答by solar

Try this, it would include all JARs in the local repository, so you wouldn't have to specify it every time:

试试这个,它将包含本地存储库中的所有 JAR,因此您不必每次都指定它:

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

Also, you should know that when referencing a file via

此外,您应该知道在通过以下方式引用文件时

files("relative/path/to/a.jar")

path is evaluated relative to the buildscript this snippet is in. So when your build.gradlefile is located in /a/project/build.gradle, then the jar should be in /a/project/relative/path/to/a.jar. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via

buildscript这个片段是在路径相对评价。所以,当你的build.gradle文件位于/a/project/build.gradle,然后将罐子应该是/a/project/relative/path/to/a.jar。在多项目 gradle 构建中,您可以将 jar 放在相对于根项目的文件夹中,并通过以下方式在所有子项目中引用它

rootProject.files("relative/to/root/a.jar")

So take this to account and re-check if your gradle settings are correct.

因此,请考虑到这一点并重新检查您的 gradle 设置是否正确。

回答by libeasy

Your libs folder is misplaced. You have to put it under MigraeneBook/app, not directly under MigraeneBook.

你的 libs 文件夹放错了地方。你必须把它放在下面MigraeneBook/app,而不是直接放在下面MigraeneBook

回答by Dr4gon

First off in my opinion adding manual libs - if can be avoided - is bad practice because you need to maintain the lib and path yourself.

首先,我认为添加手动库 - 如果可以避免 - 是不好的做法,因为您需要自己维护库和路径。

I had a quick look at the maven repo and it seems they don't have this specific lib there. This is what I'd expect:

我快速浏览了 Maven 仓库,似乎他们那里没有这个特定的库。这是我所期望的:

compile('com.anypackage.name.PushLinkAndroid-5.3.0.jar')

Since this is probably not up to you than rather to the developer team of https://www.pushlink.com/changelog.xhtmlit can't be helped.

由于这可能不取决于您,而取决于https://www.pushlink.com/changelog.xhtml的开发人员团队,因此无能为力。

Now to your problem: Did you start your build with --stacktrace?

现在解决您的问题:您是否使用 --stacktrace 开始构建?

From what I can gather from this

从我可以从中收集到的信息

compile files('C:/Users/Kai/AndroidStudioProjects/ProjectA/libs/PushLinkAndroid-5.3.0.jar')

you're using a Windows system. This is troublesome at some points because Gradle expect the paths to look like this:

您使用的是 Windows 系统。这在某些方面很麻烦,因为 Gradle 期望路径如下所示:

C:/Users/Kai/AndroidStudioProjects/ProjectA/libs/PushLinkAndroid-5.3.0.jar

My wild guess here is that gradle does something like this when you don't specify the path yourself:

我的猜测是,当您自己不指定路径时,gradle 会执行以下操作:

C:\Users\Kai\AndroidStudioProjects\ProjectA\libs/PushLinkAndroid-5.3.0.jar

You can verify the behaviour with this gradle task:

您可以使用此 gradle 任务验证行为:

task printBuildDir() {
    def buildDir = "${buildDir}"
    println buildDir
    def fixedDir = buildDir.replaceAll('\\', '/')
    println fixedDir
}

If my guess is correct, you got a couple of choices:

如果我的猜测是正确的,你有几个选择:

  1. Use the full path
  2. Use a gradle builtin path such as buildDir and replace the \ accordingly to the example (I'm not sure whether this works exactly like my example in dependencies tag, maybe you need to define a global variable yourself)
  3. Get the developers of PushLink to put this lib up to Maven Repo
  1. 使用完整路径
  2. 使用 gradle 内置路径,例如 buildDir 并根据示例替换 \ (我不确定这是否与我在依赖项标签中的示例完全一样,也许您需要自己定义一个全局变量)
  3. 让 PushLink 的开发者把这个库放到 Maven Repo

回答by Nishān Wickramrathna

Specifying each JAR filenames is also a posibility.

指定每个 JAR 文件名也是一种可能性。

dependencies {
    compile files('lib/dependency_one.jar', 'lib/dependency_two.jar')
}

This will tell Gradle to include dependency_one.jar and dependency_two.jar during compilation. To confirm that all required JARs included in compiling configuration, you can create this helper task,

这将告诉 Gradle 在编译期间包含 dependency_one.jar 和 dependency_two.jar。要确认编译配置中包含所有必需的 JAR,您可以创建此帮助程序任务,

task listJars << {
    configurations.compile.each { File file -> println file.name }
}

Then run it,

然后运行它,

$ gradle -q listJars
hibernate-core-3.6.7.Final.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-3.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jta-1.1.jar
slf4j-api-1.6.1.jar

Read more at https://docs.gradle.org/current/userguide/declaring_dependencies.html

https://docs.gradle.org/current/userguide/declaring_dependencies.html阅读更多

回答by tricknology

My issue was that I copied it from the Android Studio IDE itself, as is command+c on the libs from one project and command+v to the libs of the other. For some reason AS produces jar files with size zero... But hey I learned some new stuff about Gradle :)

我的问题是我从 Android Studio IDE 本身复制了它,一个项目的库上的 command+c 和另一个项目的库上的 command+v 也是如此。出于某种原因,AS 生成大小为零的 jar 文件......但是嘿,我学到了一些关于 Gradle 的新东西:)

回答by PRIHLOP

If you call URL scheme path, you can use this example:

如果你调用 URL 方案路径,你可以使用这个例子:

def path = "file://${projectDir}/ojdbc6.jar"

回答by pereckeSokSzam

Old question, but might help someone. I had the same problem, I had 2 libraries in the project root folder, and it didn't work with relative path. Add ../ before the relative path if your library is in your project's root folder.

老问题,但可能会帮助某人。我遇到了同样的问题,我在项目根文件夹中有 2 个库,但它无法使用相对路径。如果您的库位于项目的根文件夹中,则在相对路径之前添加 ../。

implementation fileTree(dir: 'libs', include: ['*.jar'])
...
implementation files('../library_name/libs/some.jar')