Android jni helloworld

來源:互聯網
上載者:User

標籤:

建立Android項目,設定布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="調用C函數" /></RelativeLayout>

jni開發的步驟

①寫java代碼 聲明本地方法 用到native關鍵字 本地方法不用去實現

②項目根目錄下建立jni檔案夾

③在jni檔案夾下建立.c檔案

④ 匯入<jni.h>

#include<stdlib.h>#include<stdio.h>#include<jni.h>//JNIEnv* env是JNINativeInterface的二級指標//JNIEnv是JNINativeInterface的一級指標//JNINativeInterface結構體中定義了大量的函數指標,這些函數指標在jni開發中很常用//(*env)->調用結構體中的函數指標//jobject 調用本地函數的java對象,在這個例子中就是MainActivity的執行個體//C本地函數命名規則Java_包名_類名_本地方法名//jstring (*NewStringUTF)(JNIEnv*, const char*)jstring Java_com_wuyudong_jnihello_MainActivity_helloFromC(JNIEnv* env, jobject thiz) {    char* str = "hello from c!";    return (*env)->NewStringUTF(env, str);}

⑤ 建立Android.mk makefile 告訴編譯器.c的源檔案在什麼地方,要產生的編譯對象的名字是什麼

LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE := hello #指定了產生的動態連結程式庫的名字    LOCAL_SRC_FILES := hello.c #指定了C的源檔案叫什麼名字    include $(BUILD_SHARED_LIBRARY)

⑥ 調用ndk-build編譯c代碼產生動態連結程式庫.so檔案 檔案的位置 lib->armeabi->.so

項目的路徑:F:\workspace_sdk4\01_JNIHelloworld

開啟cmd

輸入:cd /d F:\workspace_sdk4\01_JNIHelloworld

輸入:ndk-build

編譯速度很快,產生的檔案在libs目錄下,重新整理項目就可以看到

⑦ 在java代碼中載入動態連結程式庫 System.loadlibrary("動態連結程式庫的名字"); Android.mkLOCAL_MODULE所指定的名字

MainActivity.java

package com.wuyudong.jnihello;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click(View view) {        System.loadLibrary("hello");        String result = helloFromC();        Toast.makeText(getApplicationContext(), result, 0).show();    }    // 聲明本地方法 使用native關鍵字 本地方法不用實現    public native String helloFromC();}

最後部署項目到模擬器

Android jni helloworld

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.