AndroidStudio JNI+Gradle 3.0 이상 JNI 구덩이 오 르 기 여행

11188 단어 AndroidStudioJNI
1.우선 제니 는 무엇 일 까요?
JNI―(Java Native Interface),그 는 자바 플랫폼 의 특성 이지 안 드 로 이 드 시스템 이 제공 하 는 것 이 아니다.그 는 개발 자가 이 함수 들 을 호출 하여 자바 코드 호출 C/C+코드 를 실현 할 수 있 도록 JNI 함 수 를 정의 했다.
2.JNI 는 어떻게 사용 하나 요?
우 리 는 먼저 작성 한 C/C+코드 를 대응 하 는 플랫폼 의 동적 라 이브 러 리 로 컴 파일 합 니 다(windows 는.dll 파일 이 고 Liux 는.so 파일 입 니 다).
다음은 밤 을 들 겠 습 니 다.AndroidStudio 로 JNI 를 실현 하 겠 습 니 다.
3.JNI 가 NDK 를 먼저 다운로드 하 는 것 을 실현 하려 면 NDK 는 무엇 일 까요?면접 보전 이 왔 으 니 서둘러 공책 을 꺼 내 라)
  • NDK 는 일련의 도구 의 집합
  • NDK 는 안정 적 이 고 기능 이 제 한 된 API 헤더 파일 성명
  • 을 제공 했다.
  • NDK 의 발 표 는'자바+C'의 개발 방식 을 마침내 바 꾸 어 공식 적 으로 지원 하 는 개발 방식
  • 이 되 었 다.
  • NDK 는 Android 플랫폼 이 C 개발 의 시작 을 지원 하도록 합 니 다
  • 자,그럼 다음은 NDK 를 다운로드 하 겠 습 니 다.두 가지 방법 이 있 습 니 다.
  • Google 공식 다운로드 NDK:다운로드 클릭
  • SDK Manager 를 통 해 NDK:
  • 다운로드
     
    4.내 려 오 는 우리 의 새로운 프로젝트:이 프로젝트 는 하나의 MainActivity 만 포함 합 니 다.
     
    5.NDK 다운로드 가 다 되 었 는 지 확인 해 보 겠 습 니 다.어떻게 검사 해 야 하나 요?다음 과 같다.
    SDK Location 의 NDK 경 로 를 확인 합 니 다:
     
    local.properties 파일 에 NDK 경로 가 있 는 지 확인 합 니 다.
     
    6.내 려 오 면 JNI 인 터 페 이 스 를 작성 해 야 합 니 다.다음 과 같 습 니 다.
    JNI 인 터 페 이 스 는 native 키워드 로 수식 해 야 합 니 다.방법 명 보 홍 을 볼 수 있 습 니 다.괜 찮 습 니 다.계속 하 겠 습 니 다.
     
    7.프로젝트 를 빌 드 하고 my JNIUtils.java 컴 파일 후 class 파일 이 생 성 되 었 는 지 확인 합 니 다.이 위치 에서:
    AndroidJNITest/app/build/intermediates/classes/debug/com/kissdream/androidjnitest/myJNIUtils.class
     
    8.javah 를 사용 하여.h 헤더 파일 을 생 성 합 니 다.구체 적 으로 다음 과 같 습 니 다.
    Terminal 을 열 고 debug 디 렉 터 리 에 명령 을 입력 하 십시오.명령 은 다음 과 같 습 니 다.
    cd/Users/apple/Desktop/AndroidJNITest/app/build/intermediates/classes/debug
    그리고 javah+패키지 이름+파일 경 로 를 사용 하여 헤더 파일 을 생 성 합 니 다.명령 은 다음 과 같 습 니 다.
    javah com.kissdream.androidjnitest.myJNIUtils
     
    헤더 파일 이 생 성 되 었 는 지 확인 하기:
    이 경로 아래 에.h 파일 이 하나 더 있 는 것 을 발 견 했 습 니 다.AndroidJNITest/app/build/intermediates/classes/debug/com/kissdream
    하하,맞아요.이게 저희 가 만 들 헤더 파일 이에 요.
     
    9..h 파일 을 생 성 한 것 은 아직 안 됩 니 다.방법 만 밝 혔 을 뿐 우 리 는 그것 을 실현 해 야 합 니 다.그러면 어떻게 그 를 실현 할 수 있 습 니까?다음 과 같 습 니 다.
    -main 다음 에 jni 폴 더 를 새로 만 듭 니 다.그림 참조:
     
     
    .h 파일 내용 은 다음 과 같 습 니 다.
    
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_kissdream_androidjnitest_myJNIUtils */
    
    #ifndef _Included_com_kissdream_androidjnitest_myJNIUtils
    #define _Included_com_kissdream_androidjnitest_myJNIUtils
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:   com_kissdream_androidjnitest_myJNIUtils
     * Method:  getName
     * Signature: ()Ljava/lang/String;
     */
    JNIEXPORT jstring JNICALL Java_com_kissdream_androidjnitest_myJNIUtils_getName
     (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
    
    생 성 된.h 파일 을 jni 폴 더 에 복사 합 니 다.
    jni 폴 더 아래 에.c(c 언어)나.cpp(c++)파일 을 새로 만들어 서.h 파일 에서 설명 하 는 방법:
    새 c++파일 에.h 파일 에 설명 하 는 방법 을 복사 한 다음 파일 에.h 파일 을 가 져 옵 니 다:
    .h 파일 도입\#include"comkissdream_androidjnitest_myJNIUtils.h"
    
    #include "com_kissdream_androidjnitest_myJNIUtils.h"
    JNIEXPORT jstring JNICALL Java_com_kissdream_androidjnitest_myJNIUtils_getName
        (JNIEnv * env, jobject obj){
    //    C          
    //  return (*env)->NewStringUTF(env,"Kiss dream");
      //    C          
    return env->NewStringUTF((char *)"Kiss dream");
    }
     
    여기까지 저희 의 방법 은 이 루어 졌 습 니 다.
    10.방법 은 우리 가 실현 했다.그러나 우 리 는 어떻게 호출 할 것 인가?조급해 하지 마라.Follow me:
    우선 동적 라 이브 러 리 도입:
    
    public class myJNIUtils {
      static {
        //    ,     build.gradle ndk         
        System.loadLibrary("NameProvider");
      }
      //JNI     native     
      public native String getName();
    }
    Name Provider 는 d.so 파일 을 만 들 파일 이름 입 니 다.
    다음은 우리 가 그것 을 호출 할 것 이다.
     
    11.가장 중요 한 단계 가 왔 습 니 다.so 파일 생 성:
    이 편집장 도 할 줄 몰라 서 바 이 두 에 가서 결 과 를 얻 었 다.
    루트 디 렉 터 리 gradle.properties 에 추가:
    android.useDeprecatedNdk=true 는 낮은 버 전의 NDK 사용 을 허용 한 다 는 뜻 입 니 다.
    module 아래 build.gradle 에 ndk 노드 를 추가 하면 다음 그림 과 같 습 니 다.
    
    ndk {
        moduleName "NameProvider"
      }
    
    NameProvider 이 이름 은 동적 라 이브 러 리 를 도입 한 이름과 같 아야 합 니 다.
    이 두 단계 가 필요 하면 so 파일 생 성 을 실행 할 수 있 습 니 다.
    그러나 생각 보다 순 조 롭 지 않 았 습 니 다.잘못 보 고 했 습 니 다.저 는 갑자기 마음 속 에 만 마리 의 진흙 말 을 날 아가 로그 에 올 랐 습 니 다.
    
    Error:Execution failed for task ':app:compileDebugNdk'.
    > Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
     Consider using CMake or ndk-build integration. For more information, go to:
      https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
      To get started, you can use the sample ndk-build script the Android
      plugin generated for you at:
      /Users/apple/Desktop/AndroidJNITest/app/build/intermediates/ndk/debug/Android.mk
     Alternatively, you can use the experimental plugin:
      https://developer.android.com/r/tools/experimental-plugin.html
     To continue using the deprecated NDK compile for another 60 days, set 
     android.deprecatedNdkCompileLease=1512283120054 in gradle.properties
    누 나 를 아무리 생각해 도 바 이 두 의 답 은 모두 이렇게 하 는 거 야.왜 남 들 이 내 것 을 할 수 있 으 면 안 되 지?내 코드 는 그 와 똑 같 아.
    왜 남 들 이 내 것 을 할 수 있 으 면 안 되 지?내 코드 는 그 사람과 똑 같 잖 아.이 말 은 프로그래머 로 서 우 리 는 잘 알 고 있어!내 가 포기 할 만큼 힘 들 어?no no,프로그래머 인 내 가 어떻게 쉽게 포기 할 수 있 겠 어!모든 사람 은 이런 경험 을 가지 고 있다.파란색 과 마른 표고버섯 이 있 었 고 마지막 에 우 리 는 우리 의 잘못 을 찾 았 다.
    자,Log 를 자세히 살 펴 보 겠 습 니 다.
  • android.useDeprecatedNdk 는 더 이상 지원 하지 않 습 니 다
  • CMake or ndk-build 사용
  • 그리고 링크
  • CMake 나 ndk 를 사용 하여 통합 을 구축 하 는 것 을 고려 합 니 다.더 많은 정 보 를 알 고 싶 으 시 면 방문 하 십시오.
    https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
    우선,Android 의 ndk 를 사용 하여 스 크 립 트 예제 플러그 인 을 만 들 수 있 습 니 다.
    /Users/apple/Desktop/AndroidJNITest/app/build/intermediates/ndk/debug/Android.mk
    또는 실험 플러그 인 을 사용 할 수 있 습 니 다:
    https://developer.android.com/r/tools/experimental-plugin.html
    버 려 진 NDK 컴 파일 을 60 일 동안 계속 사용 하고 설정 합 니 다.
    gradle.properties 에서
    android.deprecatedNdkCompileLease=1512283120054(이 테스트 는 효과 가 없습니다)
    각종 조사 자 료 를 통 해 원래 gradle 3.0 이상 이전에 이런 방법 은 지원 되 지 않 았 음 을 발견 하 였 다.
    학습 과정 은 상세 하 게 설명 하지 않 고 결 과 를 직접 올 립 니 다.
    먼저 SDK Manager 를 통 해 다운로드:CMake 와 LLDB
     
    build.gradle 의 defaultConfig 노드 에 추가:
    
    //   Cmake  
        externalNativeBuild {
          cmake {
            cppFlags ""
            //       so  
            abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
          }
        }
    build.gradle 의 android 노드 에 추가:
    
    //   CMakeLists.txt  
      externalNativeBuild {
        cmake {
          path "CMakeLists.txt"  //        c    ,     so     
        }
      }
     
    build.gradle 파일 동급 디 렉 터 리 에 CMakeLists.txt 파일 을 추가 합 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
    
    # For more information about using CMake with Android Studio, read the
    # documentation: https://d.android.com/studio/projects/add-native-code.html
    
    # Sets the minimum version of CMake required to build the native library.
    #CMakeLists.txt
    cmake_minimum_required(VERSION 3.4.1)
    
    # Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds them for you.
    # Gradle automatically packages shared libraries with your APK.
    
    add_library( # Sets the name of the library.
          #   so    .
           NameProvider
    
           # Sets the library as a shared library.
           SHARED
           #     so     .
    
           # Provides a relative path to your source file(s).
           #     so     .
           src/main/jni/getName.cpp)
    
    # Searches for a specified prebuilt library and stores the path as a
    # variable. Because CMake includes system libraries in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.
    
    find_library( # Sets the name of the path variable.
           log-lib
    
           # Specifies the name of the NDK library that
           # you want CMake to locate.
           log )
    
    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.
    
    target_link_libraries( # Specifies the target library.
                #      .
                NameProvider
    
                # Links the target library to the log library
                # included in the NDK.
                ${log-lib} )
    
     
    이로써 우리 의 모든 절 차 는 끝났다.다음은 우리 의 성 과 를 검사 하고 기적 을 볼 때 가 되 었 다.
     
    우리 가 so 파일 을 성공 적 으로 만 들 었 음 을 볼 수 있 습 니 다.다음 효과 도 를 보 여 줍 니 다:
     
    다운로드 주소:클릭 하여 다운로드 데모
    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기