Hello world for android ndk Development

Source: Internet
Author: User

Hello world for android ndk Development

I don't know why android's ndk development materials are rare on the Internet. There are still materials that basically cannot complete the entry-level learning demo and can't be run at all. The younger brother spent three days and three nights to get helloworld, extreme insomnia.

At the beginning, we configured the ndk development Phantom on android studio, and then developed hello world. However, android studio supports ndk development relatively late and is not as mature as elipse, I encountered many problems during compilation and finally gave up (I know this is not a good habit, but I will return later)

Note: configuration of ndk on gradle

 

Here, I am nagging that using android studio makes it necessary to enhance my understanding of gradle.

Start ndk development on eclipse:

Description 1. Create an empty android Project

2. Write a local class and define an empty local method. For example, public static native String getStr ();

3. switch to the src directory of the Project. my options are home/dlzh/workspace/HelloNDK/src, and then run javah-jni com. example. hellondk. getString, and then generate. h file

4. native Method for writing HelloNdk. c files

5. Load and run the so file.

The detailed steps are as follows:

1. Create an empty android Project

2. Compile a local GetString. class

 

Public class GetString {// local code implements public static native String getStr (); // defines the local method, implemented by C language}
3. Right-click the project name and select "build project". The jni and libs folders are automatically generated under the project, right-click the project, and choose Android tools --> add native support.

 

4. Switch ubuntu to the src directory of the project and runJavah-jni com. example. hellondk. GetString command, press Enter. Copy the generated com_example_hellondk_GetString.h to the jni File

5. Open the HelloNdk. c file in the jni folder and implement the header file lifecycle method.

 

# Include
 
  
# Include
  
   
/** Header file method */JNIEXPORT jstring JNICALL Java_com_example_hellondk_GetString_getStr (JNIEnv * env, jclass) {return env-> NewStringUTF (static method call hello Ndk !);}
  
 


 

Add the following code under GetString. java to load the so file.

 

  static{System.loadLibrary(HelloNDK);}

6. Then
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.textview);tv.setText(GetString.getStr());}}

 

 

 

 

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.