Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In most of the cases it is only necessary to transfer simple data structures. For this purpose it is fairly enough to only work with static methods. The ARES Touch SDK provides a handful helper classes to avoid working directly with the JNI interfaces and Java environment. The utility class is called FxJavaUtils and provides a large set of classes. These are the steps to address a simple data transfer between the host app and the C++ app part and vice versa.

...

1.1 Create a Java class and add a public native static method

public static native RETURNTYPE JAVA_TO_CPP( PARAMS );

The RETURNTYPE as well as the PARAMS must be declared with Java known data types. The method name JAVA_TO_CPP does not need to contain characters like _ , may be just JAVATOCPP.

...

Declare your matching C++ method and make sure it matches exactly in RETURNTYPE, PARAMS and name JAVA_TO_CPP else you will more likely face an unsatisfied linker error on runtime. Keep an eye that PATH_TO_YOUR_CLASSexactly refers to the location of your Java class/file based on the root “src/main/java” folder.
e.g.: …/YOUR_ANDROID_PROJECT/src/main/java/PATH_TO_YOUR_CLASS
e.g.: …/YOUR_ANDROID_PROJECT/src/main/java/com/domain/folder/CLASSNAME
e.g.: Java_com_domain_folder_CLASSNAME_JAVA_TO_CPP()

Status
colourYellow
titleC++

...

Code Block
JNIEXPORT RETURNTYPE JNICALL Java_PATH_TO_YOUR_CLASS_JAVA_TO_CPP( JNIEnv * env, jclass clazz [, PARAMS ...] )
{
    // RETURNTYPE
    // void just return; or ignore
    // else return RETURNTYPE;
}

In the case you specify PARAMS and RETURNTYPE please declare the data types in the JNI matching pairs.

...

In general returning back values to Java is quite easy but there is one important fact that must be considered. In the case the C++ method returns back an class object it must be returned strongly. This means it is not allowed to be destroyed inside the C++method else it will lead to a crash. Java is taking care about freeing the object after it is processed on Java side else it is always necessary to clean up object references to avoid running into limit of possible references. All FxJavaUtils class objects destroy the JNI reference automatically. Only in case the C++ method needs to return an Java class object it is highly important to use methods like GetStrongJObject() or GetStrongJString() else GetJObject() or GetJString() do the job. The usage of complex objects to carry more data can be read in the topic High level: C++ and JNI data transfer .

Status
colourYellow
titleC++

...