diff --git a/CMakeLists.txt b/CMakeLists.txt index 40aa915..0dc514b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.4) project(olm VERSION 3.2.2 LANGUAGES CXX C) +option(OLM_ANDROID_JNI "Add Android JNI support" OFF) option(OLM_TESTS "Build tests" ON) option(BUILD_SHARED_LIBS "Build as a shared library" ON) @@ -48,6 +49,21 @@ add_library(olm lib/curve25519-donna/curve25519-donna.c) add_library(Olm::Olm ALIAS olm) +if(OLM_ANDROID_JNI) + target_sources(olm PRIVATE + jni/olm_account.cpp + jni/olm_session.cpp + jni/olm_jni_helper.cpp + jni/olm_inbound_group_session.cpp + jni/olm_outbound_group_session.cpp + jni/olm_utility.cpp + jni/olm_manager.cpp + jni/olm_pk.cpp + jni/olm_sas.cpp) + target_link_libraries(olm log) + target_compile_definitions(olm PRIVATE "$<$:NDK_DEBUG>") +endif() + target_include_directories(olm PUBLIC $ diff --git a/android/olm-sdk/build.gradle b/android/olm-sdk/build.gradle index e8f0f49..19803ef 100644 --- a/android/olm-sdk/build.gradle +++ b/android/olm-sdk/build.gradle @@ -1,12 +1,10 @@ -import org.apache.tools.ant.taskdefs.condition.Os - apply plugin: 'com.android.library' android { compileSdkVersion 30 defaultConfig { - minSdkVersion 14 + minSdkVersion 16 targetSdkVersion 30 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -14,6 +12,21 @@ android { // "pm clear" command after each test invocation. This command ensures // that the app's state is completely cleared between tests. testInstrumentationRunnerArguments clearPackageData: 'true' + + externalNativeBuild { + cmake { + arguments '-DANDROID_STL=c++_static', + '-DCMAKE_C_FLAGS=-fstack-protector-all -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Wall', + '-DCMAKE_CXX_FLAGS=-fstack-protector-all -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Wall', + '-DCMAKE_SHARED_LINKER_FLAGS=-z relro -z now', + '-DOLM_ANDROID_JNI=on', + '-DOLM_TESTS=off' + } + } + + ndk { + abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86_64', 'x86' + } } buildTypes { debug { @@ -31,9 +44,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - sourceSets.main { - jniLibs.srcDir 'src/main/libs' - jni.srcDirs = [] + externalNativeBuild { + cmake { + path '../../CMakeLists.txt' + } } task buildJavaDoc(type: Javadoc) { @@ -44,37 +58,11 @@ android { failOnError false } - task ndkBuildNativeRelease(type: Exec, description: 'NDK building..') { - println 'ndkBuildNativeRelease starts..' - workingDir file('src/main') - commandLine getNdkBuildCmd(), 'NDK_DEBUG=0' - } - - task ndkBuildNativeDebug(type: Exec, description: 'NDK building..') { - println 'ndkBuildNativeDebug starts..' - workingDir file('src/main') - commandLine getNdkBuildCmd(), 'NDK_DEBUG=1' - } - - task cleanNative(type: Exec, description: 'Clean NDK build') { - workingDir file('src/main') - commandLine getNdkBuildCmd(), 'clean' - } - tasks.withType(JavaCompile) { compileTask -> - if (compileTask.name.startsWith('compileDebugJava')) { - println 'test compile: Debug' - compileTask.dependsOn ndkBuildNativeDebug - } else if (compileTask.name.startsWith('compileReleaseJava')) { - println 'test compile: Release' - compileTask.dependsOn ndkBuildNativeRelease - } compileTask.dependsOn buildJavaDoc } - clean.dependsOn cleanNative - libraryVariants.all { variant -> variant.outputs.each { output -> @@ -86,24 +74,6 @@ android { } } -def getNdkFolder() { - Properties properties = new Properties() - properties.load(project.rootProject.file('local.properties').newDataInputStream()) - def ndkFolder = properties.getProperty('ndk.dir', null) - if (ndkFolder == null) - throw new GradleException("NDK location missing. Define it with ndk.dir in the local.properties file") - - return ndkFolder -} - -def getNdkBuildCmd() { - def ndkBuildCmd = getNdkFolder() + "/ndk-build" - if (Os.isFamily(Os.FAMILY_WINDOWS)) - ndkBuildCmd += ".cmd" - - return ndkBuildCmd -} - def gitRevision() { def cmd = "git rev-parse --short HEAD" return cmd.execute().text.trim() diff --git a/android/olm-sdk/src/main/jni/Android.mk b/android/olm-sdk/src/main/jni/Android.mk deleted file mode 100644 index 101346b..0000000 --- a/android/olm-sdk/src/main/jni/Android.mk +++ /dev/null @@ -1,67 +0,0 @@ -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - - -LOCAL_MODULE := olm - -SRC_ROOT_DIR := ../../../../.. - -include $(LOCAL_PATH)/$(SRC_ROOT_DIR)/common.mk -OLM_VERSION := $(MAJOR).$(MINOR).$(PATCH) - -$(info LOCAL_PATH=$(LOCAL_PATH)) -$(info SRC_ROOT_DIR=$(SRC_ROOT_DIR)) -$(info OLM_VERSION=$(OLM_VERSION)) - -LOCAL_CPPFLAGS+= -std=c++11 -Wall -LOCAL_CONLYFLAGS+= -std=c99 -LOCAL_CFLAGS+= -DOLMLIB_VERSION_MAJOR=$(MAJOR) \ --DOLMLIB_VERSION_MINOR=$(MINOR) \ --DOLMLIB_VERSION_PATCH=$(PATCH) - -#LOCAL_CFLAGS+= -DNDK_DEBUG - -LOCAL_CFLAGS+=-fstack-protector-all -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Wall -LOCAL_LDFLAGS=-z relro -z now - -LOCAL_C_INCLUDES+= $(LOCAL_PATH)/$(SRC_ROOT_DIR)/include/ \ -$(LOCAL_PATH)/$(SRC_ROOT_DIR)/lib - -$(info LOCAL_C_INCLUDES=$(LOCAL_C_INCLUDES)) - -LOCAL_SRC_FILES := $(SRC_ROOT_DIR)/src/account.cpp \ -$(SRC_ROOT_DIR)/src/base64.cpp \ -$(SRC_ROOT_DIR)/src/cipher.cpp \ -$(SRC_ROOT_DIR)/src/crypto.cpp \ -$(SRC_ROOT_DIR)/src/memory.cpp \ -$(SRC_ROOT_DIR)/src/message.cpp \ -$(SRC_ROOT_DIR)/src/olm.cpp \ -$(SRC_ROOT_DIR)/src/pickle.cpp \ -$(SRC_ROOT_DIR)/src/ratchet.cpp \ -$(SRC_ROOT_DIR)/src/session.cpp \ -$(SRC_ROOT_DIR)/src/utility.cpp \ -$(SRC_ROOT_DIR)/src/pk.cpp \ -$(SRC_ROOT_DIR)/src/sas.c \ -$(SRC_ROOT_DIR)/src/ed25519.c \ -$(SRC_ROOT_DIR)/src/error.c \ -$(SRC_ROOT_DIR)/src/inbound_group_session.c \ -$(SRC_ROOT_DIR)/src/megolm.c \ -$(SRC_ROOT_DIR)/src/outbound_group_session.c \ -$(SRC_ROOT_DIR)/src/pickle_encoding.c \ -$(SRC_ROOT_DIR)/lib/crypto-algorithms/sha256.c \ -$(SRC_ROOT_DIR)/lib/crypto-algorithms/aes.c \ -$(SRC_ROOT_DIR)/lib/curve25519-donna/curve25519-donna.c \ -olm_account.cpp \ -olm_session.cpp \ -olm_jni_helper.cpp \ -olm_inbound_group_session.cpp \ -olm_outbound_group_session.cpp \ -olm_utility.cpp \ -olm_manager.cpp \ -olm_pk.cpp \ -olm_sas.cpp - -LOCAL_LDLIBS := -llog - -include $(BUILD_SHARED_LIBRARY) - diff --git a/android/olm-sdk/src/main/jni/Application.mk b/android/olm-sdk/src/main/jni/Application.mk deleted file mode 100644 index e052911..0000000 --- a/android/olm-sdk/src/main/jni/Application.mk +++ /dev/null @@ -1,3 +0,0 @@ -APP_PLATFORM := android-16 -APP_ABI := arm64-v8a armeabi-v7a x86_64 x86 -APP_STL := c++_static \ No newline at end of file diff --git a/android/olm-sdk/src/main/jni/olm_account.cpp b/jni/olm_account.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_account.cpp rename to jni/olm_account.cpp diff --git a/android/olm-sdk/src/main/jni/olm_account.h b/jni/olm_account.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_account.h rename to jni/olm_account.h diff --git a/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/jni/olm_inbound_group_session.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp rename to jni/olm_inbound_group_session.cpp diff --git a/android/olm-sdk/src/main/jni/olm_inbound_group_session.h b/jni/olm_inbound_group_session.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_inbound_group_session.h rename to jni/olm_inbound_group_session.h diff --git a/android/olm-sdk/src/main/jni/olm_jni.h b/jni/olm_jni.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_jni.h rename to jni/olm_jni.h diff --git a/android/olm-sdk/src/main/jni/olm_jni_helper.cpp b/jni/olm_jni_helper.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_jni_helper.cpp rename to jni/olm_jni_helper.cpp diff --git a/android/olm-sdk/src/main/jni/olm_jni_helper.h b/jni/olm_jni_helper.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_jni_helper.h rename to jni/olm_jni_helper.h diff --git a/android/olm-sdk/src/main/jni/olm_manager.cpp b/jni/olm_manager.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_manager.cpp rename to jni/olm_manager.cpp diff --git a/android/olm-sdk/src/main/jni/olm_manager.h b/jni/olm_manager.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_manager.h rename to jni/olm_manager.h diff --git a/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/jni/olm_outbound_group_session.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp rename to jni/olm_outbound_group_session.cpp diff --git a/android/olm-sdk/src/main/jni/olm_outbound_group_session.h b/jni/olm_outbound_group_session.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_outbound_group_session.h rename to jni/olm_outbound_group_session.h diff --git a/android/olm-sdk/src/main/jni/olm_pk.cpp b/jni/olm_pk.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_pk.cpp rename to jni/olm_pk.cpp diff --git a/android/olm-sdk/src/main/jni/olm_pk.h b/jni/olm_pk.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_pk.h rename to jni/olm_pk.h diff --git a/android/olm-sdk/src/main/jni/olm_sas.cpp b/jni/olm_sas.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_sas.cpp rename to jni/olm_sas.cpp diff --git a/android/olm-sdk/src/main/jni/olm_sas.h b/jni/olm_sas.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_sas.h rename to jni/olm_sas.h diff --git a/android/olm-sdk/src/main/jni/olm_session.cpp b/jni/olm_session.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_session.cpp rename to jni/olm_session.cpp diff --git a/android/olm-sdk/src/main/jni/olm_session.h b/jni/olm_session.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_session.h rename to jni/olm_session.h diff --git a/android/olm-sdk/src/main/jni/olm_utility.cpp b/jni/olm_utility.cpp similarity index 100% rename from android/olm-sdk/src/main/jni/olm_utility.cpp rename to jni/olm_utility.cpp diff --git a/android/olm-sdk/src/main/jni/olm_utility.h b/jni/olm_utility.h similarity index 100% rename from android/olm-sdk/src/main/jni/olm_utility.h rename to jni/olm_utility.h