JNI交叉編譯第一篇之輸出helloWorld(android studio版)

來源:互聯網
上載者:User

標籤:

一.關于于NDK1.JNI簡介

  1.jni是Java native interface的縮寫,java 本地介面。它提供了若干的API實現了Java和其他語言的通訊(主要是C/C++)。從Java1.1開始,jni標準成為java平台的一部分,它允許Java代碼和其他語言寫的代碼進行互動。
  2.ndk:Android NDK 是在SDK前面又加上了“原生”二字,即Native Development Kit,因此又被Google稱為“NDK”。
  3..so:共用函數庫,在可執行程式啟動的時候載入,所有程式重新運行時都可自動載入共用函數庫中的函數。

 4.為何要使用ndk?

     1. 代碼的保護,由於apk的java層代碼很容易被反編譯,而C/C++庫反匯難度較大。     2. 在NDK中調用第三方C/C++庫,因為大部分的開源庫都是用C/C++代碼編寫的。     3. 便於移植,用C/C++寫的庫可以方便在其他的嵌入式平台上再次使用。
android studio下配置NDK環境

1.首先下載ndk包
2.進入菜單右上方file->Project Structure:進入下面介面,在最下面Android ndk location選擇本地ndk的目錄點擊OK


3.建立項目後,選擇file->new ->Floder->jni Folder:
這時java目錄下會出現一個jni檔案夾
4.配置本地項目的Build.gradle,選中Module下的Build.gradle,添加如下內容

apply plugin: ‘com.android.application‘android {    compileSdkVersion 23    buildToolsVersion "23.0.3"    defaultConfig {        applicationId "com.example.lancao.myhellojni"        minSdkVersion 15        targetSdkVersion 23        versionCode 1        versionName "1.0"        //在此處添加配置        ndk{            moduleName "lancao"            ldLibs "log","z","m"            abiFilters "armeabi","armeabi-v7a","x86"        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }}dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    testCompile ‘junit:junit:4.12‘    compile ‘com.android.support:appcompat-v7:23.3.0‘}

5.靜態載入動態庫,編寫naive方法,和普通java方法基本沒區別。

package com.example.lancao.myhellojni;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView textView = (TextView) findViewById(R.id.text);        textView.setText(getStringFromNative());    }    //靜態載入動態庫    static {        System.loadLibrary("lancao");    }//申明本地方法    public   native String getStringFromNative();}

6.產生標頭檔。在android studio 的命令列介面中,進入/app/src/main/java目錄下,執行命令:

javah -d  ../jni com.example.lancao.myhellojni.MainActivity

這樣就在src/main/目錄中新增了jni目錄,以及com_example_lancao_myhellojni_MainActivity.h標頭檔。

7.com_example_lancao_myhellojni_MainActivity.h的具體內容為

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_lancao_myhellojni_MainActivity */#ifndef _Included_com_example_lancao_myhellojni_MainActivity#define _Included_com_example_lancao_myhellojni_MainActivity#ifdef __cplusplusextern "C" {#endif#ifdef __cplusplus}#endif#endif

8.建立c本地檔案

//匯入本地的庫檔案,必須添加#include <android/log.h>#include <jni.h>JNIEXPORT jstring JNICALL Java_com_example_lancao_myhellojni_MainActivity_getStringFromNative(JNIEnv * env , jobject j){//返回一個字串“hello from jni !”    return (*env)->NewStringUTF(env,"Hello From JNI!");}

9.這樣就完成了交叉編譯之輸出helloWorld了

JNI交叉編譯第一篇之輸出helloWorld(android studio版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.