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());}}