From cd68df9303b9183d29fc3bd1798fb505913ec13b Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 13 May 2019 14:49:49 +0200 Subject: [PATCH] First integration of libgit2 in the Android App This will allow us to remove the jgit dependency. JGit is annoying because we have to use an old version in order to support older API versions. Additionally, there are some subtle differences in their implementation. Finally, for iOS and desktop we will be using libgit2, so we may as well use the same stack on Android. --- android/app/build.gradle | 22 +++++-- android/app/src/main/cpp/CMakeLists.txt | 57 ++++++++++++++++++- android/app/src/main/cpp/native-lib.cpp | 30 +++++++++- .../gitjournal/gitjournal/MainActivity.java | 10 ++-- 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index f778a745..de5c36bd 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -44,13 +44,21 @@ android { versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - /* + externalNativeBuild { cmake { cppFlags "" } } - */ + ndk { + abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' + } + sourceSets { + main { + jniLibs.srcDirs = ['libs'] + java.srcDirs = ['src'] + } + } } signingConfigs { @@ -66,13 +74,19 @@ android { signingConfig signingConfigs.release } } - /* + externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" } } - */ + + dexOptions { + incremental true + preDexLibraries true + jumboMode true + javaMaxHeapSize "12g" + } } flutter { diff --git a/android/app/src/main/cpp/CMakeLists.txt b/android/app/src/main/cpp/CMakeLists.txt index dfa4515f..e9df7e9f 100644 --- a/android/app/src/main/cpp/CMakeLists.txt +++ b/android/app/src/main/cpp/CMakeLists.txt @@ -1,6 +1,57 @@ cmake_minimum_required(VERSION 3.4.1) +macro(print_all_variables) + message(STATUS "print_all_variables------------------------------------------{") + get_cmake_property(_variableNames VARIABLES) + foreach (_variableName ${_variableNames}) + message(STATUS "${_variableName}=${${_variableName}}") + endforeach () + message(STATUS "print_all_variables------------------------------------------}") +endmacro() -add_library( native-lib - SHARED - native-lib.cpp ) \ No newline at end of file + +set(lib_src_DIR ${CMAKE_SOURCE_DIR}/../../../libs/${ANDROID_ABI}) +include_directories(${lib_src_DIR}/include) + +add_library(openssl-lib + STATIC + IMPORTED) + +set_target_properties(openssl-lib + PROPERTIES IMPORTED_LOCATION + ${lib_src_DIR}/lib/libssl.a) + + +add_library(crypto-lib + STATIC + IMPORTED) + +set_target_properties(crypto-lib + PROPERTIES IMPORTED_LOCATION + ${lib_src_DIR}/lib/libcrypto.a) + + +add_library(ssh2-lib + STATIC + IMPORTED) + +set_target_properties(ssh2-lib + PROPERTIES IMPORTED_LOCATION + ${lib_src_DIR}/lib/libssh2.a) + + +add_library(git2-lib + STATIC + IMPORTED) + +set_target_properties(git2-lib + PROPERTIES IMPORTED_LOCATION + ${lib_src_DIR}/lib/libgit2.a) + +add_library(native-lib + SHARED + native-lib.cpp) + +# The order of these libraries is super dooper important +# Otherwise you'll get linker errors +target_link_libraries(native-lib git2-lib ssh2-lib openssl-lib crypto-lib) diff --git a/android/app/src/main/cpp/native-lib.cpp b/android/app/src/main/cpp/native-lib.cpp index a1f314b9..a3c5af66 100644 --- a/android/app/src/main/cpp/native-lib.cpp +++ b/android/app/src/main/cpp/native-lib.cpp @@ -1,10 +1,36 @@ #include #include +#include + +#include +#include +#include +#include extern "C" JNIEXPORT jstring JNICALL Java_io_gitjournal_gitjournal_MainActivity_stringFromJNI( - JNIEnv* env, + JNIEnv *env, jobject /* this */) { - std::string hello = "Hello from C++"; + + ERR_load_crypto_strings(); + const char *openssl_version = OpenSSL_version(0); + const char *ssh2_version = libssh2_version(0); + + std::string hello = "Hello from C++: "; + hello += openssl_version; + hello += " SSH Version: "; + hello += ssh2_version; + + //git_libgit2_init(); + int major; + int minor; + int patch; + git_libgit2_version(&major, &minor, &patch); + + hello += " Git version: "; + char n_str[20]; + sprintf(n_str, "%d.%d.%d", major, minor, patch); + hello += n_str; + return env->NewStringUTF(hello.c_str()); } diff --git a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java index df185996..6e5dce8c 100644 --- a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java +++ b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java @@ -21,15 +21,15 @@ import io.flutter.util.PathUtils; // For MethodChannel public class MainActivity extends FlutterActivity implements MethodCallHandler { - //static { - // System.loadLibrary("native-lib"); - //} + static { + System.loadLibrary("native-lib"); + } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ - //public native String stringFromJNI(); + public native String stringFromJNI(); private static final String CHANNEL_NAME = "gitjournal.io/git"; @@ -37,7 +37,7 @@ public class MainActivity extends FlutterActivity implements MethodCallHandler { @Override protected void onCreate(Bundle savedInstanceState) { - //Log.d("VISH", stringFromJNI()); + Log.d("VISH", stringFromJNI()); super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this);