[Excerpt] Android development-dynamic library call

Source: Internet
Author: User
1. Write and generate a dynamic library available in Android
(1) Write a dynamic library source Program File
Take my_add.c as an example. First, go to/home/Android/development/, and create the lib_test folder under this directory. Run # CD/home/Android/Development
# Mkdir lib_test
# Chmod 777./lib_test
# CD./lib_test

Create the my_add.c source file under lib_test, as shown below. /*
My_add.c
*/
# Include < Stdio. h >
Int Add ( Int X, Int Y)
{
Int Sum = X + Y;
Printf ( " The sum of % d and % d is % d \ n " , X, Y, sum );
Return SUM;
}

This program calculates the sum of the two integer variables, returns the value, and prints the sum information.

(2) Compile the Android. mk File
Create the Android. mk file in the lib_test directory. The content is as follows:Local_path:=$ (Call my-DIR)
Include $ (clear_vars)
Local_src_files:=My_add.c
Local_module:=Libmy_add
Local_prelink_module:=False
Include $ (build_shared_library)

(3) Compile the dynamic library
Go to the lib_test directory and use the MM command to compile the dynamic library.
# Cd/home/Android/development/lib_test
# Mm
The dynamic library file generated after compilation is identified as $ (local_module). So, that is, libmy_add.so. The dynamic library is located in the/home/Android/out/target/product/generic/system/lib directory.

2. Call the dynamic library
(1) Compile the source program that calls the dynamic library
Delete the my_add.c and Android. mk files created in the lib_test directory, and create the libtest. c files and my_add.h files in the directory. The content is as follows:/*
Libtest. c
*/
# Include<Stdio. h>
# Include"My_add.h"

int main ()
{< br> Add ( 3 , 4 );
printf ( " done \ n " );
return 0 ;
}

my_add.h header file: /*
my_add.h
*/
int Add ( int X, int Y );

(2) Compile the Android. mk File
Create the Android. mk file in the lib_test directory. The content is as follows.Local_path:=$ (Call my-DIR)
Include $ (clear_vars)
Local_src_files:=Libtest. c
Local_module:=Lib_test
Local_shared_libraries:=Libmy_add

Include $ (build_executable)
Note: local_shared_libraries indicates the dynamic library file to be called. The dynamic library file here is libmy_add.so,
The directory is located in the/home/Android/out/target/product/generic/lib directory.
Find the dynamic library file.

(3) Compile
Go to the lib_test directory and use the MM command to compile.
# Cd/home/Android/development/lib_test
# Mm
The generated Executable File lib_test is located in the/home/Android/out/target/product/generic/system/bin directory.

3. Use it in the android Simulator
(1) Start the simulator
(2) run the program
After the simulator Initialization is complete, push the lib_test file to the simulator and push the libmy_add.so file to the system/lib directory of the simulator.
Because the/system/directory in the simulator is the system directory, You need to modify the permission and use the ADB remount command. Run the following commands in sequence:
# ADB remount
# ADB push/home/Android/out/target/product/generic/system/lib/libmy_add.so/system/lib
# ADB push/home/Android/out/target/product/generic/system/bin/lib_test/Data
After the push command is executed, log on to the simulator and call lib_test on the simulator terminal to execute the command.
# ADB Shell
#/Data/lib_test

4. Hide details of dynamic library functions
You can use option-fvisibility = hidden to hide unnecessary functions and global variables in the dynamic library. Therefore, when calling a dynamic library, only functions that are not hidden (open) can be called. Other functions and variables can be called (referenced ). Add this option to local_cflag In the Android. mk file, and local_cflag + =-fvisibility = hidden. Before opening a function, you only need to add _ attribute _ (visibility ("default.
For example, to generate the dynamic library libmylib. So through the following mylib. C, you can compile the Android. mk file as follows: # Android. mk File
Local_path: = $ (Call my - DIR)
Include $ (clear_vars)
Local_src_files: = \
Mylib. c
Local_module: = Libmylib
Local_cflags + =- Fvisibility = Hidden
Local_prelink_module: = False
Include $ (build_shared_library)

/*
Mylib. c
*/
# Include<Stdio. h>

_ Attribute _ (visibility ( " Default " ))) Int Add ( Int X, Int Y)
{
Int Sum = X + Y;
Printf ( " % D add % d is % d \ n " , X, Y, sum );
Return SUM;
}

IntSub (IntX,IntY)
{
IntSub=X-Y;

Printf ("% D sub % d is % d \ n", X, Y, sub );
ReturnSub;
}

IntMul (IntX,IntY)
{
IntMul=X*Y;

Printf ("% D multiply % d is % d \ n", X, Y, mul );

ReturnMul;
}

in this way, libmylib is compiled by using the MM command. in so, only int add (int x, int y) functions can be called, while int sub (int x, int y) and int mul (int x, int y) then it cannot be called. If the source file calls the sub (or Mul) function in the dynamic library, compiling the file will result in an error where the sub function cannot be found.

Related Article

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.