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

  • 相关阅读:
    从体制内的国家干部转变为自由职业者-2017年总结
    为什么选择Django?
    Django contrib Comments 评论模块详解
    Django 2.0 新特性 抢先看!
    Python 为何能坐稳 AI 时代头牌语言
    为什么说Python 是大数据全栈式开发语言
    继承中的构造方法
    super关键字
    方法的重写
    类的继承与访问控制
  • 原文地址:https://www.cnblogs.com/no7dw/p/2699570.html
Copyright © 2011-2022 走看看