First, the preparatory work
Download NDK:http://dl.google.com/android/repository/android-ndk-r12b-windows-x86_64.zip
Second, the Android studio environment configuration
Path: file->setting-> Look at the picture, there is an ndk to tick on.
Third, the new project, look at the picture
Iv. Configuring the NDK environment
The above three steps, after the completion of the project, will give you to build a Demo,demo in the CPP, you can directly run one side, feel a bit. Then, a face of the crazy, wrong ........
Look at the solution below.
Unzip the step by one of the downloaded compressed packages. I put it near the as directory. Just put it, you can find it.
This configures the environment.
Five, the demo run a run, can be done .....
There is always a restless heart, do not modify how can be done.
The demo is to return a string, I have to make a return int type, but also write a function body, run.
Then, a native method is written directly in the mainactivity. The corresponding method is generated in CPP. Then, the function body is called. Run for a moment and immediately error
for ...... .....
What's the situation? The function body has been declared. As no error and warning, a run error.
Then give the correct code, and you understand it yourself:
#include <jni.h>#include<string>extern "C"{intcount (); Jniexport jstring Jnicalljava_com_ailin_shoneworn_ndkdemo_mainactivity_stringfromjni (JNIEnv*Env, Jobject/* This*/) {std::stringHello ="Hello from C + +"; returnEnv->Newstringutf (Hello.c_str ());}intcount () {intA =1; intb=2; returnA +b;} Jniexport jint Jnicalljava_com_ailin_shoneworn_ndkdemo_mainactivity_intfromjni (jnienv*Env, jobject instance, Jint x, Jint y) { //TODO returncount ();}}
Summary: C + + has been too long useless, a lot of things have been forgotten. Hey!
Above, only need to add {} In the function body outside extend "C", the two function body is enclosed together is good.
Android Studio calls you NDK development environment building and basic use