2

I am new to NDK and as well as c, c++. so If I am mistaken please forgive me.

The error in Log cat I am getting is java.lang.UnsatisfiedLinkError After ndk-build I can see the libfirst-opengl.so library in lib folder

Hear is my code for 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();
    }

}

code for C++ file is..

#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();
}

and My make file:-

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)

I wasn't aware of the part which is genrating error so I have added my all code

please help me with that.

Thank you

Nixit Patel
  • 4,435
  • 5
  • 30
  • 57
  • I've got a few questions for you: 1. Which Android API do you use? 2. Does your APK contains "libfirst-opengl.so" in lib folder? (you can open APK as zip archive). 3. Have you include opengl lib in project properties (properties->android->library)? Also try to change `APP_ABI := armeabi-v7a` to `APP_ABI := armeabi` in file `jni/Application.mk`. Actualy I have [similar issue](http://stackoverflow.com/questions/10857301/unable-to-link-native-library-in-opencv-android-sample) but still haven't fix it :( – ArtemStorozhuk Jun 02 '12 at 12:33
  • 1)I am using 2.3.3, 2)yes it there, 3)I have seen some NDK's sample and they also haven't included such library. – Nixit Patel Jun 02 '12 at 13:46

1 Answers1

3

your native functions are in the MySurfaceViewRenderer renderer class not MainActivity so:

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_JNIOnDrawFrame

should probably be:

JNIEXPORT void JNICALL Java_com_nix_opendk_MainActivity_MySurfaceViewRenderer_JNIOnDrawFrame

I haven't attempted to access an inner class in jni but that seems sound based on the spec.

Matt
  • 492
  • 2
  • 12