extern"C"JNIEXPORTjstringJNICALLJava_com_example_myapplication_MainActivity_stringFromJNI(JNIEnv*env,jobject/* this */){std::stringhello="Hello from C++";returnenv->NewStringUTF(hello.c_str());}
//Java层packagecom.example.luomd5;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.TextView;importcom.example.luomd5.databinding.ActivityMainBinding;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luomd5' library on application startup.static{System.loadLibrary("luomd5");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;tv.setText(mdString("Hello XiaLuoHun!"));}publicstaticnativeStringmdString(Stringstring);}
packagecom.example.luomd5;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.TextView;importcom.example.luomd5.databinding.ActivityMainBinding;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luomd5' library on application startup.static{System.loadLibrary("luomd5");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;tv.setText(mdString("Hello XiaLuoHun!"));}publicstaticnativeStringmdString(Stringstring);}
//http://aospxref.com/android-8.1.0_r81/xref/art/runtime/art_method.h
classArtMethod{protected:// Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
// The class we are a part of.
GcRoot<mirror::Class>declaring_class_;// Access flags; low 16 bits are defined by spec.
// Getting and setting this flag needs to be atomic when concurrency is
// possible, e.g. after this method's class is linked. Such as when setting
// verifier flags and single-implementation flag.
std::atomic<std::uint32_t>access_flags_;/* Dex file fields. The defining dex file is available via declaring_class_->dex_cache_ */// Offset to the CodeItem.
uint32_tdex_code_item_offset_;// Index into method_ids of the dex file associated with this method.
uint32_tdex_method_index_;/* End of dex file fields. */// Entry within a dispatch table for this method. For static/direct methods the index is into
// the declaringClass.directMethods, for virtual methods the vtable and for interface methods the
// ifTable.
uint16_tmethod_index_;// The hotness we measure for this method. Managed by the interpreter. Not atomic, as we allow
// missing increments: if the method is hot, we will see it eventually.
uint16_thotness_count_;// Fake padding field gets inserted here.
// Must be the last fields in the method.
structPtrSizedFields{// Short cuts to declaring_class_->dex_cache_ member for fast compiled code access.
mirror::MethodDexCacheType*dex_cache_resolved_methods_;// Pointer to JNI function registered to this method, or a function to resolve the JNI function,
// or the profiling data for non-native methods, or an ImtConflictTable, or the
// single-implementation of an abstract/interface method.
void*data_;// Method dispatch from quick compiled code invokes this pointer which may cause bridging into
// the interpreter.
void*entry_point_from_quick_compiled_code_;}ptr_sized_fields_;};
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;tv.setText(stringFromJNI());}publicnativeStringstringFromJNI();publicnativeintLuoAdd(intn1,intn2);publicstaticnativeStringmdString(Stringstring);}
#include<jni.h>#include<string>#include"md5.h"extern"C"JNIEXPORTjstringJNICALLJava_com_example_luodst_MainActivity_stringFromJNI(JNIEnv*env,jobject/* this */){std::stringhello="Hello from C++";returnenv->NewStringUTF(hello.c_str());}extern"C"JNIEXPORTjintJNICALLJava_com_example_luodst_MainActivity_LuoAdd(JNIEnv*env,jobjectthiz,jintn1,jintn2){returnn1+n2;}extern"C"JNIEXPORTjstringJNICALLJava_com_example_luodst_MainActivity_mdString(JNIEnv*env,jclassclazz,jstringstring){constchar*szString=env->GetStringUTFChars(string,NULL);std::stringstrMd5=MD5(szString).toStr();returnenv->NewStringUTF(strMd5.c_str());}
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;StringstrSample="Hello XiaLuoHun!";tv.setText("mdString: "+mdString(strSample));}publicstaticnativeStringmdString(Stringstring);}
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;tv.setText("LuoAdd: "+LuoAdd(10,20));}publicnativeintLuoAdd(intn1,intn2);}
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;StringstrSample="Hello XiaLuoHun!";tv.setText("Refmd5: "+Refmd5(strSample));}publicStringJavamd5(Stringstring){if(TextUtils.isEmpty(string)){return"";}MessageDigestmd5=null;try{md5=MessageDigest.getInstance("MD5");byte[]bytes=md5.digest(string.getBytes());StringBuilderresult=newStringBuilder();for(byteb:bytes){Stringtemp=Integer.toHexString(b&0xff);if(temp.length()==1){temp="0"+temp;}result.append(temp);}returnresult.toString();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}return"";}publicnativeStringRefmd5(Stringstring);}
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;tv.setText(mdString("Hello XiaLuoHun!"));}publicstaticnativeStringmdString(Stringstring);}
packagecom.example.luomd5;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.TextView;importcom.example.luomd5.databinding.ActivityMainBinding;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmException;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luomd5' library on application startup.static{System.loadLibrary("luomd5");}privateActivityMainBindingbinding;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);binding=ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextViewtv=binding.sampleText;StringstrSample="XiaLuoHun";Stringstr="mdString: "+mdString(strSample)+"\n"+"Javamd5: "+Javamd5(strSample)+"\n"+"Refmd5: "+Refmd5(strSample)+"\n"+"Refmd5Sec: "+Refmd5Sec(strSample);tv.setText(str);}publicStringJavamd5(Stringstring){if(TextUtils.isEmpty(string)){return"";}MessageDigestmd5=null;try{md5=MessageDigest.getInstance("MD5");byte[]bytes=md5.digest(string.getBytes());StringBuilderresult=newStringBuilder();for(byteb:bytes){Stringtemp=Integer.toHexString(b&0xff);if(temp.length()==1){temp="0"+temp;}result.append(temp);}returnresult.toString();}catch(NoSuchAlgorithmExceptione){e.printStackTrace();}return"";}publicstaticnativeStringmdString(Stringstring);publicnativeStringRefmd5(Stringstring);publicnativeStringRefmd5Sec(Stringstring);}
#include<jni.h>#include<string>#include"md5.h"__attribute__((visibility("hidden")))jstringJNICALLLuoDynamicNative(JNIEnv*env,jobject/* this */){std::stringhello="Hello from C++ LuoHun";returnenv->NewStringUTF(hello.c_str());}extern"C"JNIEXPORTjstringJNICALLJava_com_example_luomd5_MainActivity_mdString(JNIEnv*env,jclassclazz,jstringstring){constchar*szString=env->GetStringUTFChars(string,NULL);std::stringstrMd5=MD5(szString).toStr();returnenv->NewStringUTF(strMd5.c_str());}extern"C"JNIEXPORTjstringJNICALLJava_com_example_luomd5_MainActivity_Refmd5(JNIEnv*env,jobjectthiz,jstringstring){//md5 = MessageDigest.getInstance("MD5");
jclassclsMessageDigest=env->FindClass("java/security/MessageDigest");jmethodIDgetInstance=env->GetStaticMethodID(clsMessageDigest,"getInstance","(Ljava/lang/String;)Ljava/security/MessageDigest;");jstringstrMD5=env->NewStringUTF("MD5");jobjectobjMD5=env->CallStaticObjectMethod(clsMessageDigest,getInstance,strMD5);//byte[] bytes = md5.digest(string.getBytes());
jclassclsString=env->FindClass("java/lang/String");jmethodIDgetBytes=env->GetMethodID(clsString,"getBytes","()[B");jbyteArrayjbAry=(jbyteArray)env->CallObjectMethod(string,getBytes);jmethodIDdigest=env->GetMethodID(clsMessageDigest,"digest","([B)[B");jbyteArrayjbAryResult=(jbyteArray)env->CallObjectMethod(objMD5,digest,jbAry);char*cmd5=reinterpret_cast<char*>(env->GetByteArrayElements(jbAryResult,0));inti;chardest[32]={0};for(i=0;i<16;i++){sprintf(dest+i*2,"%02x",(unsignedint)cmd5[i]);}returnenv->NewStringUTF(dest);}extern"C"JNIEXPORTjstringJNICALLJava_com_example_luomd5_MainActivity_Refmd5Sec(JNIEnv*env,jobjectthiz,jstringstring){jclassclsMainActivity=env->FindClass("com/example/luomd5/MainActivity");jmethodIDJavamd5=env->GetMethodID(clsMainActivity,"Javamd5","(Ljava/lang/String;)Ljava/lang/String;");jstringstrMd5=(jstring)env->CallObjectMethod(thiz,Javamd5,string);returnstrMd5;}
packagecom.example.luodst;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.widget.TextView;importcom.example.luodst.databinding.ActivityMainBinding;publicclassMainActivityextendsAppCompatActivity{// Used to load the 'luodst' library on application startup.static{System.loadLibrary("luodst");}privateActivityMainBindingbinding;/* @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Example of a call to a native method
TextView tv = binding.sampleText;
tv.setText(stringFromJNI());
}*/protectednativevoidonCreate(BundlesavedInstanceState);/**
* A native method that is implemented by the 'luodst' native library,
* which is packaged with this application.
*/publicnativeStringstringFromJNI();}