简体   繁体   中英

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.

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.

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.

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

[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?

2 hours after asking this question i figured it out an it pissed me off, due to how simple it was.

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.

my particular problem had not to do with my libgdx project but with my library project. 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.

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