Android Studio NDK Configuration and NDK development

Source: Internet
Author: User

To configure the development environment:

1: Download NDK, import into Android studio.

  

2: Introducing the NDK into the project

  

3: Import the NDK path in the computer's path variable, which will be used when compiling the. h file.

  

One: Build the Java native method, then compile and build the. class file.

Two: Generate. h files from the. class file.

1: Open terminal, then go to main home directory under the app directory.

2: Enter the command.

  

  3: Generate the. h file, the resulting JNI folder under the main folder.

  

 Three: Write a. c file according to the. h file .

 #include <string.h>

#include <jni.h>

Jstring Java_com_xingdongmei_media_firstjni_getfromname (jnienv *env, Jobject ob) {

Return (*ENV)->newstringutf (env, "Hello from JNI!");
}

void Java_com_xingdongmei_media_firstjni_setage (jnienv *env, Jobject ob, Jfloat age) {
printf ("asdf!");
}

Call the Getfromname method and return "Hello from JNI!"

IV: Create a new application.mk file and a android.mk file under the Jni folder as follows

APPLICATION.MK file

App_stl: = gnustl_static
App_cppflags: =-frtti-fexceptions
App_abi: = x86 Armeabi armeabi-v7a
App_platform: = android-8

ANDROID.MK file

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module : = Firstjni
Local_src_files: = Firstjni.c
Include $ (build_shared_library)
The function of the specific field:--Local_path: Represents the directory where the Mk file resides;   --Include $ (clear_vars): Compile tool function, through which some initialization operations can be performed;   --Local_module: What is the name of the compiled. so suffix file;   --Local_src_files: Specifies the name of the compiled source file; --Include $ (build_shared_library): tells the compiler to generate a dynamic library;

Five: Enter the Jni folder, enter Ndk-build in the Terminal Command window, and then generate the Libs folder and the Obj folder under the main folder.

  

 Six: Configure the path of the. so file in Build.gradle. Mine is:

Sourcesets.main {
Jnilibs.srcdir ' Src/main/libs '
}

Seven: Running results:
  

  


Eight: Common mistakes

1: If only c/cpp files, the direct compilation of this, will be error, this should be Android studio in this case automatically generated android.mk when a bug, see compile errors.

Error:execution failed for task ': App:compiledebugndk '.
> com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:Process ' command ' G:\android-ndk\android-ndk-r10\ndk-build.cmd ' finished with Non-zero exit value 2

Workaround:

   在jni文件下建一个空的empty.c文件 编译运行即可。

2: Cannot find the. so file.

10-08 03:03:12.100 11948-11948/? E/dalvikvm:dlopen ("native/libfirstjni.so") failed:dlopen failed:library "native/libfirstjni.so" not Found
10-08 03:03:12.110 11948-11948/? E/crashhandlerutils:java.lang.unsatisfiedlinkerror:dlopen failed:library "native/libfirstjni.so" not Found

Workaround:

Add the path to the. So ancestor directory file in the Build.gradle in the project's app directory.

My. So file ancestor directory structure

  

The. So file path for my build.gradle configuration:

Sourcesets.main {

    Jnilibs.srcdir ' Src/main/libs '
}

Android Studio NDK Configuration and NDK development

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.