diff --git a/mailbox-android/src/androidTest/java/org/briarproject/mailbox/core/DatabaseCreationTest.kt b/mailbox-android/src/androidTest/java/org/briarproject/mailbox/core/DatabaseCreationTest.kt index b36d39590d9c4a0fa9168c34955e6df521d631a1..6347d3e215306430895d8a8f778c02c2430ed2bd 100644 --- a/mailbox-android/src/androidTest/java/org/briarproject/mailbox/core/DatabaseCreationTest.kt +++ b/mailbox-android/src/androidTest/java/org/briarproject/mailbox/core/DatabaseCreationTest.kt @@ -1,5 +1,6 @@ package org.briarproject.mailbox.core +import android.content.Context import android.util.Log import kotlinx.coroutines.runBlocking import org.junit.Test @@ -11,6 +12,7 @@ import java.sql.Connection import java.sql.DriverManager import kotlin.test.assertEquals import kotlin.test.assertTrue +import androidx.test.core.app.ApplicationProvider.getApplicationContext; class DatabaseCreationTest { @@ -27,6 +29,9 @@ class DatabaseCreationTest { fun createDatabase(): Unit = runBlocking { Class.forName("org.sqlite.JDBC") + val ctx = getApplicationContext<Context>() + SQLiteJDBCLoader.initialize(ctx) + var foo = OSInfo.getNativeLibFolderPathForCurrentOS() Log.i(LOG_TAG, "native lib folder path: $foo") diff --git a/mailbox-android/src/main/java/org/sqlite2/SQLiteJDBCLoader.java b/mailbox-android/src/main/java/org/sqlite2/SQLiteJDBCLoader.java index fba978f024217f7f2f2bce36464d88fbd1272783..b3b52e8c29e0eee28ec1900d4124523f10b1f88a 100644 --- a/mailbox-android/src/main/java/org/sqlite2/SQLiteJDBCLoader.java +++ b/mailbox-android/src/main/java/org/sqlite2/SQLiteJDBCLoader.java @@ -24,6 +24,9 @@ //-------------------------------------- package org.sqlite2; +import android.content.Context; +import android.util.Log; + import org.sqlite.util.OSInfo; import org.sqlite.util.StringUtils; @@ -45,7 +48,7 @@ import java.util.UUID; * The library files are automatically extracted from this project's package * (JAR). * <p/> - * usage: call {@link #initialize()} before using SQLite JDBC driver. + * usage: call {@link #initialize(Context)} before using SQLite JDBC driver. * * @author leo */ @@ -59,12 +62,12 @@ public class SQLiteJDBCLoader { * @return True if SQLite native library is successfully loaded; false * otherwise. */ - public static synchronized boolean initialize() throws Exception { + public static synchronized boolean initialize(Context ctx) throws Exception { // only cleanup before the first extract if(!extracted) { cleanup(); } - loadSQLiteNativeLibrary(); + loadSQLiteNativeLibrary(ctx); return extracted; } @@ -101,67 +104,6 @@ public class SQLiteJDBCLoader { } } - /** - * @return True if the SQLite JDBC driver is set to pure Java mode; false - * otherwise. - * @deprecated Pure Java no longer supported - */ - @Deprecated - static boolean getPureJavaFlag() { - return Boolean.parseBoolean(System.getProperty("sqlite.purejava", "false")); - } - - /** - * Checks if the SQLite JDBC driver is set to pure Java mode. - * - * @return True if the SQLite JDBC driver is set to pure Java mode; false otherwise. - * @deprecated Pure Java nolonger supported - */ - @Deprecated - public static boolean isPureJavaMode() { - return false; - } - - /** - * Checks if the SQLite JDBC driver is set to native mode. - * - * @return True if the SQLite JDBC driver is set to native Java mode; false otherwise. - */ - public static boolean isNativeMode() throws Exception { - // load the driver - initialize(); - return extracted; - } - - /** - * Computes the MD5 value of the input stream. - * - * @param input InputStream. - * @return Encrypted string for the InputStream. - * @throws IOException - * @throws NoSuchAlgorithmException - */ - static String md5sum(InputStream input) throws IOException { - BufferedInputStream in = new BufferedInputStream(input); - - try { - MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); - DigestInputStream digestInputStream = new DigestInputStream(in, digest); - for(; digestInputStream.read() >= 0; ) { - - } - ByteArrayOutputStream md5out = new ByteArrayOutputStream(); - md5out.write(digest.digest()); - return md5out.toString(); - } - catch(NoSuchAlgorithmException e) { - throw new IllegalStateException("MD5 algorithm is not available: " + e); - } - finally { - in.close(); - } - } - private static boolean contentsEquals(InputStream in1, InputStream in2) throws IOException { if(!(in1 instanceof BufferedInputStream)) { in1 = new BufferedInputStream(in1); @@ -294,107 +236,27 @@ public class SQLiteJDBCLoader { * * @throws */ - private static void loadSQLiteNativeLibrary() throws Exception { + private static void loadSQLiteNativeLibrary(Context ctx) throws Exception { if(extracted) { return; } - List<String> triedPaths = new LinkedList<String>(); - - // Try loading library from org.sqlite.lib.path library path */ - String sqliteNativeLibraryPath = System.getProperty("org.sqlite.lib.path"); - String sqliteNativeLibraryName = System.getProperty("org.sqlite.lib.name"); - if(sqliteNativeLibraryName == null) { - sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc"); - if(sqliteNativeLibraryName != null && sqliteNativeLibraryName.endsWith(".dylib")) { - sqliteNativeLibraryName = sqliteNativeLibraryName.replace(".dylib", ".jnilib"); - } - } - - if(sqliteNativeLibraryPath != null) { - if(loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) { - extracted = true; - return; - } else { - triedPaths.add(sqliteNativeLibraryPath); - } - } - - // Load the os-dependent library from the jar file - String packagePath = SQLiteJDBCLoader.class.getPackage().getName().replaceAll("\\.", "/"); - sqliteNativeLibraryPath = String.format("/%s/native/%s", packagePath, OSInfo.getNativeLibFolderPathForCurrentOS()); - boolean hasNativeLib = hasResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName); - - - if(!hasNativeLib) { - if(OSInfo.getOSName().equals("Mac")) { - // Fix for openjdk7 for Mac - String altName = "libsqlitejdbc.jnilib"; - if(hasResource(sqliteNativeLibraryPath + "/" + altName)) { - sqliteNativeLibraryName = altName; - hasNativeLib = true; - } - } - } - - if(hasNativeLib) { - // temporary library folder - String tempFolder = getTempDir().getAbsolutePath(); - // Try extracting the library from jar - if(extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) { - extracted = true; - return; - } else { - triedPaths.add(sqliteNativeLibraryPath); - } - } - - // As a last resort try from java.library.path - String javaLibraryPath = System.getProperty("java.library.path", ""); - for(String ldPath : javaLibraryPath.split(File.pathSeparator)) { - if(ldPath.isEmpty()) { - continue; - } - if(loadNativeLibrary(ldPath, sqliteNativeLibraryName)) { - extracted = true; - return; - } else { - triedPaths.add(ldPath); - } - } - - extracted = false; - throw new Exception(String.format("No native library found for os.name=%s, os.arch=%s, paths=[%s]", - OSInfo.getOSName(), OSInfo.getArchName(), StringUtils.join(triedPaths, File.pathSeparator))); + String nativeLibDir = ctx.getApplicationInfo().nativeLibraryDir; + File sqliteLib = new File(nativeLibDir, "libsqlitejdbc.so"); + Log.i("sqlite-native", "lib: " + sqliteLib); + System.load(sqliteLib.getAbsolutePath()); } private static boolean hasResource(String path) { return SQLiteJDBCLoader.class.getResource(path) != null; } - @SuppressWarnings("unused") private static void getNativeLibraryFolderForTheCurrentOS() { String osName = OSInfo.getOSName(); String archName = OSInfo.getArchName(); } - /** - * @return The major version of the SQLite JDBC driver. - */ - public static int getMajorVersion() { - String[] c = getVersion().split("\\."); - return (c.length > 0) ? Integer.parseInt(c[0]) : 1; - } - - /** - * @return The minor version of the SQLite JDBC driver. - */ - public static int getMinorVersion() { - String[] c = getVersion().split("\\."); - return (c.length > 1) ? Integer.parseInt(c[1]) : 0; - } - /** * @return The version of the SQLite JDBC driver. */ diff --git a/mailbox-android/src/main/jniLibs/sqlite/aarch64/libsqlitejdbc.so b/mailbox-android/src/main/jniLibs/sqlite/aarch64/libsqlitejdbc.so new file mode 100755 index 0000000000000000000000000000000000000000..7bad179fe5a836e451c2cb7396087860aae1a116 Binary files /dev/null and b/mailbox-android/src/main/jniLibs/sqlite/aarch64/libsqlitejdbc.so differ diff --git a/mailbox-android/src/main/jniLibs/sqlite/android-arm/libsqlitejdbc.so b/mailbox-android/src/main/jniLibs/sqlite/android-arm/libsqlitejdbc.so new file mode 100755 index 0000000000000000000000000000000000000000..35f3fba421b28fe72e9791045c54520b31ecb9e7 Binary files /dev/null and b/mailbox-android/src/main/jniLibs/sqlite/android-arm/libsqlitejdbc.so differ diff --git a/mailbox-android/src/main/jniLibs/sqlite/arm/libsqlitejdbc.so b/mailbox-android/src/main/jniLibs/sqlite/arm/libsqlitejdbc.so new file mode 100755 index 0000000000000000000000000000000000000000..fe111c21af68547e9cc0d32efed63b115d975bfd Binary files /dev/null and b/mailbox-android/src/main/jniLibs/sqlite/arm/libsqlitejdbc.so differ diff --git a/mailbox-android/src/main/jniLibs/sqlite/ppc64/libsqlitejdbc.so b/mailbox-android/src/main/jniLibs/sqlite/ppc64/libsqlitejdbc.so new file mode 100755 index 0000000000000000000000000000000000000000..e1a224881ed661b6cd306e52329d4923a60d4665 Binary files /dev/null and b/mailbox-android/src/main/jniLibs/sqlite/ppc64/libsqlitejdbc.so differ