Android-studio 的 NDK

Foreword

This introduction configuration tutorial ndk under Android-studio

text

1. Download and CMake ndk

If the download is slow, you can go to download a separate


image.png

2. sdk configuration and ndk

This is ndk address before downloading the ndk


image.png

If you set the path sdk and ndk in the SDK location inside, then do not local.properties in the configuration, it automatically configures, or we need to configure route related sdk and ndk in local.properties

image.png

Gradle.properties configuration file

android.useDeprecatedNdk=true


Sogou screenshots 20180102171419.png

3. Establish jni directory

Jni can create a folder directly and jniLibs
搜狗截图20180102170250.png

Configuring build

Ndk configuration of moduleName, the name of the library is generated so lib + daoshuriwidget + .so

 ndk {
            moduleName "daoshuriwidget"
            abiFilters "armeabi", "x86","armeabi-v7a"
            ldLibs "log"
        }


搜狗截图20180102170716.png

Jni and jniLibs configuration directory, c files in jni, the resulting so in the library on jniLibs

搜狗截图20180102171024.png

5. Write the Native Method

public class LittleWidgetWatcher {
    static {
        System.loadLibrary("daoshuriwidget");//导入so库
         //导入在jniLibs下面的so库,名称和build中配置moduleName名称相同
    }

   //此方法就是用于调用c++的方法,要加上native
    public native void openLittleWidgetWatcher(int uId);
}

6. generate .h file

AS find the Terminal app and then cd into the directory

输入javah -classpath build/intermediates/classes/meizu/debug -jni com.pybeta.daymatter.service.LittleWidgetWatcher


搜狗截图20180102172333.png

Generated .h file in the root directory

搜狗截图20171225232725.png

If you want to generate .h files in the following file members in order to develop then top with -d, then the generated .h file in the directory jni

搜狗截图20171225233855.png

7. start writing C ++ code

Jni new folder below the C ++ code
modifications .h name and copy .h jni folder below to
copy the file to the C ++ method to generate .h

//.h文件夹生成的代码
#ifndef _Included_com_fanao_daoshuri_ndkprotect_LittleWidgetWatcher
#define _Included_com_fanao_daoshuri_ndkprotect_LittleWidgetWatcher
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_fanao_daoshuri_ndkprotect_LittleWidgetWatcher
 * Method:    openLittleWidgetWatcher
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_com_fanao_daoshuri_ndkprotect_LittleWidgetWatcher_openLittleWidgetWatcher
  (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

Because no new .h generated method parameter name, so you want to manually add the name of the new Senate

//c++下面的代码
JNIEXPORT void JNICALL
Java_com_fanao_daoshuri_ndkprotect_LittleWidgetWatcher_openLittleWidgetWatcher(JNIEnv *env,jobject instance, jint userId) {
//父进程 也就是小插件的进程
     user_id = userId;
/* sigaction用于信号处理,sa.sa_flags=SA_RESTART:使被信号打断的系统调用自动重新发起
 信号处理交给sig_handler处理的,当子进程挂了的时候会向其父进程发送一个SIGCHLD信号,
 父进程就会收到SIGCHLD信号,并且开始执行sig_handler方法,重生成子进程*/
     LOGI("开启进程\n");
     struct sigaction sa;
     sa.sa_flags = 0;
     sa.sa_handler = sig_handler;
     sigaction(SIGCHLD, &sa, NULL);
     create_child();
}

Import .h file in c ++ folder below


搜狗截图20180102174028.png

Import the relevant header files (this header file in Common.h file)

#include <signal.h>
#include <sys/wait.h>
#include <android/log.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <stdlib.h>
#include <linux/signal.h>
#include <android/log.h>
#define LOG_TAG "widget"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
void create_child();
void child_start_monitor();

8. The library compiled so, so the library to copy jniLibs

Click the Compile button or Ctrl + F9 to compile, generate so library


搜狗截图20180102173230.png


搜狗截图20180102172916.png

These so library (along with its folders) agreed to be copied to the jniLibs

搜狗截图20180102173334.png

9. Use jni method, run the project

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        Intent intent = new Intent(this, LittleWidgetService.class);
        startService(intent);
    }

Next introduce ndk double daemon process ndk double guard process

Guess you like

Origin blog.csdn.net/k393393/article/details/78954115