zoukankan      html  css  js  c++  java
  • Android Studio NDK开发浅谈

    环境:

    Android Studio 1.1.0

    NDK-r10d

    1、新建项目---》包名:com.mxl.az.ndk

    新建包含native方法的类:JniOperation.class

    public class JniOperation {
    
        public static native String getString();
        public native int add(int a, int b);
    
    }

    2、然后使用javah命令生成.h文件

    打开“小黑框”,进入项目目录的.../app/build/intermediates/classes/debug/中

    javah -jni com.mxl.az.ndk.JniOperation

    执行命令后,会在debug目录下生成.h文件

    3、在src/main目录下新建jni文件夹

    把.h文件copy到jni文件夹中

    4、新建main.c,include-->.h文件,并实现其中的俩个方法

    #include "com_mxl_az_ndk_JniOperation.h"
    #include <android/log.h>
    
    #define  LOG_TAG    "HelloJni"
    #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
    #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    
    JNIEXPORT jstring JNICALL Java_com_mxl_az_ndk_JniOperation_getString
      (JNIEnv * env, jclass cla)
    {
        int i;
        int size = 4;
    
        for(i=0; i<size; i++){
            LOGI("MXL");
        }
    
        return (*env)->NewStringUTF(env, "JNI 运算结果:");
    }
    
    
    JNIEXPORT jint JNICALL Java_com_mxl_az_ndk_JniOperation_add
      (JNIEnv * env, jobject obj, jint a, jint b)
    {
      return a + b;
    }

    5、在local.properties中配置ndk路径

    ndk.dir=/Users/mxl/Android/android-ndk-r10d

    6、配置一下build.gradle文件中的ndk

    defaultConfig {
            minSdkVersion 8
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
    
            ndk {
                abiFilter "armeabi"
                moduleName "hellojni"
                ldLibs "log", "z", "m", "jnigraphics", "android"
            }
    
        }

    7、在Activity中加载库,运行

     1 public class MainActivity extends ActionBarActivity {
     2 
     3     static {
     4         System.loadLibrary("hellojni");
     5     }
     6 
     7     private TextView tv;
     8 
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         setContentView(R.layout.activity_main);
    13 
    14         tv = (TextView) findViewById(R.id.tv);
    15         JniOperation jniOperation = new JniOperation();
    16         tv.setText(JniOperation.getString() + jniOperation.add(2,8));
    17     }
    18 }
  • 相关阅读:
    New version of VS2005 extensions for SharePoint 3.0
    QuickPart : 用户控件包装器 for SharePoint Server 2007
    随想
    发布 SharePoint Server 2007 Starter Page
    如何在SharePoint Server中整合其他应用系统?
    Office SharePoint Server 2007 中文180天评估版到货!
    RMS 1.0 SP2
    SharePoint Server 2007 Web内容管理中的几个关键概念
    如何为已存在的SharePoint站点启用SSL
    Some update information about Office 2007
  • 原文地址:https://www.cnblogs.com/maxinliang/p/4432984.html
Copyright © 2011-2022 走看看