Skip to content

Commit 3cf49f4

Browse files
committed
Migrate unit-test to RegisterNatives.
1 parent df390bb commit 3cf49f4

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

unit-test/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ android {
4747
}
4848

4949
dependencies {
50+
implementation project(":base")
5051
implementation libs.appcompat
5152
implementation libs.material
5253
implementation libs.androidx.constraintlayout

unit-test/app/src/main/cpp/CMakeLists.txt

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,25 @@ project("unittest")
88

99
include(AppLibrary)
1010

11+
find_package(base REQUIRED CONFIG)
1112
find_package(googletest REQUIRED CONFIG)
1213
find_package(junit-gtest REQUIRED CONFIG)
1314

1415
add_app_library(adder OBJECT adder.cpp)
1516

16-
# Creates and names a library, sets it as either STATIC
17-
# or SHARED, and provides the relative paths to its source code.
18-
# You can define multiple libraries, and CMake builds them for you.
19-
# Gradle automatically packages shared libraries with your APK.
20-
21-
add_app_library( # Sets the name of the library.
17+
add_app_library(
2218
unittest
23-
24-
# Sets the library as a shared library.
2519
SHARED
26-
27-
# Provides a relative path to your source file(s).
2820
$<TARGET_OBJECTS:adder>
29-
native-lib.cpp)
30-
31-
# Searches for a specified prebuilt library and stores the path as a
32-
# variable. Because CMake includes system libraries in the search path by
33-
# default, you only need to specify the name of the public NDK library
34-
# you want to add. CMake verifies that the library exists before
35-
# completing its build.
36-
37-
find_library( # Sets the name of the path variable.
38-
log-lib
39-
40-
# Specifies the name of the NDK library that
41-
# you want CMake to locate.
42-
log)
43-
44-
# Specifies libraries CMake should link to your target library. You
45-
# can link multiple libraries, such as libraries you define in this
46-
# build script, prebuilt third-party libraries, or system libraries.
21+
native-lib.cpp
22+
)
4723

4824
target_link_libraries( # Specifies the target library.
4925
unittest
50-
51-
# Links the target library to the log library
52-
# included in the NDK.
53-
${log-lib})
26+
PRIVATE
27+
base::base
28+
log
29+
)
5430

5531
add_app_library(app_tests SHARED adder_test.cpp)
5632
target_link_libraries(app_tests
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
#include <base/macros.h>
12
#include <jni.h>
23

34
#include "adder.h"
45

5-
extern "C" JNIEXPORT jint JNICALL
6-
Java_com_example_unittest_MainActivity_add(JNIEnv*, jobject, jint a, jint b) {
7-
return add((int)a, (int)b);
8-
}
6+
jint Add(JNIEnv*, jobject, jint a, jint b) { return add((int)a, (int)b); }
7+
8+
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _Nonnull vm,
9+
void* _Nullable) {
10+
JNIEnv* env;
11+
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
12+
return JNI_ERR;
13+
}
14+
15+
jclass c = env->FindClass("com/example/unittest/MainActivity");
16+
if (c == nullptr) return JNI_ERR;
17+
18+
static const JNINativeMethod methods[] = {
19+
{"add", "(II)I", reinterpret_cast<void*>(Add)},
20+
};
21+
int rc = env->RegisterNatives(c, methods, arraysize(methods));
22+
if (rc != JNI_OK) return rc;
23+
24+
return JNI_VERSION_1_6;
25+
}

0 commit comments

Comments
 (0)