Call an existing. So library in an Android project

Source: Internet
Author: User

Note that the. So library refers to the Android platform, not the General Linux, UNIX platform;
1. Existing Library libcom_ycan_testlib.so
2. New Android Project TestLib2
3. Add a new class:
Class Name: TestLib
Package path: Refer to the existing library name, Com.ycan
4. Declare the local method of the library in the new class as follows:

?
1 2 3 4 5 6 package com.ycan;   public class testlib {      public native int add ( Code class= "Java keyword" >int a, int b);      public native int sub ( int a, Code class= "Java keyword" >int b); }

  

These interface functions should be told to you by the library's provider, I think so at the moment.
5. Then copy the library file to the following directory:
\workspace\testlib2\libs\armeabi
It seems to be the path, I just started without the Armeabi directory, the results run in error, and finally created a new directory like this, just OK.
6, call the library interface in the main class, my library is to do add and subtract, the basic calling code is as follows:

?
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;     } }

  Last Run Effect:

To be reminded, be sure to keep the classes in the new project that contain the local methods and the. So library consistent (including registration), or you will get an error unsatisfiedlinkerror

Reprint: http://blog.csdn.net/fww330666557/article/details/7842315

Thank the original author for sharing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.