android ndk - Opportunity to using native methods systemwide. How to call method or how it should be named? -


i want use 1 native library plurality of applications. library has compiled through android build system , located in /system/lib/. can loaded in application through system.loadlibrary("libexample"). method in library should declared like

jniexport jstring jnicall java_application1_mainactivity_method1 

turning out unusable because library should used several applications. , of course several applications have own unique names. tried named method like

 jniexport jstring jnicall method1 

and call

public native string method1(string string); 

but of course application trying find java_application1_mainactivity_method1

how call method or how should named?

updated:

i tried use this(see post green tick) tutorial complete project. wrote library using native method:

package com.example.mylib;  import android.os.bundle; import android.app.activity; import android.view.menu;  public class myclass extends activity { public native static string mymethod(string string);     static {         system.loadlibrary("nativelibrary");     } } 

then trying use in application:

// no interesting inclusions import com.example.mylib.myclass;  public class mainactivity extends listactivity { // no interesting code. mymethod(file.getabsolutepath()) //some no interesting code  } 

and working need! confused import com.example.mylib.myclass; "never used" in eclipse. , if create image "is library" project latest no resolving. idea?

yes, can use same jni signature in many applications. class may not belong default package of application, defined in androidmanifest.xml. what?


example:

start hellojni sample ndk (in eclipse, use import -> android -> existing android code, , point ${android-ndk-root}/samples/hello-jni).

build , run on device or emulator.

open new android application project, call testcrossjni. package name our app be: test.cross.jni - no relation com.example.hellojni!

choose "create activity" -> create blank activity.

add new java class project (src/com/example/hellojni/hellojni.java):

package com.example.hellojni;  public class hellojni {     public static string gets() {         return stringfromjni();     }      /* native method implemented      * 'hello-jni' native library, packaged      * application.      */     private native string  stringfromjni();      /* used load 'hello-jni' library on application      * startup. library has been unpacked      * /data/data/com.example.hellojni/lib/libhello-jni.so @      * installation time package manager.      */     static {         system.load("/data/data/com.example.hellojni/lib/libhello-jni.so");     } } 

edit res/layout/activity_main.xml: replace

    line 12 android:text="@string/hello_world" />

    with android:id="@+id/hello_world" />

in src/test/cross/jni/mainactivity.java, add following after

    line 12 setcontentview(r.layout.activity_main);

((android.widget.textview)findviewbyid(r.id.hello_world)).settext(com.example.hellojni.hellojni.gets()); 

profit!


Comments