简体   繁体   English

如何使用Gradle将jar文件添加到libgdx项目

[英]How can I add jar files to a libgdx project with Gradle

I am working with libgdx and I would like to build some helper classes to use in my future libgdx projects. 我正在使用libgdx,我想构建一些帮助程序类以在将来的libgdx项目中使用。

I made a new java project and adding a class called Renderer with a method called clear to clear the screen, in order to access all the libgdx classes I added the jars from the core project to a "libs" folder under the new project and added it to the build path. 我创建了一个新的Java项目,并使用名为clear的方法添加了一个名为Renderer的类以清除屏幕,以便访问所有libgdx类,我将核心项目中的jars添加到了新项目下的“ libs”文件夹中,并添加了它到构建路径。

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;

public class Renderer {

    public void clear(int r, int g, int b) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(r/255f, g/255f, b/255f, 1f);
    }

}

I would like to export this utilities project as a jar file and use Gradle to add it to the libgdx project. 我想将此实用程序项目导出为jar文件,并使用Gradle将其添加到libgdx项目中。

I read this page in order to achieve this. 我阅读了页面以实现此目的。

After adding compile fileTree(dir:"../libs", include: "*.jar") to my core and android projects and refreshing the Gradle project, the desktop project works fine and has access to the Renderer class, however the android project wont compile with this error message 在将compile fileTree(dir:"../libs", include: "*.jar")到我的核心和android项目并刷新Gradle项目之后,桌面项目可以正常工作并且可以访问Renderer类,但是android项目将不会编译此错误消息

[2018-05-31 19:14:35 - TheCosmos-android] Dx unsupported class file version 
52.0
...while parsing com/arcxesgames/crest/graphics/renderer/Renderer.class
[2018-05-31 19:14:35 - TheCosmos-android] Dx 1 error; aborting
[2018-05-31 19:14:35 - TheCosmos-android] Conversion to Dalvik format failed 
with error 1

I believe this is from android counting jar files two times, is there anyway to fix this? 我相信这是来自Android两次计数jar文件的,是否有解决方法?

2 hours after asking this question i figured it out an it pissed me off, due to how simple it was. 提出这个问题2小时后,我发现它很简单,因此惹恼了我。

so to use a jar file all you have to do is add compile fileTree(dir:"../libs", include: "*.jar") to your project roots build.gradle project and "dir" is from the root folder so if you add a folder called "libs" into your core folder and add the jars into that you need to do compile fileTree(dir:"libs", include: "*.jar") for the core project and compile fileTree(dir:"../core/libs", include: "*.jar") for the android project. 因此,要使用jar文件,您要做的就是将compile fileTree(dir:"../libs", include: "*.jar")到项目根目录build.gradle项目中,而“ dir”来自根文件夹因此,如果您在核心文件夹中添加一个名为“ libs”的文件夹,并将其添加到jar中,则需要为核心项​​目compile fileTree(dir:"libs", include: "*.jar")compile fileTree(dir:"../core/libs", include: "*.jar")用于android项目。

my particular problem had not to do with my libgdx project but with my library project. 我的特定问题与我的libgdx项目无关,而与我的库项目有关。 I was using JDK8 with the lib and JDK6 for the libgdx projects because android does not support JDK8 as of now, once I changed the projects JDK version everything compiled and worked as expected on my device. 我将JDK8与lib和JDK6一起用于libgdx项目,因为android目前不支持JDK8,一旦我更改了项目JDK版本,所有内容都会在设备上按预期进行编译和工作。

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

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