zoukankan      html  css  js  c++  java
  • Android JNI学习之二 demo

    Android JNI学习之二 demo

    主要参考了文章http://www.cnblogs.com/wmj/archivse/2010/07/25/1784872.html

    1. 建立android工程,两个类,一个是activity,一个包含声明native方法的类

    package org.simon;
    
     
    
    import android.app.Activity;
    
    import android.os.Bundle;
    
     
    
    public class JNITest extends Activity {
    
        /** Called when the activity is first created. */
    
        @Override
    
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            Nadd nadd = new Nadd();
    
            this.setTitle("100 + 20 = " + nadd.nadd(100, 20));
    
        }
    
    }
    
    
    package org.simon;
    
     
    
    public class Nadd {
    
        static {
    
           System.loadLibrary("Nadd");
    
        }
    
        
    
        public native int nadd(int a, int b);
    
     
    
    }
    
    
    

    2. 用JDK自带的javah生成.h头文件。

     具体办法参见前一篇文章

    http://www.cnblogs.com/simoncook/archive/2010/09/25/1834750.html

    3. 编辑.c文件实现native方法。

    com_hello_jnitest_Nadd.c文件:
    #include <stdlib.h>
    #include "com_hello_jnitest_Nadd.h"
    JNIEXPORT jint JNICALL Java_com_hello_jnitest_Nadd_nadd(JNIEnv * env, jobject c, jint a, jint b)
    {
       return (a+b);
    }
    

    4. 下载工具链,并安装

     在这里

    http://www.codesourcery.com/gnu_toolchains/arm/download.html

    Windows: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe

    Linux: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

    我当时用的是ubuntu(10.4)平台

    所以是linux那个链接

    5. 编译。c文件产生so文件

    root@ubuntu:~/project/JNI/JNItest# /root/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi-gcc -I /home/tools/jdk1.6.0_21/include -I /home/tools/jdk1.6.0_21/include/linux -fpic -c org_simon_Nadd.c

    root@ubuntu:~/project/JNI/JNItest# /root/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi-ld -T /root/CodeSourcery/Sourcery_G++_Lite/arm-none-linux-gnueabi/lib/ldscripts/armelf_linux_eabi.xsc -share -o libNadd.so org_simon_Nadd.o

     

    6. 将so文件push进模器

    root@ubuntu:/home/tools/android-sdk-linux_x86-1.5_r1/tools# ./adb remount

    remount succeeded

    root@ubuntu:/home/tools/android-sdk-linux_x86-1.5_r1/tools# ./adb push /root/project/JNI/JNItest/libNadd.so /system/lib

    28 KB/s (1969 bytes in 0.068s)

     

    7. 运行工程,测试。

  • 相关阅读:
    Linux
    CUDA开发
    Linux C++
    Python
    C++
    模式识别
    Linux
    算法
    算法
    leetcode 数组,链表,跳表
  • 原文地址:https://www.cnblogs.com/simoncook/p/1837520.html
Copyright © 2011-2022 走看看