Android - JNI recommendations

I want to include a small, thin and medium C parser in my Android project. In the past, I programmed JNI, but not on any native (C) -th development on Android. My plan is to compile C lib into SO and create a JNI wrapper around it, which I am going to use in my application. This is how it can / should be done? The second and most important question is how can I include .so in my APK? Where will it go?

+3
source share
4 answers

use Android NDK
Download n docs Android NDK 1.6

JNI lib, lib .

+2

, , , Java/JNI/C , Java.

-, "Java Int", "C long" JNI, .

, JNI- :

  • .
  • C.
  • .

, Java. "C", java- (+, -, *, ==, > ), "" (int, char, String) Java, , C.

java- - , JVM , Java-, .

"" Java , , , xml beans .. .., .

+5

JNI Android ( src, res). (1), someshared-lib.c.

(1)

Java_YourPackageName_YourClassNameWhereYoudeclareYourNativeFunction_NativeFuntionName(JNIEnv* env,jobject thiz)
{


//your c code , the JNI will act as a wrapper for it

return (*env)->NewStringUTF(env, "<string to pass or you can mention jchar * type string >");

}

(2) IN java

package YourPackageName;

public class YourClassNameWhereYoudeclareYourNativeFunction extends Activity
{

    public native String  NativeFuntionName();
    String returnValue = NativeFuntionName();


}

(3) Android.mk :

LOCAL_PATH: = $( my-dir)

$(CLEAR_VARS)

LOCAL_MODULE: = someshared-lib// lib , c

LOCAL_SRC_FILES: = someshared-lib.c// , (1)

$(BUILD_SHARED_LIBRARY)

ndk-build ( PATH =: $PATH

JNI, : ndk-build

, someshared-lib lib, . apk device.To ,

/data/data/your_package_name/lib.

/data/data/your _package_name/lib (/system/lib) (JNI), Android.

, -, , c, :

Java_YourPackageName_YourClassNameWhereYoudeclareYourNativeFunction_NativeFuntionName(JNIEnv* env,jclass obj,jobject thiz)
{
jfieldID fid;
jboolean enable_flag;

//Pass the class object having all the variable from the android app to  the JNI in the jclass obj and access its members using the field ID and using Get and Set firld ID.

clazz = (*env)->GetObjectClass(env, info);
fid = (*env)->GetFieldID(env,clazz,"some_variable","X"); //"X" - type of variable for boolean it //is Z , for INtezer it is I, for double it is D,

//for getting teh value fomr the JNI
 enable_flag = (*env)->GetBooleanField(env, **thiz**, fid);


//for setting the value 

fid = (*env)->GetFieldID(env,clazz,"other_float_variable","D");
(*env)->SetDoubleField(env,**thiz**,fid,other_float_variable);



}

Android . ,

(2) :

YourPackageName;

public class YourClassNameWhereYoudeclareYourNativeFunction extends Activity
{

    public native String  NativeFuntionName();
    String returnValue = NativeFuntionName( exampleStruct exst);

exampleStruct:

public class exampleStruct {

protected boolean  some_variable = 0;//no log saving by default 
protected float  other_float_variable   = 0;

}

}

, .

+3

Source: https://habr.com/ru/post/1719686/


All Articles