Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNI #29

Open
wants to merge 1 commit into
base: develop-v1.1.0
Choose a base branch
from
Open

JNI #29

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ plugins {
alias(libs.plugins.androidLibrary).apply(false)
alias(libs.plugins.kotlinMultiplatform).apply(false)
alias(libs.plugins.vanniktech.mavenPublish).apply(false)
alias(libs.plugins.kotlin.android) apply false
}
16 changes: 15 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ mavenPublish = "0.29.0"

#Coroutines
kotlin-coroutines = "1.9.0"
kotlinVersion = "1.9.0"
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
material = "1.12.0"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Expand All @@ -16,8 +23,15 @@ nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.grad
#Coroutines
kotlin-coroutines = {module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines"}
kotlin-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlin-coroutines" }
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }

[plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
vanniktech-mavenPublish = {id = "com.vanniktech.maven.publish", version.ref = "mavenPublish"}
vanniktech-mavenPublish = {id = "com.vanniktech.maven.publish", version.ref = "mavenPublish"}
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlinVersion" }
1 change: 1 addition & 0 deletions nativelib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
54 changes: 54 additions & 0 deletions nativelib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "more.math.nativelib"
compileSdk = 35

defaultConfig {
minSdk = 22

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
externalNativeBuild {
cmake {
cppFlags("")
}
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
externalNativeBuild {
cmake {
path("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
}
Empty file added nativelib/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions nativelib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
15 changes: 15 additions & 0 deletions nativelib/src/androidTest/java/more/math/TestNativeLib.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package more.math

import more.math.nativelib.NativeLib
import org.junit.Test
import org.junit.Assert.*

class TestNativeLib {
@Test
fun testInitString() {
val lib = NativeLib().apply { init() }
val expected = "Hello from C++"
val actual = lib.stringFromJNI()
assertEquals(expected, actual)
}
}
37 changes: 37 additions & 0 deletions nativelib/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("nativelib")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt.
nativelib.cpp)

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log)
11 changes: 11 additions & 0 deletions nativelib/src/main/cpp/nativelib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_more_math_nativelib_NativeLib_stringFromJNI(
JNIEnv* env,
jobject thiz) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
9 changes: 9 additions & 0 deletions nativelib/src/main/java/more/math/nativelib/NativeLib.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package more.math.nativelib

class NativeLib {
external fun stringFromJNI(): String

fun init() {
System.loadLibrary("nativelib")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package more.plumsoftware.nativelib

import more.math.nativelib.NativeLib
import org.junit.Test
import org.junit.Assert.*

class TestNativeLib {
@Test
fun testInitString() {
val lib = NativeLib().apply { init() }
val expected = "Hello from C++"
val actual = lib.stringFromJNI()
assertEquals(expected, actual)
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ dependencyResolutionManagement {

rootProject.name = "more-math"
include(":library")
include(":nativelib")