首页 > 代码库 > 在Android项目中调用已有.so库

在Android项目中调用已有.so库

注意该.so库指的是android平台的,非一般linux、unix平台;
1、现有库libcom_ycan_testLib.so
2、新建android项目TestLib2
3、添加新类:
类名:testLib
包路径:参考现有库名,应为com.ycan
4、在新类中声明库的本地方法,如下:

?
1
2
3
4
5
6
package com.ycan;
 
public class testLib {
    public native int add (int a, int b);
    public native int sub (int a, int b);
}

  

这些接口函数,应该由库的提供者告诉你吧,我目前是这样认为的。
5、然后把库文件拷贝到如下目录:
\workspace\TestLib2\libs\armeabi
似乎必须是这个路径吧,我刚开始没有armeabi目录,结果运行出错,最后自己新建了一个这样的目录,才ok了。
6、在主类中调用库接口,我的库是做加减法的,基本调用代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.testlib2;
 
import com.ycan.testLib;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
     
    private Button btnAdd;
    private Button btnSub;
    private EditText num1;
    private EditText num2;
    private EditText result;
    private int a,b,rlt;
    private testLib lib = new testLib();
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        btnAdd = (Button)findViewById(R.id.button_add);
        btnSub = (Button)findViewById(R.id.button_sub);
        num1 = (EditText)findViewById(R.id.num1);
        num2 = (EditText)findViewById(R.id.num2);
        result = (EditText)findViewById(R.id.num3);
         
        btnAdd.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String strnum1 = num1.getText().toString();
                a = Integer.parseInt(strnum1);
                String strnum2 = num2.getText().toString();
                b = Integer.parseInt(strnum2);
                rlt = lib.add(a,b);
                Dostop(rlt);
            }
             
        });
         
        btnSub.setOnClickListener(new OnClickListener()
        {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String strnum1 = num1.getText().toString();
                a = Integer.parseInt(strnum1);
                String strnum2 = num2.getText().toString();
                b = Integer.parseInt(strnum2);
                rlt = lib.sub(a,b);
                Dostop(rlt);
                 
            }
        }
        );
    }
    static {       
        System.loadLibrary("com_ycan_testLib");   
    }
    private void Dostop(int number){
        Toast.makeText(this, ""+number, 8).show();//显示8秒
        String x=Integer.toString(rlt);
        result.setText(x);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

  最后运行效果:

需要提醒的一点,一定要保持新项目中包含本地方法的类和.so库中的一致(包括报名),否则会报错UnsatisfiedLinkError

 

转载:http://blog.csdn.net/fww330666557/article/details/7842315

感谢原作者的分享