Android technology 12: Simulated NDK login and android12ndk Simulation

Source: Internet
Author: User

Android technology 12: Simulated NDK login and android12ndk Simulation

Because most Android applications are written in Java, it is easy to obtain the source code through decompilation. To maintain the security of the core algorithm and logic, most applications use c or c ++ to implement this part. For example, for user login, c or c ++ code is used at the underlying layer. The following demonstrates how to verify the correctness of the user name and password in a simple C language to achieve logon.

1. Create an Android Application

The creation process is similar to that of a General android Application. Add two EditText and one Button, as shown in the following figure.

 

Layout File

1 <LinearLayout xmlns: 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 <EditText 7 android: id = "@ + id/et1" 8 android: layout_width = "fill_parent" 9 android: layout_height = "wrap_content" 10 android: hint = "username"/> 11 <EditText 12 android: id = "@ + id/et2" 13 android: layout_width = "fill_parent" 14 android: layout_height = "wrap_content" 15 android: hint = "password"/> 16 <Button17 android: id = "@ + id/bt1" 18 android: layout_width = "wrap_content" 19 android: layout_height = "wrap_content" 20 android: hint = "login"/> 21 </LinearLayout>

 

2. Add the Native METHOD

Public native int login (String username, String password );

3. Get the corresponding header file

D: \ Android \ ndk_login \ src> javah com. forsta. login. MainActivity

Get the com_forsta_login_MainActivity.h file.

Com_forsta_login_MainActivity.h

 1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class com_forsta_login_MainActivity */ 4  5 #ifndef _Included_com_forsta_login_MainActivity 6 #define _Included_com_forsta_login_MainActivity 7 #ifdef __cplusplus 8 extern "C" { 9 #endif10 /*11  * Class:     com_forsta_login_MainActivity12  * Method:    login13  * Signature: (Ljava/lang/String;Ljava/lang/String;)I14  */15 JNIEXPORT jint JNICALL Java_com_forsta_login_MainActivity_login16   (JNIEnv *, jobject, jstring, jstring);17 18 #ifdef __cplusplus19 }20 #endif21 #endif

 

4. Add the 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 the char array 9 * Jstring2CStr converts the jstring type in java into a char in C string 10 */11 char * Jstring2CStr (JNIEnv * env, jstring jstr) 12 {13 char * rtn = NULL; 14 jclass clsstring = (* env)-> FindClass (env, "java/lang/String "); // String15 jstring strencode = (* env)-> Ne WStringUTF (env, "GB2312"); // obtain a java string "GB2312" 16 jmethodID mid = (* env)-> GetMethodID (env, clsstring, "getBytes ", "(Ljava/lang/String;) [B"); // [String. getBytes ("gb2312"); 17 jbyteArray barr = (jbyteArray) (* env)-> CallObjectMethod (env, jstr, mid, strencode); // String. getByte ("GB2312"); 18 jsize alen = (* env)-> GetArrayLength (env, barr); // The length of the byte array 19 jbyte * ba = (* env) -> GetByteArrayElements (env, barr, JN I _FALSE); 20 if (alen> 0) 21 {22 rtn = (char *) malloc (alen + 1); // "\ 0" 23 memcpy (rtn, ba, alen); 24 rtn [alen] = 0; 25} 26 (* env)-> ReleaseByteArrayElements (env, barr, ba, 0); // 27 return rtn; 28} 29 30 // logon judgment logic 31 int login (char * username, char * password) 32 {33 char * user = "forsta "; 34 char * pwd = "123456"; 35 int I = 0, j = 0; 36 if (strcmp (user, username )! = 0 | strcmp (password, pwd )! = 0) {37 return 404; 38} 39 return 200; 40} 41 42 JNIEXPORT jint JNICALL Java_com_forsta_login_MainActivity_login43 (JNIEnv * env, jobject obj, jstring username, jstring pwd) {44 char * name = Jstring2CStr (env, username); 45 char * password = Jstring2CStr (env, pwd); 46 return login (name, password); 47 48}

 

Android. mk

1 LOCAL_PATH := $(call my-dir)2 include $(CLEAR_VARS)3 LOCAL_MODULE    := login4 LOCAL_SRC_FILES := login.c5 include $(BUILD_SHARED_LIBRARY)

5. Compile and load library files

Administrator @ lenovo-PC/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

 

Load library files

Static {
System. loadLibrary ("login ");
}

 

6. logon implementation

1 bt. setOnClickListener (new OnClickListener () {2 3 @ Override 4 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 {10 Toast. makeText (getApplicationContext (), "Logon successful, code = 200", 1 ). show (); 11} else {12 13 Toast. makeText (getApplicationContext (), "Logon Failed, code = 404", 1 ). show (); 14} 15 16} 17 });

7. Display Results

 


What programming language is required for Android NDK development?

C or C ++ is used for NDK.

Confusion about android FrameWork Ndk

The FrameWork is an android FrameWork, and the NDK calls its own so, that is, the jni technology in java. java calls c or c ++,
Basically, it doesn't matter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.