Pack Cocos2d-x's Libcurl separately to Android

Source: Internet
Author: User

Study n Long +n times, finally in this week resolved, thank you Niang. Feel the first time to run through, the heart is really extremely nervous, feel nothing will love again. Click the button to return the expected value, OK. Done, for this, relaxed a morning, now to write some how I straightened out. I can't guarantee that every library can do this, but it should be helpful for some of you who are just getting started. All right, come on!

1. Research background and significanceThe company to separate the network library, so, first think of Libcurl, because engaged in half a year cocos2d-x, so, directly found the Cocos2d-x engine source code pre-compiled third-party library Libcurl. What is a precompiled library? Explained below. 1.1 Pre-compiled librariesThe so-called precompiled library is actually a static library, a binary form of executable code that can be loaded by the operating system into memory execution. Under Linux or UNIX is a library file that ends in. A. Library files are compiled first, and when we import library files into our own dynamic or static files or projects, we increase the speed at which the code is compiled. The static library is loaded into the executable program when the code is compiled, the volume is large, the dynamic library is loaded into memory during the execution of the program, simple reference in the process of compiling, so the code size is small. This article explains in detail the basics of static and dynamic libraries under Linux. Linux Library Details1.2libcurlLibcurl is an open source Free library file, client transfer library, support FTP, FTPS, TFTP, HTTP, HTTPS, GOPHER, Telent, DICT, FILE, LDAP, etc., C language, cross-platform, support windows, Unix, Linux, etc., thread-safe, support Ipv6, easy to use. Official website: http://curl.haxx.se/download.htmlwebsite API detailed: http://curl.haxx.se/libcurl/c/in fact, the main is 4 functions: 1) Curl_easy_init () initialization, 2) curl_easy_setopt () set properties, 3) Curl_easy_perform, connect. 4) Curl_easy_getinfoThis article details how to use Libcurl to write downloads to memory: http://blog.163.com/xu_chao2000/blog/static/27770610200801303252802/2. Pack Android version Libcurl packThere are a lot of tutorials on the Libcurl package, because Cocos2d-x has provided us with a precompiled library of almost any platform, i.e.. a files. So, this step I omitted in this, although may be online tutorial Some comparison pit dad, but, spend more time, combined together, will make up a perfect solution. 3. Start packingbefore packaging, we may need to learn about the JNI and NDK programming knowledge, but this knowledge, is not a two-article can be explained clearly, so, there is no intention to explain, interested can look at my previous blog, or Baidu will have a large article on the Internet. 3.1 New Android Empty Projectuse Eclipse to create a new Android project. 1) Since the new project of Android itself does not contain the JNI directory, we need to add the directory ourselves and add two files to the directory. Android.mk and application.mk These two files are files that are used by ndk-build. 2) Copy the precompiled Library of the cocos2d-x. We need the Libcurl precompiled library in the Cocos2d-x engineCocos2d-x-3.3rc0/external/curldirectory, copy the two files in this directory, include and prebuilt, to our new Android project in the previous step. 3) Remove the platform directory from the Android directory in the Include and prebuilt two directories. Because we can't use those .4) Update eclipse, as you can see, we've just had the following directory: The focus is on the changes in the JNI directory (network restrictions, temporarily not able to upload photos)
3.2 Let's start with a simple example

The android.mk file can be said to be part of the final article. Let's start with a step-by-step talk about it.

First read this article and warm up to learn about the ways to package static and dynamic libraries.

Next we start with the two-libs example of the NDK samples and analyze the notation of Mk. (That is, when setting up an Android environment, samples/two-libs in the extracted NDK directory)

First on the code

1) First file: Defines a method of addition

First.h#ifndef first_h#define First_hextern int First (int  x, int  y), #endif/* First_h *///first.c#include " First.h "int first  (int  x, int  y) {    return x + y;}
2) SECOND.C: This file is a related file of JNI.
Second.c#include "First.h" #include <jni.h>//Add JNI header File// The following function means the Add method in the Mainactivity.java under the Com.example.twolibs package Com.example.androidlibcurldemo. Jintjava_com_example_androidlibcurldemo_mainactivity_add (jnienv*  env,                                      jobject this  ,                                      jint     x ,                                      jint     y) {    return first (x, y);}
This Add method, Is the two native method declared in Mainactivity.java in the SRC directory of our previous step to create the project, and plainly, SECOND.C's role is to define the native method in Java and call C's code in the implementation, such as the first function in first.c. Equivalent to the appearance of an intermediary.
3) Android.mk: Android.mk file in the example

#定义变量LOCAL_PATH指向mk所在的路径, such as the Android.mk file in/usr/lib/curl/android.mk# so Local_path represents the value is:/usr/lib/curl# Note, this variable is not cleaned up, If additional MK files are introduced later, this variable is able to be passed to the introduced Mk file in local_path:= $ (call My-dir) # First Lib, which'll be built statically#clear_vars cleanup variable, Because these variables are static global, if not clear, the next time you compile to use these variables will cause the wrong include $ (clear_vars) #本地静态库模块名字, if you import the library files elsewhere, you will use this name # The use of the name here is libxxx.so or libxxx.a in the name of XXX, but also can libxxx, however, these two are still a bit different in use, #使用XXX或libXXX最后打包库后都是libXXX. A/so, To use local_ldlibs: =-lz to indicate that the import is a local library # if the library is not found error local_module: = libtwolib-firstlocal_src_files: = first.c# Create a static library, Note that this library is the first time we created, so use build_static_library# if Local_src_files (that is, the source file) is using. A uses the include $ (prebuilt_static_library), See the code below # If you are using a. So file, use the include $ (prebuilt_shared_library), prebuilt indicates that you are using the source file in the precompiled library include $ (build_static_library) # Second Lib, which would depend on and include the first one# cleanup variable include $ (clear_vars) #模块名称LOCAL_MODULE: = Libtwolib-seco nd# source file Local_src_files: = second.c# Import the associated static library, if there are other static libraries, add it directly later, note the space separated local_static_libraries: = libtwolib-first# Finally create the dynamic library file, libtwolib-second.so# If you eventually create a static library file, you need to change gkfx to static, such as Libtwolib-firstinclude $ (build_shared_library) 

4) Application.mk

We only add App_abi in this MK file := all NDK will automatically package the corresponding platform ABI for our library files, different platforms of the library file is still a difference.

But now most Android phones are Armeabi, so if you do not add this sentence, the NDK will generate the default Armeabi directory, and compile the platform to use the library files.

5) Compile the library file

We enter into the Android project created, the root directory can, open the terminal, such as I use the Mac, open terminal after input:

$ cd/users/yuxikuo/android_workplace/androidlibcurldemo

$ ndk-build

Android NDK:WARNING:APP_PLATFORM android-19 is larger than Android:minsdkversion 8 in./androidmanifest.xml    [Armeab I] Compile thumb  : Twolib-second <= Second.c[armeabi] Compile thumb  : Twolib-first <= First.c[armeabi] Stat Iclibrary  : Libtwolib-first.a[armeabi] sharedlibrary  : Libtwolib-second.so[armeabi] Install        : libtwolib-second.so = libs/armeabi/libtwolib-second.so
The result of packaging is as follows: The NDK in Android copies the packaged package file to 1) under the libs/in the project directory under the corresponding Abi directory, ABI represents the current machine operating system, a total of 4 classes, different target system ABI has different packaging methods and compatibility. 2) A new obj/local directory is also created in the project catalog, which also copies the corresponding library files according to the different ABI. However, the different ABI directories need to be created by themselves, by default it is Armeabi, because most Android uses this, and it can run on all arm CPUs.

Armeabi = ARMv5TE or more

armeabi-v7a = ARMV7 or more

x86 = x86 Platform

MIPS = MIPS

Since packaging is based on different ABI, we will generate 4 packages each time, see the following code for the packaging process.

And now the other platforms are not common, and armeabi to other platforms are also compatible, so we are here for the time being only consider the Armeabi platform library file packaging.

Android NDK:WARNING:APP_PLATFORM android-19 is larger than Android:minsdkversion 8 in./androidmanifest.xml [armeabi- V7A] Compile thumb:twolib-second <= second.c[armeabi-v7a] Compile thumb:twolib-first <= first.c[armeabi-v7a] S TATICLIBRARY:LIBTWOLIB-FIRST.A[ARMEABI-V7A] sharedlibrary:libtwolib-second.so[armeabi-v7a] Install:libtwoli b-second.so = libs/armeabi-v7a/libtwolib-second.so//armeabi-v7a[armeabi] Compile thumb:twolib-second <= Second.c[armeabi] Compile thumb:twolib-first <= First.c[armeabi] Staticlibrary:libtwolib-first.a[armeabi] SharedL Ibrary:libtwolib-second.so[armeabi] install:libtwolib-second.so = libs/armeabi/libtwolib-second.so//arme   Abi[x86] Compile:twolib-second <= second.c[x86] compile:twolib-first <= first.c[x86] StaticLibrary : Libtwolib-first.a[x86] sharedlibrary:libtwolib-second.so[x86] install:libtwolib-second.so = libs/x86/l Ibtwolib-second.so//x86[mips] Compile:twolib-second <= second.c[mips] compile:twolib-first <= first.c[mips] Staticlibrary:li Btwolib-first.a[mips] sharedlibrary:libtwolib-second.so[mips] install:libtwolib-second.so = libs/mips/libt Wolib-second.so//mips
6) called in the Java code.

Add the following code in the Mainactivity.java

@Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);                Super.oncreate (savedinstancestate);        TextView  TV = new TextView (this);        int       x  = +;        int       y  =;        Here, we dynamically load the library at runtime        //Before calling the native method.        Load the generated library and take care to remove Lib and so from the libxxx.so. Keep only xxx        system.loadlibrary ("Twolib-second");        int  z = Add (x, y);        Tv.settext ("The sum of" + x + "and" + y + "is" + z);        Setcontentview (TV);    }    public native int Add (int  x, int  y),//native function declaration, defined in our SECOND.C file, and uses the first function in first.c for addition
Note that if an error or crash occurs, see if the SECOND.C function package name is not an error, and then see if there is an Add method that declares native in Java
Upload later.

3.3plays: Using LIBCURL.A to pack so libraries to Android







Pack Cocos2d-x's Libcurl separately to Android

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.