Jump to content
  • Advertisement
Sign in to follow this  

Porting C++ to Android

This topic is 2063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


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 {
    protected void onCreate(Bundle savedInstanceState) {

        //If I uncomment this line, the app crashes, else it's ok
        //int a = MyFunctionName();

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    private native int MyFunctionName();
    static {

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.

Share this post

Link to post
Share on other sites

I cannot see anything wrong in the code as such.

have you checked if the library was built?

it should show up in the console window. And the .so library file should be in the libs folder.

what are the messages in the logcat window?

Edited by steven katic

Share this post

Link to post
Share on other sites

you may need to put the following line in your Android.mk  file to see the log output:




Sorry, no you don't , you only need to do this to use the logging library: i.e to log your own native messages to the LogCat window.

Edited by steven katic

Share this post

Link to post
Share on other sites

Wow, thanks alot!


I'm new to Java and Eclipse so I don't know my way around and I didn't even knew how to look for errors... after learning where the log is I found it was an UnstatisfiedLink - Native Method not found, and a simple google taught me that JNI doesn't work with C++ naming, so I have to use extern "C" before the functions and now it's not crashing anymore!


Really, thank you!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!