2

I've seen here and here that you cannot override native methods in android, but I was wondering if I can have a class declaring some native methods, and then extend it with another class declaring some additional native methods.

My situation is as follows:

public class A{
    public native int aMethod();
}

public class B extends A{
    public native int bMethod();

    static {
        try{
            System.loadLibrary("MyNativeLibraryWithBothaMethodAndbMethod");
        }
        catch (java.lang.UnsatisfiedLinkError e){
            System.out.println (e);
        }
    }
}

public class MyActivity extends Activity {
    private B bClass;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        bClass = new B();
        bClass.aMethod();//Working fine
        bClass.bMethod();//UnsatisfiedLinkError

    }
}

The library is loaded without problems, and the first method is called, but not the second.

If I move the bMethod from B to A, everything works fine.

Moving the System.loadlibrary() from B to A seems to have no effect.

Is it possible to extend A class with additional native methods?

Community
  • 1
  • 1
ocramot
  • 1,361
  • 28
  • 55

1 Answers1

3

Ok, I just found the solution. The problem was in the .cpp file:

I edited the MyNativeLibraryWithBothaMethodAndbMethod file:

#include "my_package_name_A.h"
JNIEXPORT jint JNICALL Java_my_package_name_A_aMethod( ... ){ ... }
JNIEXPORT jint JNICALL Java_my_package_name_A_bMethod( ... ){ ... }

with

#include "my_package_name_A.h"
JNIEXPORT jint JNICALL Java_my_package_name_A_aMethod( ... ){ ... }
JNIEXPORT jint JNICALL Java_my_package_name_B_bMethod( ... ){ ... }

Note the "B" in the second signature.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
ocramot
  • 1,361
  • 28
  • 55