2

我是 NDK 以及 c、c++ 的新手。所以如果我错了,请原谅我。

我得到的 Log cat 中的错误是java.lang.UnsatisfiedLinkError 在 ndk-build 之后我可以看到libfirst-opengl.solib 文件夹中的库

听到是我的 JAVA 代码

public class MainActivity extends Activity {
    private GLSurfaceView mGLSurfaceView;

    /** Called when the activity is first created. */
    static{
        System.loadLibrary("first-opengl");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set window full screen and remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Create the EGL surface (set OpenGL version 2.0) and override the
        // renderer with our custom one
        mGLSurfaceView = new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(2);
        mGLSurfaceView.setRenderer(new MySurfaceViewRenderer());
        setContentView(mGLSurfaceView);
    }

    @Override
    public void onPause() {
        super.onPause();
        mGLSurfaceView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mGLSurfaceView.onResume();
    }

    public class MySurfaceViewRenderer implements Renderer {

        public void onSurfaceCreated(GL10 unused, EGLConfig config) {
            JNIOnSurfaceCreated();
        }

        public void onSurfaceChanged(GL10 unused, int w, int h) {
            JNIOnSurfaceChanged(w, h);
        }

        public void onDrawFrame(GL10 unused) {
            JNIOnDrawFrame();
        }

        private native void JNIOnSurfaceCreated();

        private native void JNIOnSurfaceChanged(int w, int h);

        private native void JNIOnDrawFrame();
    }

}

C++ 文件的代码是..

#include <jni.h>
#include <string.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <stdio.h>
#include <stdlib.h>
#include <android/log.h>


#define  LOG_TAG    "opengl-first"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

void surfaceCreated(){
    const GLubyte* puiOpenGLVersion = glGetString(GL_VERSION);
    LOGI("OpenGL Version: \"%s\"\n", (char*)puiOpenGLVersion);

}

void surfaceChanged(int w, int h){

    LOGI("JNIOnSurfaceChanged %dx%d\n", w, h);
    glViewport(0, 0, w, h);
}

void drawFrame(){
     // Clear the screen to a random shade of grey
        float fColor = (float)(rand() % 256) / 255.0f;
        glClearColor(fColor, fColor, fColor, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
}

extern "C" {
    JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnDrawFrame(JNIEnv* env, void* reserved);
    JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnSurfaceChanged(JNIEnv* env, void* reserved, int w, int h);
    JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnSurfaceCreated(JNIEnv* env, void* reserved);
};

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnSurfaceCreated(JNIEnv* env, void* reserved)
{
    surfaceCreated();
}

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnSurfaceChanged(JNIEnv* env, void* reserved, int w, int h)
{
    surfaceChanged(w,h);
}

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnDrawFrame(JNIEnv* env, void* reserved)
{
    drawFrame();
}

和我的制作文件:-

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_LDLIBS    := -llog -lGLESv2
LOCAL_CFLAGS    := -Werror
LOCAL_MODULE    := first-opengl
LOCAL_SRC_FILES := opengl-first.cpp

include $(BUILD_SHARED_LIBRARY)

我不知道产生错误的部分,所以我添加了所有代码

请帮帮我。

谢谢

4

1 回答 1

3

您的本机函数位于 MySurfaceViewRenderer 渲染器类中,而不是 MainActivity,因此:

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnDrawFrame

应该是:

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_MySurfaceViewRenderer_JNIOnDrawFrame

我没有尝试访问 jni 中的内部类,但这似乎基于规范。

于 2012-06-03T00:08:50.290 回答