Go Android calls so file (C code library) method in detail

Source: Internet
Author: User

First, why call C DLL to use the source code compiled into so library

Android is a Linux kernel-based mobile terminal system, and DLLs are generated and called in the Windows Environment C library, so can not be directly called for Android system.

Ii. installation (I) installation of Cygwin

1. Download Cygwin
Cygwin can be installed on the Windows Virtual UNIX compilation environment, and there are many Linux editors and other tools to choose from. In this project Cygwin is used primarily to compile the build so library. So the installation of GCC, make package is heavy.
Download the installer to Http://cygwin.com/setup.exe

2. Select "Install from Internet"


3. Select the installation directory

4. Select Download Storage Directory

5. Select "Direct Connection"

6. Select a mirror point to download

I first chose a feeling relatively close to the force of the mirrors.163.com (speed 300-400k around), the results downloaded to 51% do not go (1 hours or so), and then obediently change Back mirrors.kernel.org speed is a bit slow, but the installation was successful (about 40k, go out, do not know how long it took, noon at the beginning of the Night 6 o'clock back).

7.Category Bar Find Devel Click on the right to make default the install, other packages default. Installation can be done.

8. Test. Desktop Point Open the program, enter the make-v,gcc-v instruction test.

(II) Installing the NDK

1. Download the NDK (window.zip version), e.g. Android-ndk-r8-windows.zip
2. Unzip to a directory, such as D:\android\android-ndk-r8
3. Open the Cygwin installation directory under Home\administrator\.bash_profile and append at the end
Ndk=/cygdrive/d/android/android-ndk-r8
Export NDK
(Legend to use VIM or nodepad open, otherwise Cygwin do not recognize, I originally used vim, so did not experiment directly with the consequences of TXT open).

4. Restart, enter the CD $NDK return, the interface should be as follows:

5. Use CD to switch to D on Cygwin, add/cygdrive/d first

Third, the first call so Android project

1. New Android Project (MYFIRSTJNI)
2. Modify the code as follows:

public class Jnitest extends activity{/** called when the Activity is first created. */@Overridepublic void OnCreate (Bundl E savedinstancestate) {super.oncreate (savedinstancestate);//Setcontentview (R.layout.main); TextView TV = new TextView (this), Tv.settext ("" +intfromjni ()); Setcontentview (TV); public static native  int intfromjni ();//Port static {system.loadlibrary ("Myfirstjni");}}


Where Intfromjni is the function to be called from so. We need to generate. h files for so files first.
3. Jump to the Android project's bin\classes directory via Cygwin and enter the Javah command to compile the. class file. Such as:
Javah-jni Com.will.myfirstjni.MyFirstJni
Successful compilation generates. h files in the classes directory, such as:
Com_will_myfirstjni_myfirstjni.h
4. Create a new folder in the project JNI, copy the. h file to it, and add a. c file with the same name. Copy the functions in. h to. c To add the parameters of the function. As follows.

#include <string.h> #include <jni.h>jniexport jint jnicall java_com_will_myfirstjni_myfirstjni_ Intfromjni (JNIEnv *k, Jclass j) {return (jint) 100000;}


5. Copy the Android.mk file to the JNI directory of the project into the NDK directory Sample\hello-jni\jni directory.
The following two lines of code are mainly modified:

Local_module    : = myfirstjnilocal_src_files: = com_will_myfirstjni_myfirstjni.c


local_module--Project Name
LOCAL_SRC_FILES--C file name

6. In Cygwin, jump to the JNI directory, enter the $ndk/ndk-build command, compile, and the libs\armeabi\libmyfirstjni.so file will be generated. If you don't see it, right-click Refresh.

7. Start the simulator with the following effect.

Go Android calls so file (C code library) method in detail

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.