Ext.: http://www.2cto.com/kf/201412/361768.html
After you create a new project using Android Sutdio, you then record the basic steps for creating the NDK project. This article will achieve:
1. Create an NDK project
2. Output the log statement in JNI
3. Specify the ABI version of the compiled so library
4. Resolve issues in creating NDK projects
Step:1. Add native interface Note that the native interface and System.loadlibrary () are well written and have no special place
。 P.s:oncreate () executes SetText () in R.id.txt, so you need to modify the XML layout file as normal development steps. Give the code directly as follows:
?
1234567891011121314 |
public class MainActivity
extends Activity{
static {
System.loadLibrary(
"JniTest"
);
}
public native String getStringFromNative();
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtView = (TextView) findViewById(R.id.txt);
txtView.setText(getStringFromNative());
}
}
|
Step:2. Performing Build->make Project
This step executes, verifying that there are no other errors in the project and compiling the project, resulting in a. class file. The generation path of the class file is under App_path/build/intermediates/classes/debug. For example:
Step:3.javah Generating C header files
Click "View->tool windows->terminal", which is the terminal command line tool in Studio. Execute the following command to generate the C language header file. It is important to note that the Javah command is executed to enter the \app\src\main directory, so that the generated. h file is also under the \app\src\main path, which can be seen directly in the engineering structure of studio. Operation command: javah-d jni-classpath <sdk_android.jar>;<app_classes> lab.sodino.jnitest.MainActivity The detailed operation diagram is as follows:
12 |
javah -d jni -classpath c:\Users\sodinochen\AppData\Local\Android\sdk\platforms \android- 16 \android.jar;..\..\build\intermediates\classes\debug lab.sodino.jnitest.MainActivity |
For "Major version 51:50 new, this compiler supports the latest major version" is caused by the installation of two versions of the JDK on the computer, and the current use of the old JDK. When the old JDK is removed and the Java version command is executed to show that the current JDK is the latest 1.7, there is no longer a hint. Such as:
Final build Result: < yo? " http://www.2cto.com/kf/ware/vc/"target=" _blank "class=" Keylink ">vcd48cd48aw1nihnyyz0=" http://www.2cto.com/ Uploadfile/collfiles/20141216/2014121608565552.png "alt=" \ "/>
Step:4. editing C files
Implement the method in the header file in the Main.c file, specifically to return a string directly, and print out the related log using Android_log. The code is as follows:
?
1234567891011121314151617181920212223242526272829303132 |
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include
#ifndef LOG_TAG
#define LOG_TAG
"ANDROID_LAB"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#endif
/* Header for class lab_sodino_jnitest_MainActivity */ #ifndef _Included_lab_sodino_jnitest_MainActivity
#define _Included_lab_sodino_jnitest_MainActivity
#ifdef __cplusplus
extern
"C" {
#endif
/*
* Class: lab_sodino_jnitest_MainActivity
* Method: getStringFromNative
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_lab_sodino_jnitest_MainActivity_getStringFromNative
(JNIEnv * env, jobject jObj){
LOGE(
"log string from ndk."
);
return (*env)->NewStringUTF(env,
"Hello From JNI!"
);
} #ifdef __cplusplus
}
#endif
#endif
</android></jni.h>
|
When we get here, we execute a "build->make Project" and find that "Messages Gradle Build" gives the following hints:
?
1234 |
Error:Execution failed for task ‘:app:compileDebugNdk‘ . > NDK not configured. Download the NDK from http: //developer.android.com/tools/sdk/ndk/.Then add ndk.dir=path/to/ndk in local.properties. (On Windows, make sure you escape backslashes, e.g. C:\\ndk rather than C:\ndk) |
This indicates that the NDK is not configured and requires the NDK path to be configured in the Local.properties file in the project. Well, the hints are clear, so let's go to the next step.
Step:5. Configuring the NDK This step consists of two actions:
1. Specify the NDK path
2. Modify the Build.gradle configuration project in a total of two build.gradle configuration files, we want to modify is in \app\build.gradle this file. Added to the Defaultconfig branch.
?
12345 |
NDK { &NBSP;&NBSP;&NBSP;&NBSP; modulename "Jnitest" &NBSP;&NBSP;&NBSP;&NBSP; ldlibs "log" "Z" "M" &NBSP;&NBSP;&NBSP;&NBSP; abifilters "Armeabi" "armeabi-v7a" "x86" |
The above configuration code specifies the so library name is jnitest, the library to which the link is used, the local_ldlibs in the corresponding android.mk file, and the final output specifies the so library under three ABI architectures. After adding as:
At this point, you can compile the so file by executing "Build->rebuild Project". However, there is a problem on the window platform:
?
1234567 |
Error:Execution failed
for task
‘:app:compileDebugNdk‘
.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
D:\Mission\adt-bundle-windows\ndk-r10b\ndk-build.cmd NDK_PROJECT_PATH=
null APP_BUILD_SCRIPT=C:\Users\sodinochen\AndroidstudioProjects\JniTest2\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-
21 NDK_OUT=C:\Users\sodinochen\AndroidstudioProjects\JniTest2\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=C:\Users\sodinochen\AndroidstudioProjects\JniTest2\app\build\intermediates\ndk\debug\lib APP_ABI=armeabi,armeabi-v7a,x86
Error Code:
2
Output:
make.exe: *** No rule to make target `C:\Users\sodinochen\AndroidstudioProjects\JniTest2\app\build\intermediates\ndk\debug\obj/local/armeabi/objs/JniTest/C_\Users\sodinochen\AndroidstudioProjects\JniTest2\app\src\main\jni
‘, needed by `C:\Users\sodinochen\AndroidstudioProjects\JniTest2\app\build\intermediates\ndk\debug\obj/local/armeabi/objs/JniTest/C_\Users\sodinochen\AndroidstudioProjects\JniTest2\app\src\main\jni\main.o‘
. Stop.
|
This error is baffling. A few times toss, find a video out of the probable cause and solution: The source see YouTube video 02:50 start: https://www.youtube.com/watch?v=okLKfxfbz40#t= 362 a bug in the NDK under Windows, this problem occurs when only one file is compiled, and the workaround is to add an empty util.c file. The compiled library file is exported to the path of studio.
Step:6. Installation Run
Interface:
To view Log printing:
Development of JNI Engineering for Android Studio