I am coding a game engine and I'm trying to port it to Android to check for errors and tests.
But I still haven't even been able to get a simple function working...
I'm following a few tutorials and I think I know what I am supposed to do:
- Put my C sources in my android project's JNI folder.
- Build it with the Android NDK, after setting everything up.
- Link the lib on my Activity class, call a method, crash.
My C source looks like this:
#include <jni.h> jint Java_my_engine_test_MainActivity_MyFunctionName(JNIEnv* env, jobject obj) { return 0; }
My Android.mk is:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := EngineTest LOCAL_SRC_FILES := EngineTest.c include $(BUILD_SHARED_LIBRARY)
And my Java file is:
package my.engine.test; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //If I uncomment this line, the app crashes, else it's ok //int a = MyFunctionName(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private native int MyFunctionName(); static { System.loadLibrary("EngineTest"); } }
So I've made certain that the function name is according to the specification (Java, package name, class name, then function name) and that I'm using JNI types, and it's a really really really simple function.
Also, for some reason, none of the examples that came with the NDK build for me when using the Eclipse NDK support. I get syntax errors (syntax!). So I'm guessing they must've changed it somewhere along the way...
I'm at a loss, I've been looking all over for what could be causing this and how I can get a simple C function to work in the Android Emulator, but found nothing so far. I'm still checking other tutorials and following them but they're all the same thing and have the same errors, so I'm guessing it must be something else.
I've also set up the NDK folder in Eclipse and now it's auto-building my C sources every time, and it builds, but the app simply crashes when I try to access a function from the lib.
Does anyone know what could be causing this?
Also, a question, the engine's nearly finished for a first project and I made it with certain rules that I'd like to ask about when porting to Android.
I've made it a single class containing everything to run a game, and a few classes available to the game code such as Image, Models, Animations, etc.
There's only one rule that the game code must abide to and that is to inherit the "GameState" class (abstract) so it forces implementations of the methods required for the engine to run (Start, Pause, Resume, Update, Exit functions).
I was thinking in having the engine and making the game elsewhere, like Java (when I get the engine working) for faster coding, but then there's the problems of the C++ classes (Image, Models etc) that probably won't be available on Java (although this I can fix with a wrapper), and the GameState class that a state must inherit to set it to the engine. I don't think I can wrap this last one and I'm don't know how to proceed with this.
If there's no way, I'll probably try coding the entire game in C++ and trying to port it all at once... if only I can get this NDK to work...
Thank you in advance.