zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    maven继承父工程统一版本号
    shiro权限控制参考
    动态查询列表页面的分页
    SVN服务器更改ip地址后怎么办
    cookie记住密码功能
    分享小插件的问题
    阿里云短信验证
    从svn上更新maven项目时,所有文件变成包的形式
    Maven工具
    Mybatis的dao层传递单参出现的问题
  • 原文地址:https://www.cnblogs.com/no7dw/p/2699570.html
Copyright © 2011-2022 走看看