mac android ndk的自我总结

(1)下载 mac ndk  

(2)配置环境  在终端输入:pico .bash_profile 进行编辑

     

  export PATH=$PATH:/Users/Sinde/Documents/android-sdk-mac
  export PATH=$PATH:/Users/Sinde/Documents/android-ndk-r8
  export PATH=$PATH:/Users/Sinde/Documents/cocos2d-1.0.1-x-0.9.2
  ANDROID_NDK_ROOT=/Users/Sinde/Documents/android-ndk-r8
  export ANDROID_NDK_ROOT
  ANDROID_SDK_ROOT=/Users/Sinde/Documents/android-sdk-mac
  export ANDROID_SDK_ROOT
  ANDROID_COCOS2DX_ROOT=/Users/Sinde/Documents/cocos2d-1.0.1-x-0.9.2
  export ANDROID_COCOS2DX_ROOT

    后保存( control+X) 选 Y

   环境变量的配置可以方便我们使用

  (3)   这样我们的ndk算是配好啦

(4)创建一个ndk的项目例如 jni-jni

       在项目根目录中创建一个jni文件夹里面放你的c/c++代码

       并且里面需要一个配置文件 android.mk 里面的类容如下

    #  

     # 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 a

  #      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    := good-jni

LOCAL_SRC_FILES := good-jni.c

 

include $(BUILD_SHARED_LIBRARY)

这里需要注意的是:

LOCAL_MODULE    := good-jni

LOCAL_SRC_FILES := good-jni.c

他们对应你的c/c++文件名

(5)在你的项目中需要调用c++代码的文件中有

 

publicclass Jni extends Activity {

 

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    TextView  tv = new TextView(this);

        tv.setText( stringFromJNI() );

   

        setContentView(tv);

   

}

 

public native String  stringFromJNI();//声明

static {

        System.loadLibrary("good-jni");

    }

}

stringFromJNI()为你c++中的方法名  并且必须要保持一致  

c++代码如下:

#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
*/
jstring
Java_com_example_hellojni_Jni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}

需要注意的是com_example_hellojni为你的包名Jni为你的调用C++的类名  stringFromJNI为方法名

(6)完成好这些后 我们需要在终端里面生成对应的so文件

    在上面我们进行环境的配置的作用现在就体现出来了直接 cd $ANDROID_NDK_ROOT进入ndk根目录

    在进入你项目的根目录cd 你的项目目录

    在执行 ndk-build的命令 即可生成对应so文件

    文件在libs里面

  (7)直接在eclipse运行即可

    

      

posted @ 2012-06-04 18:37  sinde12  阅读(1346)  评论(0编辑  收藏  举报