Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Porting C++ to Android


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 28 February 2014 - 01:45 PM

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.



Sponsor:

#2 steven katic   Members   -  Reputation: 274

Like
0Likes
Like

Posted 28 February 2014 - 06:21 PM

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, 28 February 2014 - 06:25 PM.


#3 steven katic   Members   -  Reputation: 274

Like
0Likes
Like

Posted 28 February 2014 - 06:56 PM

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

 

LOCAL_LDLIBS := -llog

 

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, 28 February 2014 - 09:38 PM.


#4 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 28 February 2014 - 09:46 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS