LibGDX's TexturePacker with gradle

2020-02-28 16:52发布

问题:

I am attempting to create a gradle task which runs TexturePacker according to the instructions here. (Note that I am using Android Studio and its directory structure rather than Eclipse.) I first added the following to the Android Studio project's build.gradle:

import com.badlogic.gdx.tools.texturepacker.TexturePacker
task texturePacker << {
  if (project.ext.has('texturePacker')) {
    logger.info "Calling TexturePacker: "+texturePacker
    TexturePacker.process(texturePacker[0], texturePacker[1], texturePacker[2])
  }
}

This gave the error

unable to resolve class com.badlogic.gdx.tools.texturepacker.TexturePacker

Moving the texturePacker task to build.gradle in the desktop project produces the same error. According to http://www.reddit.com/r/libgdx/comments/2fx3vf/could_not_find_or_load_main_class_texturepacker2/, I also need to add compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion" to the root build.gradle under the desktop project's dependencies. When I do so, I still get the same error.

So I have several questions:

  1. Where is the correct place for the texturePacker task? Which build.gradle do I put this in?

  2. How do I solve the dependency issue and the unable to resolve class... error?

  3. How do I specify the input and output directories and the atlas file when running this with gradle? (Assuming the first two questions are solved.)

回答1:

I got it working by adding gdx-tools to my buildscript dependencies:

buildscript{
    dependencies {
       ...
       classpath 'com.badlogicgames.gdx:gdx-tools:1.5.4'
    }
}

by doing this, my desktop's build.gradle was able to import the texture packer class:

import com.badlogic.gdx.tools.texturepacker.TexturePacker
task texturePacker << {
    if (project.ext.has('texturePacker')) {
        logger.info "Calling TexturePacker: "+ texturePacker
        TexturePacker.process(texturePacker[0], texturePacker[1], texturePacker[2])
    }
}

please note that your desktop project's ext will need to have texturePacker defined:

project.ext {
    mainClassName = "your.game.package.DesktopLauncher"
    assetsDir = new File("../android/assets");
    texturePacker = ["../images/sprites", "../android/assets", "sprites"]
}