Stevecode
Stevecode

Reputation: 45

Android NDK, and placing function outside activity class

Mentioned in the article: http://androidcookbook.com/Recipe.seam?recipeId=77

Is:

"In the Activity class, outside any methods:

static {
    System.loadLibrary("sqrt-demo");
}
// In a method of the Activity class where you need to use it:
double d = SqrtDemo.sqrtC(123456789.0);

"

If a designer wants to place all NDK actions (loading a library,defining a function) outside of the activity, can she/he?
I tried to solve this question by creating a new class and importing it into the activity. I placed the load library in the classes constructor, and I placed the method definition in the other class. The activity appears to load the library but will crash, with the error that It cannot find the function

Edit to add details: I attempted this again but by placing the loadLibrary function back in the Activity. I get the same crash report: java.lang.UnsatisfiedLinkError: Native method not found:

Upvotes: 1

Views: 293

Answers (1)

Stevecode
Stevecode

Reputation: 45

It appears I have found the issue, it was an overlook on my part. You can load the library from the imported class, and you can place the method definition in the the imported class. YOU MUST however modify the function as so: initial header: JNIEXPORT jobjectArray JNICALL Java_com_stackoverflow_MainAcitivty_helloWorld(){

to JNIEXPORT jobjectArray JNICALL Java_com_stackoverflow_newclass_helloWorld(){

Upvotes: 1

Related Questions