Android calls third-party libraries--first (reproduced)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/jiuyueguang/article/details/9447245

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0: Preface:

  1. These two days have been studying with the use of the Android JNI call third-party library, Internet search method, but all is generalities, no demo, after my several torture, wrote n more Helloword project, always unsuccessful, The project name is also transferred from HELLOOWRD to Shithelloword and then to Fuckhelloword to Bitchhelloword and then to Ganhelloword and then to Helloword, which succeeded in the end! Yeah, recorded, convenient for himself and others
  2. Had to spit out my broken computer, Ubuntu, after Eclipse opened, the CPU usage soared to 180%, then Ubuntu killed Eclipse, and then Eclipse withdrew, I later concluded that my Eclipse project had the source code for the entire filesystem of the Android system, and then put him close to project and it was much better not to be killed by Ubuntu.
  3. Space Description:
    1. First: The main introduction is to create a Hello project from scratch, which creates a library libhello.so and then calls him in a Java program, with a relatively one go step
    2. The second: mainly introduces the use of Android program directly call third-party library
    3. The third article: mainly introduces the integration of third-party libraries into their own libraries using Android programs,
    4. In these three articles, avoid boredom, the author will set a certain scene,
  4. If the author has the wrong place, please point out, because the author also just made "understand"

[First: Android calls third-party libraries-write library libhello.so]

[Second: Android calls third-party libraries-write library Android program directly call third-party library libhello.so]

[Third: Android calls third-party libraries--write library Android programs to integrate third-party libraries libhello.so to their own libraries libhelloword.so]

[Fourth: Android calls third-party libraries--call multiple third-party libraries for writing]

1: The scene: Party a non-software company to do an Android client, the function is mainly "face recognition", and then find Party B software company (the company face recognition is very good, mainly with C + + code to achieve face recognition, but this company did not do Android program, Long-term thinking of a Android programmer, temporarily consider, the Android program to outsource to C-party software company)

2: Analysis: Party B C + + company is now the main task is to do their own business logic library, and then the library to C-side Android to do the Android program, and then get the money. The first article is to Party B C + + company as the main body to do the program, the author to Hello simulation

3: Prerequisites:

  1. Ubuntu64 bit System
  2. ANDROID-NDK-R8E is already installed and configured into eclipse.
  3. Android-sdk-linux is already installed and configured into eclipse.

4: Get Up:

Create a new Android project (here Party B C + + company is a new Android program, later will be converted to C + + program), the name is Hello Register as Com.hello.hello, the JNI and Libs and obj folders in the figure do not care about him, they are automatically generated later.

Project Hello right-"Android tools-" Add Native support-"Fill in the name of the library you need to generate, write Hello here, OK, then automatically generated the JNI folder, there are Hello.cpp and android.mk

Add code in Helloactivity.java such as

Package Com.hello.hello;      Import android.app.Activity;      Import Android.app.AlertDialog;            Import Android.os.Bundle;  Public classHelloactivity extends Activity {Static{system.loadlibrary ("Hello"); }           Public StaticNative String SayHello ();//Local library functions@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);              Setcontentview (R.layout.main); NewAlertdialog.builder ( This). Setmessage (SayHello ()). Show (); }      }  

4. Using Javah to generate header files, and the use of Eclipse integration Javah Way, it is more convenient, in case my Java files have changed places, use this way to show great convenience, please drive cloth [ Integrate eclipse and Javah to generate JNI header files under Ubuntu develop Android native program]

5. Write the Hello.cpp file under the Jni folder

#include <string.h>#include<jni.h>#include"Com_hello_hello_helloactivity.h"            /** class:com_hello_word_hellowordactivity * Method:sayhello * Signature: () Ljava/lang/strin       G */jniexport jstring jnicall Java_com_hello_hello_helloactivity_sayhello (jnienv*env, Jclass) {          returnEnv->newstringutf ("Hello"); } jint jni_onload (JAVAVM* VMS,void*reserved) {jnienv* env =NULL; Jint result= -1; if(Vm->getenv (void* *) &env, jni_version_1_4)! =JNI_OK) {              returnresult; }          returnjni_version_1_4; }  

6. Write the android.mk file under the Jni folder.

    Local_path: = $ (call my-dir)            include $ (clear_vars)            local_module    := Hello      local_src _files:= hello.cpp            include $ (build_shared_library)  

7. Modify the project's compilation options: Project Right-"properties-" C + + build-"build command to fill in your android-ndk root directory; The Behaviour tab has a null build.

8. Compile project, Project right-"build project, see console output, show that our is correct, good!

     for Project Hello * * * * *            /opt/embedsky/android-ndk-r8e/ndk-build       Compile+ + thumb  : Hello <= hello.cpp      sharedlibrary  : libhello.so      Install        = libs/armeabi/libhello.so  

9. Run the Android project and you will see that the description is correct, so that the library files we have written can be correctly called by our own programs.

    1. Now the project is completed, the header files used in the project are Com_hello_hello_helloactivity.h and the generated cry file Libhello.so (under the Libs/armeabi folder) and the interface document is given to the project manager, Then the project manager is forwarded to the third-party Android company
    2. Go to the jokes of C-side android-------"[Second: Android uses JNI to invoke third-party libraries-write library Android programs to call third-party libraries directly libhello.so]

5:demo [poke here to download demo]

Android calls third-party libraries--first (reproduced)

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.