开发者

SQLite with Android NDK

开发者 https://www.devze.com 2023-02-21 06:42 出处:网络
Is it somehow possible to use SQLite with C++ on an Android phone? I haven\'t found any docume开发者_运维问答ntation around how this could be possible.Just download the SQLite3 amalgamation source fil

Is it somehow possible to use SQLite with C++ on an Android phone? I haven't found any docume开发者_运维问答ntation around how this could be possible.


Just download the SQLite3 amalgamation source file from: http://www.sqlite.org/download.html

And then add sqlite3.c to your LOCAL_SRC_FILES variable in Android.mk.


It isn't possible to use the built-in SQLite via NDK (or it wasn't six months ago when I looked into this), that can only be accessed with Java. However it may be possible to link in your own completely separate C++ build of SQLite.


See SQLite Android Bindings http://www.sqlite.org/android/doc/trunk/www/index.wiki which describes how to include sqlite3 for Android targets 15 (4.0.3) and greater. It's copied below.

SQLite Android Bindings

The SQLite library is a core part of the Android environment. Java applications and content providers access SQLite using the interface in the android.database.sqlite namespace.

One disadvantage of using Android's built-in SQLite support is that the application is forced to use the version of SQLite that the current version of Android happened to ship with. If your application happens to require a newer version of SQLite, or a build with a custom extension or VFS installed, you're out of luck.

The code in this project allows an application to use the Android NDK to build a custom version of SQLite to be shipped with the application while still continuing to use the standard Java interface.

Normal Usage

Installation

Android API levels 15 (Android 4.0.3) and greater are supported. If targetting API level 16 or greater, use the default "trunk" branch of this project. Or, for API level 15, use the "api-level-15" branch. It is not possible to target an API level lower than 15.

Copy the following files from this project into the equivalent locations in the application project.

jni/Android.mk
jni/Application.mk
jni/sqlite/*                (copy contents of directory recursively)
src/org/sqlite/database/*   (copy contents of directory recursively) 

Following this, the directory structures should contain these files.

For API level 15 only, also copy the following:

src/org/sqlite/os/*         (copy contents of directory recursively) 

Directory "jni/sqlite/" contains copies of the sqlite3.h and sqlite3.c source files. Between them, they contain the source code for the SQLite library. If necessary, replace these with the source for the specific version of SQLite required. If SQLite is to be compiled with any special pre-processor macros defined, add them to the "jni/sqlite/Android.mk" file (not jni/Android.mk).

Once the files have been added to the project, run the command "ndk-build" in the root directory of the project. This compiles the native code in the jni/ directory (including the custom SQLite version) to shared libraries that will be deployed to the device along with the application. Assuming it is successful, unless you modify the sources or makefiles within the jni/ directory structure, you should not need to run "ndk-build" again.

Application Programming

The classes that make up the built-in Android SQLite interface reside in the "android.database.sqlite" namespace. This interface provides all of the same classes, except within the "org.sqlite.database.sqlite" namespace. This means that to modify an application to use the custom version of SQLite, all that is usually required is to replace all occurrences "android.database.sqlite" within the source code with "org.sqlite.database.sqlite".

For example,the following:

import android.database.sqlite.SQLiteDatabase;

should be replaced with:

import org.sqlite.database.sqlite.SQLiteDatabase;

As well as replacing all uses of the classes in the android.database.sqlite.* namespace, the application must also be sure to use the following two:

org.sqlite.database.SQLException
org.sqlite.database.DatabaseErrorHandler

instead of:

android.database.SQLException
android.database.DatabaseErrorHandler

Aside from namespace changes, there are other differences from the stock Android interface that applications need to be aware of:

The SQLiteStatement.simpleQueryForBlobFileDescriptor() API is not available. The collation sequence "UNICODE" is not available. The collation sequence "LOCALIZED", which normally changes with the system's current locale, is always equivalent to SQLite's built in collation BINARY.


Disclaimer: i have only used this method for standalone executables, not libraries that implement JNI functions. It may work for a .so or not. Also, i'm working with a custom Android device not a phone.

You can use the built in SQLite via NDK but it's more of a hack than something supported. You need to nick sqlite3.h and libsqlite.so from an android source distribution and compile using them. Put sqlite3.h in your application source directory and you need to put the .so somewhere under the out/yourapp directory or build/platform/android-x/arch-arm/usr/lib for the linking step to finish. I have it in both places but i'm not sure which one is really needed.

You will end up linking to the libsqlite.so you provided but the binary will run fine using the system libsqlite.so on a target device.

0

精彩评论

暂无评论...
验证码 换一张
取 消