Solve the Problem of referencing sqlite3 library when porting Android cocos2d-x, cocos2d-xsqlite3
1. First talk about cocos2d-x 3. x Portable Android simple steps
(1) Go to the proj. android directory of your project and open Android. mk in the jni directory (do not forget to back up one first)
If you have few project files, you can honestly include all cpp files in android. mk, such:
LOCAL_SRC_FILES: = hellocpp/main. cpp \
.../../Classes/AppDelegate. cpp \
../Classes/Audio. cpp \
../Classes/Chinese. cpp \
../Classes/ComboEffect. cpp \
This is the safest way to do this, but there are hundreds of files. You can try the following method:
Change the original
LOCAL_SRC_FILES: = AppDelegate. cpp \
HelloWorldScene. cpp
Changed:
MY_CPP_LIST: = $ (wildcard $ (LOCAL_PATH)/*. cpp)
MY_CPP_LIST + = $ (wildcard $ (LOCAL_PATH)/hellocpp/*. cpp)
MY_CPP_LIST + = $ (wildcard $ (LOCAL_PATH)/.../../Classes/*. cpp)
LOCAL_SRC_FILES :=$ (MY_CPP_LIST: $ (LOCAL_PATH)/% = %)
(2) The build_native.py command is not required. we use eclipse to directly import proj. android project, the method is File-> new-> other... -> Android Project from Existing Code, libcocos library with that E: \ cocos2d-x-3.3 \ cocos \ platform \ android \ java (create a Project first import) do not copy what java ah, ogg: It's okay if you don't copy the file. A copy of the file makes a wonderful mistake.
After the import, plug in the mobile phone, do not let it enter sleep state, right-click the Project Debug As-> Andorod Project will directly execute build_native.py
There are almost 100% errors during compilation, because the Eclipse check errors are more strict than VS. For example, non-ANSI itoa functions are not allowed to be used, and some function branches do not return values and report errors without warning. Please be patient and correct the errors.
What's even worse is the crash when running the mobile phone. I forgot the error, and the error is fatal and does not prompt the cause, but the program can run a little bit, at this time, you only need to enter the log ("xxx") function from the program entrance to see where the logCat went wrong.
2. sqlite3 library cocos2d-x 3. x porting Android Problems
(1) An error is reported, indicating that all sqlite database functions cannot be found.
Android. mk does not include. c compilation. We put sqlite3.h and sqlite3.c in the classes directory, and then write in Android. mk as follows:
MY_CPP_LIST: = $ (wildcard $ (LOCAL_PATH)/*. cpp)
MY_CPP_LIST + = $ (wildcard $ (LOCAL_PATH)/hellocpp/*. cpp)
MY_CPP_LIST + = $ (wildcard $ (LOCAL_PATH)/.../../Classes/*. cpp)
MY_CPP_LIST + = $ (wildcard $ (LOCAL_PATH)/.../../Classes/*. c) // This sentence is newly added. compile all. c files.
In this way, no error is reported in the program, but an error still occurs during running. It is invalid if the database is used, because an error occurs when accessing your xxx. db file.
The reason is that android has File Permission check, and the database. db file that is not in the read/write path cannot be accessed. You can't store it in your resource. You have to copy it to the read/write path,
Let's write a function:
In a Tool class, we specifically define the class of Tool functions.
// The sqlite file cannot be read in the android environment directly under resources. You must copy the file to a readable path before reading and writing. This function will. copy the db file to writablePath and return its path.
Static std: string getDataBaseFilePath (std: string filePath );
Then write the file as follows:
String Tools: getDataBaseFilePath (std: string filePath) {sqlite3 * dbFile = nullptr; string path = ""; path = FileUtils: getInstance ()-> fullPathForFilename (filePath ); # if CC_TARGET_PLATFORM = CC_PLATFORM_ANDROIDpath = FileUtils: getInstance ()-> getWritablePath (); // obtain the read/write path in the android environment, which is generally the SD card path + = filePath; FILE * file = fopen (path. c_str (), "r"); // try to open it in the read/write path. db, but this file should not be available at this time, so the file returns an empty if (file = nullptr) {ssize_t size; const char * data = (char *) (FileUtils: getInstance () -> getFileData (filePath, "rb", & size); // obtain the database file content in the resource file = fopen (path. c_str (), "wb"); // open the file in the read/write path of android. Here, a new file is created and fwrite (data, size, 1, file) is written ); // upload the database file under resource to CC_SAFE_DELETE_ARRAY (data);} fclose (file); # endiflog ("database file: % s", path. c_str (); return path ;}The Code is to copy the. db file under the resource (which must be directly in the resource root directory) to the read/write path, and then we can use it,
To use database files, write the following code:
// Open a database. If the database does not exist, create a database file string path = Tools: getDataBaseFilePath ("game. db "); result = sqlite3_open (path. c_str (), & pDB );This path is a valid database file path, and everything is fine.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.