"Reprint" issue of Android NDK loading dynamic library in COCOS2DX

Source: Internet
Author: User

Original address: http://blog.csdn.net/sozell/article/details/10551309COCOS2DX the Android NDK loads the dynamic library problemChat

Recently in the access to the various Platform SDK, encountered a lot of problems, but also learned a lot of knowledge, before always feel nothing good to write, after all, did 4 months of game development, did not encounter any real big problem, COCOS2DX engine package is also very good, can let people spend most of the time on the game logic, The effect of the treatment, of course, before the libevent or small pits, but compared with the later encountered, also not what.

My earliest access to the SDK is 360, do not know whether it is good luck or a bit of the back, for me this only know C + + and Lua and a little bit of Java, take this as the first practiced hand, a bit bitter. There was no concept of activity at the time, but it was a bit too much to look back on and nothing worth saying. However, 360 of the SDK has libpaypalm_app_plugin_jar_360game.so this dynamic library access, especially with cocos2dx embedded, or there is a slightly different from the native Android program. But fortunately this only a dynamic library, began to also use some of the methods of unreliable, yesterday began to access the mobile payment SDK, there are two shared_library, with the beginning of the method is uncertain, but this evening search to search, and finally found some doorway, I hope this article will be a bit of a help to the people who have been so bitter as before. But in-depth principle I still do not understand, do not wish I can dig deep, Linux I also confined to a few shell command level.

Knowledge you need to know

    • At least I know about COCOS2DX (when I wrote this article I used 2.1.3) and knew what the NDK was.
    • Get a general idea of the difference between a dynamic library and a static library and know what the *.so file is
    • Will be a little Java, have successfully run through the COCOS2DX program (seemingly do not understand Java can also run through, hehe)
    • Learn the simple rules for writing makefile under Android

I assume that you are a COCOS2DX programmer, if not, it is not very helpful, because I do not know how to compare this kind of people to see the article.

If you are engaged in three-party dynamic library access to the COCOS2DX, then congratulations, read me this article should be helpful, I will not speak too deep, anyway the bottom of also do not understand, or to use, know how to use the main.

The General Payment SDK access, there are clients and service side, the server is not in the scope of this article, just talk about the client, and, here only on Android, iOS I do not understand. Anyway, all of the patterns are providing at least one jar package, Java as the invocation portal, and then if there are more underlying operations, it will be done by a. So dynamic library file. Generally speaking, if it is a native Android program, do not involve the C + + operation, so the so file is placed directly under the Android project under the Libs/armeabi folder (of course, for different CPUs will be available), At least their demo will be written in this way, and the help document will tell you to copy the so file to the corresponding directory. But if you run into COCOS2DX, it's not that good. Because of the nature of the framework itself, it is decided to write a dynamic library, which is then loaded by the activity call. Each time the eclipse build clears the Proj.android/libs/armeabi folder (and the same folder as the other CPU types), you're not going to have to pack any of those *.so files in the SDK. This will use a Makefile Dynamic library module to load. Prebuilt in fact itself COCOS2DX generated Android project will load a lot of prebuilt, such as JPG processing ah, PNG processing ah, curl Ah such, can refer to cocos2dx/platform/third_party/ Android/prebuilt directory, all put here, but the difference is that these are static library, I was added to the project before the Iconv and Libevent are also the same painting gourd to load in, but now encountered is a dynamic library, there will be a little different, but roughly the same routine. Method is in the jni/android.mk, in this makefile, the dynamic library module listed here, let the program to load. Here you need to know what the next ndk_module_path is, and the call import-module function command in makefile (not sure if that's true). Writing dynamic library (shared_library) modules

Talk about it, take 360. How to integrate the SDK into the jar I will not start, specifically how to deal with the inside of the libpaypalm_app_plugin_jar_360game.so.

First of all need to write a separate dynamic library module, which is said to write, in fact, the equivalent of a declaration, tell the compiler, where to find what files linked to the program, just write a makefile,android.mk. You can put in the current program under the JNI, create a new Libprebuilt folder, and then make a Armeabi folder (other folders similar, 360 seems to provide two, but I compiled are Armeabi type), the Libpaypalm_app_ plugin_jar_360game.so this dynamic library in. Create a new android.mk in the Libprebuilt directory with the following contents:

[Plain]View Plaincopy< param name= "allowfullscreen" value= "false" >
    1. Local_path: = $ (call My-dir)
    2. Include $ (clear_vars)
    3. Local_module: = Paypalm_app_plugin_jar_360game
    4. Local_src_files: = $ (Target_arch_abi)/libpaypalm_app_plugin_jar_360game.so
    5. Include $ (prebuilt_shared_library)

A little explanation.

    • Local_module, this is the name of the module, in other places to refer to this module, the name is usually the dynamic library to remove the head of the Lib and tail. So the remaining characters
    • Local_src_files, if it is a pure tripartite library, just need to fill in the path of the library, the front of the Target_arch_abi is used to match your compiled CPU type, will go to the corresponding folder to find
    • Prebuilt_shared_library, which shows this one with the compiled dynamic library, is important, and the static library distinguishes

As well, the rest is to refer to this dynamic library in the jni/android.mk. [Plain]View Plaincopy
    1. Local_shared_librarie: = Paypalm_app_plugin_jar_360game
This step is not enough, because makefile only know to load this dynamic library, but do not know where to load, but also need to write the android.mk included in, the simplest way is [Plain]View Plaincopy
    1. Include $ (local_path)/libprebuilt/android.mk

Well, this build project, you will see the last install libpaypalm_app_plugin_jar_360game.so words, in the APK Libs will also find the corresponding *.so file, at least, I access the 360 payment is a pass. If still not, please look down again, after all, I did not go to study the principle, dare not pack the ticket said must be able to use. How to load two. So the problem is that I was in the access to the mobile mm payment encountered, first in the makefile on the toss for a long time. Mobile mm Payment There are two dynamic libraries need to load, in fact, can also be used in the same way to draw the gourd, but because the need to include two times (two *.so modules respectively), but because the $ (local_path) This variable will change (many operations will cause its change, Even a makefile--with a $ (local_path) parameter can cause direct navigation to that folder, but the makefile will look confusing. In fact, you can use the method of loading the static library module before, this can be done with the call import-module function. But before we say this, there is a concept to figure out what Ndk_module_path is, and where it is defined. What is Ndk_module_path

I don't say much, literally, the path to the module, which is actually the search path when calling call Import-module. This can be specific to read a simple reference article: dot here

Ndk_module_path is where the definition of this actually search the folder to know, it is defined in the build_native.sh, my version is defined in the COCOS2DX directory and corresponding Platform/third_party/android/ Under the prebuilt. Well, knowing the above two small concepts, you can go on. We can use $ (call import-module,xxx) to load, Put the module you need to load (. so file) and the corresponding write Android.mk file into this prebuilt folder (you can sub-class folder), and then directly call, reference to other static library loading mode, your own jni/ The bottom of the android.mk is yes. Then add the dynamic library modules you need to load in the makefile, such as [Plain]View Plaincopy
    1. Local_shared_libraries: = Identifyapp casdkjni

OK, build under test, you should show the output of the install identifyapp.so and install casdkjni.so at the end, which means that the two dynamic libraries are packaged into the APK. Unfortunately, after this, the move mm payment call up will appear to load these so file failure causes initialization failure prompt, in fact, the last step. The key step is to find System.loadlibrary ("Cocos2dcpp") in the main Java file, and then add the third-party library name you want to load below. Seemingly the default is not indicated, will go down to the system path to find so file (no root or system permissions, no permission to this folder operation), these are the reason is to be loaded into the Data/appname/lib directory. [Java]View Plaincopy< param name= "allowfullscreen" value= "false" >
    1. Static  {  
    2. System.loadlibrary ("Identifyapp");
    3. System.loadlibrary ("Casdkjni");
    4. System.loadlibrary ("Cocos2dcpp");
    5. }
Load order Here's a question to pay particular attention to, the loading order of these dynamic libraries, must be put to libcocos2dcpp before loading, otherwise in loading libcocos2dcpp, will because there is no prior to these two dependent dynamic library and error, reported that the corresponding dynamic library wood loaded。 Off-topic If there is no active load above, but the dynamic library is already copied to the corresponding directory, I am engaged in the discovery of a very magical phenomenon. Installed, the first start on the error, the report can not find identifyapp.so, anyway, how to get up, and then compile a version without the two dynamic library, direct coverage, unexpectedly, and then I put the real machine on the program first uninstall, and then install the final version of the compilation, but also reported to find the dynamic library. Anyway, be sure to install two different versions in this order, it's OK. Mao Yan estimate actually overwrite installs the words will not delete the file, seemingly do not display import, the program itself will go to the current app directory to automatically load dynamic library, and makefile display given Local_shared_librarys, seemingly must load a bit, otherwise can not find. Did not go to scrutiny, but also can give think scrutiny friend a small clue. Reference
    • Load the dynamic library section can refer to the Stackflow on this post: please hit me, anyway these days, this aspect of the article is also quickly I turned rotten.
    • Information on C + + dynamic libraries for Java-loaded JNI can be found here: please hit me.

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.