NDK开发 第一章 Hello-jni

   本章所学

  • 如何配置cmake
  • Jni读取运行时的CPU型号
  • java调用native方法

代码来源

googlesamples/android-ndk: Android NDK samples with Android Studio
https://github.com/googlesamples/android-ndk

 

1、在build.gradle中配置cmake

 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId 'com.example.hellojni'
        minSdkVersion 20
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    //cmake 相关配置
    externalNativeBuild {
        cmake {
            version '3.10.2'
            path "src/main/cpp/CMakeLists.txt"
        }
    }
//此处打开的话,生成多个apk,一个apk中只含有一种so
//    flavorDimensions 'cpuArch'
//    productFlavors {
//        arm7 {
//            dimension 'cpuArch'
//            ndk {
//                abiFilter 'armeabi-v7a'
//            }
//        }
//        arm8 {
//            dimension 'cpuArch'
//            ndk {
//                abiFilters 'arm64-v8a'
//            }
//        }
//        x86 {
//            dimension 'cpuArch'
//            ndk {
//                abiFilter 'x86'
//            }
//        }
//        x86_64 {
//            dimension 'cpuArch'
//            ndk {
//                abiFilter 'x86_64'
//            }
//        }
//        universal {
//            dimension 'cpuArch'
//            // include all default ABIs. with NDK-r16,  it is:
//            //   armeabi-v7a, arm64-v8a, x86, x86_64
//        }
//    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}

2、CMakeLists.txt 编写

NDK开发 第一章 Hello-jni_第1张图片

cmake_minimum_required(VERSION 3.4.1)

add_library(hello-jni SHARED
            hello-jni.cpp)

# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
                      android
                      log)

 

3、编写hello-jni.cpp

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include 
#include 

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   hello-jni/app/src/main/java/com/example/hellojni/HelloJni.java
 */
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{//判断CPU架构型号
#if defined(__arm__)
    #if defined(__ARM_ARCH_7A__)
    #if defined(__ARM_NEON__)
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a/NEON (hard-float)"
      #else
        #define ABI "armeabi-v7a/NEON"
      #endif
    #else
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a (hard-float)"
      #else
        #define ABI "armeabi-v7a"
      #endif
    #endif
  #else
   #define ABI "armeabi"
  #endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
    //C++写法
    return env->NewStringUTF("Hello from JNI c++ LIBS!" ABI ".");
    //C的写法,此处使用会报错,用在.c文件中
//    return (*env)->NewStringUTF(env, "Hello from JNI c !  Compiled with ABI " ABI ".");
}

 

你可能感兴趣的:(ndk)