Android实现对c++方式调用

There are example  about how Android platform call c code via NDK , which are in android-ndk-r8(version) dirctory :

/home/dengwei/android-NDK/samples

[dengwei@localhost samples]$ ls
bitmap-plasma  hello-jni      hello-neon      native-activity  native-media   san-angeles     two-libs
hello-gl2      module-exports  native-audio     native-plasma  test-libstdc++

 

However , there is NOT a example about how to call c++ code via NDK. This article is about how android call c++ code.

ori url  

 The following is a modify version of hello-jni (/home/dengwei/android-NDK/samples/hello-jni), I copy it into hello-jni-cpp

You need to modify 3 files :Android.mk,hello-jni.c,com.example.hellojni.HelloJni.java
/First ,Android.mk:///

    
        LOCAL_PATH := $(call my-dir)
        LOCAL_CPP_EXTENSION := .cpp
        include $(CLEAR_VARS)
        LOCAL_MODULE    := hello-jni
        LOCAL_SRC_FILES := hello-jni.cpp
        include $(BUILD_SHARED_LIBRARY)

 $mv hello-jni.c hello.jni.cpp///
 Next , hello-jni.c:
        #include <string.h>

        #include <jni.h>
        class myMath
        {
             public:
             static int myAdd(int x,int y){return x+y;}
        };
        extern "C"
        {
             JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_myAdd
                 ( JNIEnv* env,jobject thiz )
             {
                  return myMath::myAdd(10,20);
             }
        }

/Last , HelloJni.java:///     

    package com.example.hellojni;
    import android.app.Activity;
    import android.widget.TextView;
    import android.os.Bundle;
    public class HelloJni extends Activity
    {
       
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            System.loadLibrary("hello-jni");
            TextView  tv = new TextView(this);
       
            int z=myAdd();
            tv.setText(Integer.toString(z));
       
            setContentView(tv);
        }
   
        static {
            System.loadLibrary("hello-jni");
        }  
   
        native static int myAdd();    
    }

sometimes you might come to an error : could NOT find somefile.o ,what you need to do is : $ ndk-build clean

EOF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值