简体   繁体   中英

Issues calling C++ from Java code (Android)

I am attempting to call native functions from android code, using the ndk, and am running into some issues. The app will run for a short amount of time, then an error appears indicating that is "has stopped unexpectedly." Logcat reveals this is because of an UnsatisfiedLinkError: surfaceCreated.

Logcat:

02-12 00:09:33.918: E/AndroidRuntime(25900): FATAL EXCEPTION: GLThread 10
02-12 00:09:33.918: E/AndroidRuntime(25900): java.lang.UnsatisfiedLinkError: surfaceCreated
02-12 00:09:33.918: E/AndroidRuntime(25900):    at com.example.lesnaH.gameengine.GameRenderer.surfaceCreated(Native Method)
02-12 00:09:33.918: E/AndroidRuntime(25900):    at com.example.lesnaH.gameengine.GameRenderer.onSurfaceCreated(GameSurfaceView.java:23)
02-12 00:09:33.918: E/AndroidRuntime(25900):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1373)
02-12 00:09:33.918: E/AndroidRuntime(25900):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1138)

Here is where I am trying to call the functions from Java:

package com.example.lesnaH.gameengine;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class GameSurfaceView extends GLSurfaceView {
    private GameRenderer renderer;

    public GameSurfaceView(Context context)
    {
        super(context);
        renderer = new GameRenderer();
        setRenderer(renderer);
    }
}

class GameRenderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        surfaceCreated();
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        surfaceChanged(width, height);
    }

    public void onDrawFrame(GL10 gl) {
        drawFrame();
    }

    public native void surfaceCreated();
    public native void surfaceChanged(int w, int h);
    public native void drawFrame();
}

Here is the implementation of those functions in C++:

#include <jni.h>
void initializeOpenGL();
void resizeViewport(int newWidth, int newHeight);
void renderFrame();

JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_surfaceCreated(JNIEnv*, jobject)
{
    initializeOpenGL();
}

JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_surfaceChanged(JNIEnv*, jobject, jint width, jint height)
{
    resizeViewport(width, height);
}

JNIEXPORT void JNICALL Java_com_example_lesnaH_gameengine_GameRenderer_drawFrame(JNIEnv*, jobject)
{
    renderFrame();
}

And here is my Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := gameengine
LOCAL_SRC_FILES := graphics.cc \
                   graphicsExports.cc
LOCAL_LDLIBS := -lGLESv2

include $(BUILD_SHARED_LIBRARY)

If anyone wants me to post anything else/provide any more information, I would be happy to do so. Thanks so much for any help you can give me!

我认为您缺少System.loadLibrary()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM