Reading/Writing Files on Android

Hi everyone,

I’m trying to load and save files on Android.
I still have the same project setup as here:

I’m using the Android Cheat Sheet approach:
(dir)

Root Folder
| + src/main
| | + java/<pack>
| | | - JmeCommunicator.java //interface
| | | - MainActivity.java //Implements JmeCommunicator
| | - res
| | - AndroidManifest.xml
|
| + game
| | +src/main/java/<pack>
| | | - Main.java //A SimpleApplication. Has 'public JmeCommunicator comm;'
| |
|

I try to communicate though an interface implemented by MainActivity (and passed through to Main). However, Eclipse shows errors, and upon gradle build, java can’t compile JmeCommunicator (error: cannot find symbol). This makes sense to me, but I don’t know how to fix this.

Does anyone have a solution or an alternative?
Thanks in advance.

Check your classpath. If it’s missing any libraries that JmeCommunicator references, that would cause the error you’re seeing.

This is what JmeCommunicator currently looks like, if that’s what you mean:

public interface JmeCommunicator {
	
}

EDIT:

I’m also using Gradle, and I’m not good at it, so that might be a problem.

If you’re calling methods through a reference of type JmeCommunicator, then that’s your problem. If you have, say, JmeCommunicator comm = new ThingaMaBobbyThatImplementsJmeCommunicator(); and then call comm.doSomething() you’ll get an error. All the compiler knows is that comm is a JmeCommunicator, and as far as its concerned JmeCommunicator can’t do anything at all because the interface is empty.

I’m not as far as to actually call something through the interface.
All I’m trying to do now is this:
(in Main.java)

public JmeCommunicator getJmeCommunicator() {
    return comm;
}
    
public void setJmeCommunicator(JmeCommunicator comm) {
    this.comm = comm;
}

Ok, then that’s not it. Can you post a full trace of the compiler output? There’s not really enough information here to say what’s going on.

Incremental java compilation is an incubating feature.
The TaskInputs.source(Object) method has been deprecated and is scheduled to be removed in Gradle 4.0. Please use TaskInputs.file(Object).skipWhenEmpty() instead.
:preBuild UP-TO-DATE
:preDebugBuild UP-TO-DATE
:checkDebugManifest
:preReleaseBuild UP-TO-DATE
:game:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
CatanViewer\game\src\main\java\io\github\runningchickendev\ctnview\Main.java:33: error: cannot find symbol
private JmeCommunicator comm;
^
symbol: class JmeCommunicator
location: class Main
CatanViewer\game\src\main\java\io\github\runningchickendev\ctnview\Main.java:56: error: cannot find symbol
public JmeCommunicator getJmeCommunicator() {
^
symbol: class JmeCommunicator
location: class Main
CatanViewer\game\src\main\java\io\github\runningchickendev\ctnview\Main.java:60: error: cannot find symbol
public void setJmeCommunicator(JmeCommunicator comm) {
^
symbol: class JmeCommunicator
location: class Main
Note: CatanViewer\game\src\main\java\io\github\runningchickendev\ctnview\state\MenuState.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
1 warning
FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:game:compileJava’.
    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.

seems like a default ‘class not found’ error

I’m probably going to give up at trying to make some communicator class/interface, and will settle using Input/OutputStreams instead. That way I don’t have to pass any ‘unknown’ classes to the sub-project. First try looked promising, but I haven’t fully tested it yet.

I would still like to see a way to implement class tho. I think this problem has to do with Gradle.

Oh, I see what’s happening. You have the JmeCommunicator the root project and your main application in the Root/game subproject. Is your build.gradle in the root directory?

Edit: Gradle definitely won’t like that setup. The easiest way is probably to move your Android specific stuff into another subproject and then reference that as a dependency in your game project. (Assuming you set up a root project in the root folder and add subprojects for the other two.)

you should move the JmeCommunicator interface to your main game project and write the implementation in the Android project since it references your game as a library.

You can just attach an app state in the main activity and pass it the file path. This is an easy way to interact between the Android Stuff and your main project.

It’s as easy as just saving the file to that path.

That worked.
Thanks!