Android NDK初步涉獵

來源:互聯網
上載者:User

標籤:android

大致分是這幾步:

(1)建立Android工程:

                                                                                                          

HelloJni.java代碼

package com.panpass.main;public class HelloJni {static{System.loadLibrary("demo-jni");}public void sayHello(){}public native String jniSay();}

MainActivity.java代碼

package com.panpass.main;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import com.example.demo.R;public class MainActivity extends Activity {private TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_activity);mTextView = (TextView) findViewById(R.id.text_view);HelloJni demojin = new HelloJni();mTextView.setText(demojin.jniSay());}}
(2)產生標頭檔。

在cmd中進入相應的目錄下:

例如我的工程在這裡:



最終是進入到這裡:


也就是開啟cmd後輸入命令:

cd  C:\android\workone\Demo\bin\classes


接著輸入:javah  -jni com.panpass.main.HelloJni


就會產生.h檔案。


(3)建立jni檔案。

                                                                  

demo-jni.c就是c層函數的具體實現。如下:

/* * Copyright (C) 2009 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 <string.h>#include <jni.h>/* 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: * *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */jstringJava_com_panpass_main_HelloJni_jniSay( JNIEnv* env,                                                  jobject thiz){    return (*env)->NewStringUTF(env, "hello jni");}

其中的

Java_com_panpass_main_HelloJni_jniSay( JNIEnv* env,                                                  jobject thiz)
就是從產生的.h檔案中來的。看產生的.h檔案:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_panpass_main_HelloJni */#ifndef _Included_com_panpass_main_HelloJni#define _Included_com_panpass_main_HelloJni#ifdef __cplusplusextern "C" {#endif/* * Class:     com_panpass_main_HelloJni * Method:    haveReturnFromJni * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_haveReturnFromJni  (JNIEnv *, jobject, jstring);/* * Class:     com_panpass_main_HelloJni * Method:    jniSay * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_panpass_main_HelloJni_jniSay  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

Android.mk代碼如下:

# Copyright (C) 2009 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.#LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := demo-jniLOCAL_SRC_FILES := demo-jni.cinclude $(BUILD_SHARED_LIBRARY)
具體意思可以baidu。

(4)下載ndk使用ndk產生.so庫。

ndk下載去百度。

配置ndk環境變數:



其中NDK:



在cmd中進入工程根目錄:



接著執行:

ndk-build


重新整理工程你就會發現:


多了libs和obj

最後運行,OK。







                                                                                                             

Android NDK初步涉獵

聯繫我們

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