diff --git a/gradle/.gitignore b/gradle/.gitignore index aa724b770..23732d095 100644 --- a/gradle/.gitignore +++ b/gradle/.gitignore @@ -13,3 +13,4 @@ .externalNativeBuild .cxx local.properties +.kotlin diff --git a/gradle/app/build.gradle b/gradle/app/build.gradle index 17de3e9e2..de5c48f16 100644 --- a/gradle/app/build.gradle +++ b/gradle/app/build.gradle @@ -20,7 +20,7 @@ android { buildTypes { debug { minifyEnabled true - debuggable false + debuggable true // Note: Most logs will be stripped when this is false! proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } release { @@ -29,6 +29,11 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + externalNativeBuild { + cmake { + path = file("src/main/cpp/CMakeLists.txt") + } + } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/gradle/app/src/main/cpp/CMakeLists.txt b/gradle/app/src/main/cpp/CMakeLists.txt new file mode 100644 index 000000000..1f6a18c8f --- /dev/null +++ b/gradle/app/src/main/cpp/CMakeLists.txt @@ -0,0 +1,15 @@ + cmake_minimum_required(VERSION 3.6.0) + project(native-lib) + + add_library( + native-lib + SHARED + native.cpp) + + find_library( + log-lib + log) + + target_link_libraries( + native-lib + ${log-lib}) diff --git a/gradle/app/src/main/cpp/native.cpp b/gradle/app/src/main/cpp/native.cpp new file mode 100644 index 000000000..6bb45631d --- /dev/null +++ b/gradle/app/src/main/cpp/native.cpp @@ -0,0 +1,21 @@ +#include +#include + +// Leave this public so that the compiler can't elide the null ptr access. +char *invalid_ptr = nullptr; + +static void trigger_sefgault() { + *invalid_ptr = 0; +} + +extern "C" +JNIEXPORT jstring JNICALL +Java_io_bitdrift_gradleexample_MainActivity_stringFromJNI(JNIEnv *env, jobject thiz) { + return env->NewStringUTF("This is a string coming via JNI"); +} + +extern "C" +JNIEXPORT void JNICALL +Java_io_bitdrift_gradleexample_FirstFragment_triggerSegfault(JNIEnv *env, jobject thiz) { + trigger_sefgault(); +} diff --git a/gradle/app/src/main/java/io/bitdrift/gradleexample/FirstFragment.kt b/gradle/app/src/main/java/io/bitdrift/gradleexample/FirstFragment.kt index b536e5ca3..74900e059 100644 --- a/gradle/app/src/main/java/io/bitdrift/gradleexample/FirstFragment.kt +++ b/gradle/app/src/main/java/io/bitdrift/gradleexample/FirstFragment.kt @@ -24,6 +24,7 @@ import io.bitdrift.gradleexample.databinding.FragmentFirstBinding */ class FirstFragment : Fragment() { + private external fun triggerSegfault() private var _binding: FragmentFirstBinding? = null // This property is only valid between onCreateView and @@ -51,6 +52,9 @@ class FirstFragment : Fragment() { clipboardManager.setPrimaryClip(data) } + binding.buttonCrash.setOnClickListener { + triggerSegfault() + } binding.buttonFirst.setOnClickListener { findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment) } diff --git a/gradle/app/src/main/java/io/bitdrift/gradleexample/MainActivity.kt b/gradle/app/src/main/java/io/bitdrift/gradleexample/MainActivity.kt index 035dc1ccd..c6f341b7b 100644 --- a/gradle/app/src/main/java/io/bitdrift/gradleexample/MainActivity.kt +++ b/gradle/app/src/main/java/io/bitdrift/gradleexample/MainActivity.kt @@ -26,6 +26,9 @@ import timber.log.Timber class MainActivity : AppCompatActivity() { + init { System.loadLibrary("native-lib"); } + private external fun stringFromJNI(): String? + private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var binding: ActivityMainBinding @@ -43,6 +46,8 @@ class MainActivity : AppCompatActivity() { Log.i("MainActivity", "Bitdrift Logger configured with url: ${Logger.sessionUrl}") Timber.i("Bitdrift Logger configured with url: %s", Logger.sessionUrl) + Log.i("MainActivity", "Calling JNI method: ${stringFromJNI()}") + Timber.i("Calling JNI method: ${stringFromJNI()}") super.onCreate(savedInstanceState) diff --git a/gradle/app/src/main/res/layout/fragment_first.xml b/gradle/app/src/main/res/layout/fragment_first.xml index fb44a3d91..9dd226830 100644 --- a/gradle/app/src/main/res/layout/fragment_first.xml +++ b/gradle/app/src/main/res/layout/fragment_first.xml @@ -16,6 +16,16 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> +