0

My project use native library. when I change the package name , I got following error.

code: Native method not found:

 com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors:()I
                                                                            at com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors(Native Method)
                                                                            at com.nooshindroid.yastashir.controller.JNIServer.runServer(JNIServer.java:27)
                                                                            at com.nooshindroid.yastashir.game.FreebloksActivity.startNewGame(FreebloksActivity.java:520)
                                                                            at com.nooshindroid.yastashir.game.FreebloksActivity$16.onClick(FreebloksActivity.java:774)
                                                                            at com.nooshindroid.yastashir.game.ColorListDialog.onItemClick(ColorListDialog.java:79)

when I sreach the net I understand that I must rebuil my JNI folder in command line. In command line I don't know how to give project path. I read somewhere that I must write like this .

 cd <project>
 $ <ndk>/ndk-build

I don't know the steps to do that job. it show my project path and when I write Dir , it shows me all of things that exist in my folder. but nothing change.

bmm
  • 73
  • 1
  • 7

2 Answers2

0

WHen you use JNI, unless you specify otherwise, the package name is part of the function name it looks for in the C code. So if you change the package name in Java, you also need to change all your C code JNI function names.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I know it how can I change it. – bmm Jun 28 '16 at 08:26
  • You edit the C files. If you want to change it permanently so you can change your package name at will you can use registerNatives to override the default mapping, but its a lot of work and you have to maintain that mapping. See http://stackoverflow.com/questions/1010645/what-does-the-registernatives-method-do for a writeup on it – Gabe Sechan Jun 28 '16 at 08:29
  • Because the command doesn't rename the functions for you. It just builds the C code by running the compiler. You have to change it by hand, although it probably wouldn't be difficult to write a custom sed command – Gabe Sechan Jun 28 '16 at 08:39
  • unfortunately, I cant understand how to fix my C code. and the link you mention is so difficult cause I am new with android. please suggest an understandable way – bmm Jun 28 '16 at 08:42
  • Learn C. That's basically what you need to fix it. It actually has nothing to do with android it's all about JNI- how java and C talk to one another. – Gabe Sechan Jun 28 '16 at 08:43
  • Yes. But it doesn't solve your problem, you need to edit the C code. Until then building it won't do what you want. – Gabe Sechan Jun 28 '16 at 08:47
  • Would you please tell that for me? I just want to test it. – bmm Jun 28 '16 at 08:49
  • You have it in your original post. You need to have the ndk installed and the ndk-build script from it in your path. You may need cygwin or similar Unix shell app if running on Windows. – Gabe Sechan Jun 28 '16 at 08:51
  • it doesn't work. the link that u mention , where I add this method? – bmm Jun 28 '16 at 08:53
0

As @GabeSechan has said, you need to edit the C or C++ code to match the new package name. When you change the package name, the directory structure changes. If you don't edit the C or C++ code to match, the methods won't be found.

You need to change your JNI methods from

Java_com_old_package_name_methodName 

to

Java_com_new_package_name_methodName 

See the JNI documentation for an example.

Rebuild your native code after renaming the methods.

Francesca Nannizzi
  • 1,695
  • 13
  • 18