Last active
December 30, 2015 00:09
-
-
Save modamoda/7748012 to your computer and use it in GitHub Desktop.
How to handle instantiated C++ object from JAVA through JNI(NDK)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOCAL_PATH := $(call my-dir) | |
include $(CLEAR_VARS) | |
LOCAL_MODULE := SampleCode | |
LOCAL_SRC_FILES := package_name_ToasterSample.cpp | |
include $(BUILD_SHARED_LIBRARY) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check Signatures | |
javap -classpath bin/classes -p -s com.example.helloJni | |
#auto-generate header files | |
javah -classpath bin/classes com.example.hellojni.HelloJni |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "package_name_ToasterSample.hh" | |
using namespace MyNamespace::SampleCode; | |
Sampler::Sampler() | |
: _value(0), _str(nullptr), _ptr(0) | |
{ | |
_ptr = reinterpret_cast<jlong>(this); | |
} | |
Sampler::Sampler(jlong ptr) | |
: _value(0), _str(nullptr), _ptr(ptr) | |
{ | |
} | |
Sampler::~Sampler() | |
{ | |
} | |
Sampler* Sampler::GetSampler(jlong ptr) | |
{ | |
Sampler *s = reinterpret_cast<Sampler *>(ptr); | |
return s; | |
} | |
int Sampler::GetValue() | |
{ | |
return this->_value; | |
} | |
void Sampler::SetValue(int v) | |
{ | |
this->_value = v; | |
} | |
JNIEXPORT jlong JNICALL Java_me_moda_deleteme_ToasterSample_createToaster (JNIEnv *, jobject) | |
{ | |
Sampler *sampler = new Sampler(); | |
return reinterpret_cast<jlong>(sampler); | |
} | |
JNIEXPORT void JNICALL Java_me_moda_deleteme_ToasterSample_deleteToaster | |
(JNIEnv *, jobject, jlong ptr) | |
{ | |
Sampler *sampler = Sampler::GetSampler(ptr); | |
delete sampler; | |
} | |
JNIEXPORT jint JNICALL Java_me_moda_deleteme_ToasterSample_getValue(JNIEnv *env, jobject obj, jlong ptr) | |
{ | |
Sampler *s = Sampler::GetSampler(ptr); | |
return (int)(s->GetValue()); | |
} | |
JNIEXPORT void JNICALL Java_me_moda_deleteme_ToasterSample_setValue(JNIEnv *env, jobject obj, jlong ptr, jint value) | |
{ | |
Sampler *s = Sampler::GetSampler(ptr); | |
s->SetValue(value); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package package.name.deleteme; | |
public class ToasterSample | |
{ | |
static { | |
System.loadLibrary("SampleCode"); | |
} | |
public static ToasterSample getToaster() { | |
return new ToasterSample(); | |
} | |
public ToasterSample() { | |
super(); | |
_ptr = createToaster(); | |
} | |
protected void finalize() { deleteToaster(); } | |
private synchronized void deleteToaster() { deleteToaster(_ptr); } | |
private long _ptr; | |
private int _value; | |
private static String _str; | |
private native long createToaster(); | |
private native void deleteToaster(long ptr); | |
public int getValue() { | |
return getValue(_ptr); | |
} | |
public void setValue(int v) { | |
setValue(_ptr, v); | |
} | |
private native int getValue(long ptr); | |
private native void setValue(long ptr, int v); | |
public static native String getStr(); | |
public static native void setStr(String str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment