Skip to content

Commit 072adcf

Browse files
committed
jni integration working
1 parent 1b08cb9 commit 072adcf

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
.gradle/
44
build/
55
local.properties
6+
library/.externalNativeBuild
67
*.iml

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies {
3737
})
3838
implementation 'com.android.support:appcompat-v7:28.0.0'
3939
testImplementation 'junit:junit:4.12'
40-
// implementation project(path: ':library')
41-
implementation "com.stringcare:library:$stringcare_version"
40+
implementation project(path: ':library')
41+
// implementation "com.stringcare:library:$stringcare_version"
4242
}
4343

4444

library/CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# For more information about using CMake with Android Studio, read the
2+
# documentation: https://d.android.com/studio/projects/add-native-code.html
3+
4+
# Sets the minimum version of CMake required to build the native library.
5+
6+
cmake_minimum_required(VERSION 3.4.1)
7+
8+
# Creates and names a library, sets it as either STATIC
9+
# or SHARED, and provides the relative paths to its source code.
10+
# You can define multiple libraries, and CMake builds them for you.
11+
# Gradle automatically packages shared libraries with your APK.
12+
13+
add_library( # Sets the name of the library.
14+
native-lib
15+
16+
# Sets the library as a shared library.
17+
SHARED
18+
19+
# Provides a relative path to your source file(s).
20+
src/main/cpp/native-lib.cpp)
21+
22+
# Searches for a specified prebuilt library and stores the path as a
23+
# variable. Because CMake includes system libraries in the search path by
24+
# default, you only need to specify the name of the public NDK library
25+
# you want to add. CMake verifies that the library exists before
26+
# completing its build.
27+
28+
find_library( # Sets the name of the path variable.
29+
log-lib
30+
31+
# Specifies the name of the NDK library that
32+
# you want CMake to locate.
33+
log)
34+
35+
# Specifies libraries CMake should link to your target library. You
36+
# can link multiple libraries, such as libraries you define in this
37+
# build script, prebuilt third-party libraries, or system libraries.
38+
39+
target_link_libraries( # Specifies the target library.
40+
native-lib
41+
42+
# Links the target library to the log library
43+
# included in the NDK.
44+
${log-lib})

library/build.gradle

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ android {
1313
targetSdkVersion 28
1414
versionCode 4
1515
versionName version
16-
1716
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18-
17+
externalNativeBuild {
18+
cmake {
19+
cppFlags "-fexceptions"
20+
}
21+
}
1922
}
2023
buildTypes {
2124
release {
@@ -27,6 +30,11 @@ android {
2730
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2831
}
2932
}
33+
externalNativeBuild {
34+
cmake {
35+
path "CMakeLists.txt"
36+
}
37+
}
3038
}
3139

3240
dependencies {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <jni.h>
2+
#include <string>
3+
4+
extern "C" JNIEXPORT jstring JNICALL
5+
Java_com_stringcare_library_SC_stringFromJNI(
6+
JNIEnv *env,
7+
jobject /* this */) {
8+
std::string hello = "Hello from C++";
9+
return env->NewStringUTF(hello.c_str());
10+
}

library/src/main/java/com/stringcare/library/SC.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ public class SC {
4040
private static Context context;
4141
private static List<ContextListener> listeners = new ArrayList<>();
4242

43+
static {
44+
System.loadLibrary("native-lib");
45+
}
46+
4347
private SC () {
4448
// nothing to do here
4549
}
50+
51+
/**
52+
* A native method that is implemented by the 'native-lib' native library,
53+
* which is packaged with this application.
54+
*/
55+
public static native String stringFromJNI();
56+
4657
public static void init(Context c) {
4758
context = c;
4859
if (!listeners.isEmpty()) {
@@ -171,6 +182,7 @@ private static String byteArrayToHexString(byte[] bytes) {
171182
* @return String
172183
*/
173184
public static String getString(@StringRes int id) {
185+
if (true) return SC.stringFromJNI();
174186
if (context == null) {
175187
Log.e(TAG, "Library not initialized: SC.init(Context)");
176188
return null;

0 commit comments

Comments
 (0)