Since most Android applications are written in Java, it is easy to get the source code through anti-compilation, so in order to maintain the core algorithm, logic security, most applications will use the part C or C + + implementation, for example: The user's Landing, the underlying in C or C + + code written. The following will demonstrate the simple C language to verify user name and password correctness, to achieve login.
1. Create an Android app
The creation process is similar to a typical Android application by adding 2 edittext and a button, as shown in the example below
Layout file
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 <EditText7 Android:id= "@+id/et1"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Wrap_content"Ten Android:hint= "User name"/> One <EditText A Android:id= "@+id/et2" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" the Android:hint= "Password"/> - <Button - Android:id= "@+id/bt1" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" - Android:hint= "Login"/> + </LinearLayout>
2. Add Native method
public native int login (String username,string password);
3. Get the appropriate header file
D:\android\ndk_login\src>javah com.forsta.login.MainActivity
Get Com_forsta_login_mainactivity.h file
Com_forsta_login_mainactivity.h
1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class com_forsta_login_mainactivity*/4 5 #ifndef _included_com_forsta_login_mainactivity6 #define_included_com_forsta_login_mainactivity7 #ifdef __cplusplus8 extern "C" {9 #endifTen /* One * class:com_forsta_login_mainactivity A * Method:login - * Signature: (ljava/lang/string; ljava/lang/string;) I - */ the jniexport jint jnicall java_com_forsta_login_mainactivity_login -(JNIENV *, Jobject, jstring, jstring); - - #ifdef __cplusplus + } - #endif + #endif
4. Add Jni folder, android.mk,login.c file
Login.c
1#include <stdio.h>2#include <jni.h>3#include <malloc.h>4#include <string.h>5#include"Com_forsta_login_mainactivity.h"6 7 /**8 * Return value char* This represents the first address of a char array9 * JSTRING2CSTR Converts the type of jstring in Java into a char string in the C languageTen */ One Char* JSTRING2CSTR (jnienv*env, jstring jstr) A { - Char* Rtn =NULL; -Jclass clsstring = (*env)->findclass (env,"java/lang/string");//String theJstring Strencode = (*env)->newstringutf (env,"GB2312");//get a Java string "GB2312" -Jmethodid mid = (*env)->getmethodid (env,clsstring,"getBytes","(ljava/lang/string;) [B");//[String.getbytes ("gb2312"); -Jbytearray barr= (Jbytearray) (*env)->callobjectmethod (Env,jstr,mid,strencode);//String. GetByte ("GB2312"); -Jsize alen = (*env)->getarraylength (Env,barr);//the length of a byte array +jbyte* ba = (*env)getbytearrayelements (env,barr,jni_false); - if(Alen >0) + { ARTN = (Char*) malloc (alen+1);//" the" at memcpy (Rtn,ba,alen); -rtn[alen]=0; - } -(*env)->releasebytearrayelements (Env,barr,ba,0);// - returnRtn; - } in - //Login Judgment Logic to intLoginChar*username,Char*password) + { - Char*user="Forsta"; the Char*pwd="123456"; * intI=0, j=0; $ if(strcmp (user,username)! =0|| strcmp (password,pwd)! =0){Panax Notoginseng return 404; - } the return $; + } A the jniexport jint jnicall java_com_forsta_login_mainactivity_login +(JNIENV *env, Jobject obj, jstring username, jstring pwd) { - Char*name=jstring2cstr (env,username); $ Char*password=jstring2cstr (env,pwd); $ returnlogin (Name,password); - -}
Android.mk
1 Local_path: = $ (call My-dir) 2 include $ (clear_vars) 3 local_module : = login4local_src_files: = login.c5 include $ (build_shared_ LIBRARY)
5. Compiling, loading library files
[Email protected]/cygdrive/d/android/ndk_login
$ ndk-build
Android NDK:WARNING:APP_PLATFORM android-18 is larger than Android:minsdkversion 8 in./androidmanifest.xml
[Armeabi] Compile Thumb:login <= login.c
jni/login.c:in function ' jstring2cstr ':
Jni/login.c:22:4: warning:incompatible Implicit declaration of built-in function ' memcpy ' [enabled by default]
[Armeabi] SharedLibrary:liblogin.so
[Armeabi] Install:liblogin.so = libs/armeabi/liblogin.so
Loading library files
static{
System.loadlibrary ("login");
}
6. Login implementation
1 Bt.setonclicklistener (New Onclicklistener () {2 3 @Override4 Public void OnClick (View v) {5 String Username=t1.gettext (). toString (). Trim ();6 String Pwd=t2.gettext (). toString (). Trim ();7 int result =login (username,pwd);8 if (result==200)9 {Ten Toast.maketext (Getapplicationcontext (), "Login successful, code=200", 1). Show (); One }else{ A - Toast.maketext (Getapplicationcontext (), "Login failed, code=404", 1). Show (); - } the - } -});
7. Display Results