Android ndk Development

Source: Internet
Author: User

Android ndk Development

I do not have any foundation for C/C ++. Even though I have learned C for one semester, I have returned it to my teacher. This article is my accumulated knowledge of an NDK project. It can be said that it is an article for beginners.

1. Preparation

For NDK development, pay attention to the development environment and version (I think ). I am using Eclipse (Luna 4.4.0) and NDK version r10. It should be in <= r6 NDK and cygwin needs to be installed. (I will not discuss it here. There is a lot of information on the Internet ), the download link of the NDK can be attached to a friend who cannot go through the wall. It is 32-bit and 64-bit, and the website cannot be accessed. However, the download link can be directly used to download the link.

After the NDK is installed, You need to configure it on Eclipse.

1. For windows-preferences-android-ndk-ndklocation, select the ndk installation directory.

2. Configure the builder Environment Right-click the project to be developed by ndk-properties-builder-new-program and configure the following in the pop-up window that appears.

 

3. Right-click the Android tools-Add Native Support project

The jni directory and Android. mk, xx. cpp are displayed, indicating that the jni directory is successfully added. Now we can develop NDK.

 

2. Java calls methods in C ++.

For example, getting strings from the C ++ file and printing them

In Activity Class:

1 2 3 4 5 6 7 8 9 10 11 static{ System.loadLibrary("xx");// Xx is the LOCAL_MODULE field in the Android. mk file. } // The method name must be the same as the method name in ndk. public native String getString(); // Onclick Method public void click(View v){ String str = getString(); System.out.println("Call methods in JNI :"+str); }

In ndk. cpp

1 2 3 4 5 6 7 8 9 10 11 #include #include /** * Extern "C" must be added. If no value is added, the method call fails and no answer is found. If you know what the call is, please, the method name must comply with JNI specifications. The Java _ package name_classname_method names must be separated. */ extern "C" jstring Java_com_test_ndk_A_getString(JNIEnv* env,jobject thiz){ jstring str ; // In C, it is called (* env)->. This is also the case for most blog documents on the Internet. str = env->newStringUTF("hello world");// You cannot use Chinese characters. Otherwise, an error is returned. return str; }

Run the project to see the effect.

3. Call the Java method in C ++

In ClassB

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 static{ System.loadLibrary("ndk"); } public native void loadJavaMethod(); public void f1(){ String str ; str = "hello world from java"; System.out.println(str); } public String f2(){ String str ; str = "f2: hello world from java"; System.out.println(str); return str; } public void f3(String str,int i){ System.out.println("F3 content :"+str+", Number :"+i); }

In ndk. cpp

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 #include #include extern "C" void Java_com_test_ndk_B_loadJavaMethod(JNIEnv *env,jobject thiz){ // Call a method without parameters or return values jclass cls = env->GetObjectClass(thiz); // GetMethodID ("jclass object", "method name", "method parameter ") jmethodID mID = env->GetMethodID(cls,"f1","()V"); if(mID != NULL){ env->CallVoidMethod(thiz,mID); } // Call the method with No Response parameters // Except the basic data type, the parameter type must follow this format. // L package name/class name; package name separated by/, must end; // For details, refer to this article jclass cls = env->GetObjectClass(thiz); jmethodID mID = env->GetMethodID(cls,"f3","(Ljava/lang/String;I)V"); jstring content = env->NewStringUTF("hehe"); jint i = 10; if(mID != NULL){ env->CallVoidMethod(thiz,mID,content,i); } // Call a method with a returned value and no parameters jclass cls = env->GetObjectClass(thiz); jmethodID mID = env->GetMethodID(cls,"f2","()Ljava/lang/String;"); if(mID != NULL){ env->CallObjectMethod(thiz,mID); } // Call the methods in other classes. assume there is A Student class. To use the internal class A in the Student class, the format is //com/test/ndk/Student$A jclass stu = env->FindClass("com/test/ndk/Student"); // Instantiate the construction method without Parameters jobject stuObj = env->NewObject(stu,env->GetMethodID(stu," " ,"()V")); jmethodID getNameId = env->GetMethodID(stu,"getName","()V"); if(getNameId !=NULL){ env->CallVoidMethod(stuObj,getNameId); } }

4. Call third-party so files in your own so files

This is generally because the method in the third-party C/C ++ is not written in accordance with the JNI specification. In this case, it needs to be repackaged and used, of course, the premise is that there is a third-party instruction document.

Configure third-party so files to the pre-compiled Environment

Create a prebuilt folder under the jni file of the project (with a random name) and put the third-party so file in it, for example, libthird. so. Then create the Androdid. mk file under this folder. The file content is

1 2 3 4 5 6 7 8 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libthird LOCAL_SRC_FILES := libthird.so include $(PREBUILT_SHARED_LIBRARY)

Open the Android. mk file under jni and add the following fields

1 2 3 4 5 6 7 8 9 10 11 12 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ndk LOCAL_SRC_FILES := ndk.cpp # The name is the same as the name defined by LOCAL_MODULE in the third-party mk. LOCAL_SHARED_LIBRARIES := libthird include $(BUILD_SHARED_LIBRARY) # Add a path include $(LOCAL_PATH)/prebuilt/Android.mk

After the configuration is complete, the third-party so file can be viewed in the libs \ armeabi directory of the project. Note that the third-party so file cannot be directly put in this directory. Otherwise, it will be deleted during builde.

Suppose there is such a method in a third-party C/C ++ file.

1 2 3 4 // Extern "C" must also be added to a third-party package. during testing, if no method is added, the call is unsuccessful, but this cannot be made to a third party, to be resolved extern "C" int f1(){ return 101; }

Call a third-party method in your own C/C ++ File

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include #include #include void *filehandle = NULL; jstring (*f1)() =NULL; extern "C" jint Java_com_test_ndk_classA_f1(JNIEnv * env,jobject thiz){ jint i ; filehandle = dlopen("/data/data/com.fly.ndk2/lib/libndk.so", RTLD_LAZY); if(filehandle){ f1 = (int(*)())dlsym(filehandle,"f1"); } if(f1){ i = f1(); dlclose(filehandle); filehandle = NULL; } return i; }

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.