国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android Studio中使用cmake開發(fā)JNI實戰(zhàn)

這篇具有很好參考價值的文章主要介紹了Android Studio中使用cmake開發(fā)JNI實戰(zhàn)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

JNI學(xué)習(xí)大綱

一、JNI編程入門

二、Android Studio中使用cmake開發(fā)JNI實戰(zhàn)

第一章節(jié)我們介紹了JNI的開發(fā)步驟,那這一章節(jié)我們就開始在Android Studio中實戰(zhàn)一下吧,Let's Start。

1. Android Studio中安裝CMake插件

  • AS中菜單欄選擇Tools>SDK Manager
  • 在Android SDK中選擇SDK Tools,安裝CMake和NDK。

Android Studio中使用cmake開發(fā)JNI實戰(zhàn),JNI開發(fā),Android,android studio,c++,android

2. JNI開發(fā)

2.1 編寫JNI代碼

在項目工程下的src/main創(chuàng)建cpp目錄,編寫native-lib.cpp(JNI代碼實現(xiàn)文件)和對應(yīng)的CMakeLists.txt(JNI代碼編譯配置)。文章來源地址http://www.zghlxwxcb.cn/news/detail-628628.html

// native-lib.cpp

#include <jni.h>
#include <android/log.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_jni_test_JNITestService_stringByJNI(
        JNIEnv *env, jobject /* this */) {
    std::string hello = "hello JNI from C++";
    return env->NewStringUTF(hello.c_str());
}
// CMakeLists.txt

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.
project("jnitest")

# 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.
set(project_root_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
include_directories(${project_root_dir}/common)

add_library( 
    # Sets the name of the library.
    jnitest

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).
    native-lib.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.
    jnitest
    
    # Links the target library to the log library included in the NDK.
    ${log-lib}
)

2.2 gradle中編譯配置

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            // 設(shè)置生成so的arm架構(gòu)
            cmake {
                cppFlags ''
                abiFilters 'arm64-v8a'
            }
        }
    }

    externalNativeBuild {
        cmake {
            // 編譯
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
}

到了這里,關(guān)于Android Studio中使用cmake開發(fā)JNI實戰(zhàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包