我是NDK以及c,c ++的新手。如果我弄错了,请原谅我。
我得到的Log cat中的错误是java.lang.UnsatisfiedLinkError
在ndk-build之后,我可以看到lib文件夹中的libfirst-opengl.so
库
听到我的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();
}
和我的make文件: -
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)
我不知道产生错误的部分,所以我添加了我的所有代码
请帮助我。
谢谢
答案 0 :(得分:3)
您的原生函数位于MySurfaceViewRenderer渲染器类而不是MainActivity中:
JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnDrawFrame
应该是:
JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_MySurfaceViewRenderer_JNIOnDrawFrame
我没有尝试访问jni中的内部类,但这似乎是基于规范的声音。