Reputation: 613
I'm using libgdx and as I wanted to debug my game, I tried import Log from Android utilities to my main game file located in "core\src\com\mygdx\game\". For some reason AndroidStudio doesn't allow me to import that class. Can anyone point me a solution? Tried: rebuild project.
Upvotes: 1
Views: 3716
Reputation: 81539
As it is specified in the wiki under the page Interfacing with Platform-specific Code, what you need to do is provide an interface for what library you want to use, and use the interface in the core. And what you must do is provide the implementation from each launcher class.
For example, your desktop launcher looks something like this,
package com.badlogic.drop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "Drop";
config.width = 800;
config.height = 480;
new LwjglApplication(new Drop(), config);
}
}
You can see the new Drop()
here - you can specify additional dependencies that you want to bind within the game, right?
public class Drop extends Game {
public static final String TAG = Drop.class.getSimpleName();
private Logger logger;
public Drop(Logger logger) {
this.logger = logger;
logger.debug(TAG, "`Drop` game initialized.");
...
}
}
And in the desktop it changes to
new LwjglApplication(new Drop(new DesktopLogger()), config);
Where Logger is [core]
public interface Logger {
void debug(String tag, String message);
//...
}
And desktop logger is [desktop]
public class DesktopLogger implements Logger {
public void debug(String tag, String message) {
System.out.println("D/" + tag + ": " + message);
}
}
And android logger is [android]
public class AndroidLogger implements Logger {
public void debug(String tag, String message) {
Log.d(tag, message);
}
}
So Android launcher becomes
public class AndroidLauncher extends AndroidApplication {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config= new AndroidApplicationConfiguration();
config.useAccelerometer = false;
config.useCompass = false;
initialize(new Drop(new AndroidLogger()), config);
}
}
Upvotes: 1