안드로이드 스튜디오 的 NDK

머리말

안드로이드 스튜디오에서이 도입 구성 튜토리얼 NDK

본문

1. 다운로드 및 CMake의 NDK

다운로드가 느린 경우, 별도의 다운로드로 이동 할 수 있습니다


의 image.png

2. SDK 구성 및 NDK

이것은 NDK를 다운로드하기 전에 NDK 주소입니다


의 image.png

당신이 내부의 SDK 위치에 경로 SDK를 설정하고 NDK 경우, 구성하지 local.properties, 그것은 자동으로 구성 할, 또는 우리는 경로 관련 SDK를 구성해야하고 local.properties에 NDK

의 image.png

구성 파일을 Gradle.properties

android.useDeprecatedNdk=true


20180102171419.png Sogou는 스크린 샷

3. JNI 디렉토리를 구축

JNI는 직접 jniLibs 폴더를 만들 수 있습니다
搜狗截图20180102170250.png

구성 빌드

MODULENAME의 NDK 구성, 라이브러리의 이름이 생성됩니다 그래서 LIB + daoshuriwidget + .so를

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


搜狗截图20180102170716.png

JNI 및 jniLibs 구성 디렉토리, JNI에서 C 파일, 결과 jniLibs에 도서관에서 너무

搜狗截图20180102171024.png

원주민 방법을 쓰기 5.

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

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

6. .H 파일을 생성

AS하면 디렉토리에 터미널 응용 프로그램 다음 CD를 찾을 수

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


搜狗截图20180102172333.png

루트 디렉토리에 생성 된 .H 파일

搜狗截图20171225232725.png

당신은 디렉토리 JNI에서, 다음 생성 된 .H 파일을 다음 개발 -d로 가기 위해 다음 파일 멤버의 파일을 .H 생성하려면

搜狗截图20171225233855.png

7. 시작 C를 작성 ++ 코드

는 C ++ 코드 아래 JNI 새 폴더
수정 아래에 이름이있는 .h 및 JNI 폴더를 .H 복사
.H 생성 ++ 방법 C로 파일을 복사

//.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

새로운 .H 생성 방법 매개 변수 이름 때문에 수동으로 새 상원의 이름을 추가 할 수 있도록

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

C에서 가져 오기 .H 파일 ++ 아래 폴더


搜狗截图20180102174028.png

(Common.h 파일에이 헤더 파일) 관련 헤더 파일 가져 오기

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

라이브러리 jniLibs 복사하도록 제 라이브러리 때문에 컴파일

컴파일 컴파일 버튼 또는 Ctrl + F9를 클릭, 그래서 라이브러리 생성


搜狗截图20180102173230.png


搜狗截图20180102172916.png

이 때문에 라이브러리 (그 폴더와 함께) jniLibs에 복사하기로 합의

搜狗截图20180102173334.png

9. JNI 방법, 프로젝트를 실행

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

다음 NDK 더블 데몬 프로세스를 소개 더블 가드 과정을 NDK

추천

출처blog.csdn.net/k393393/article/details/78954115