mirror of
https://github.com/android/ndk-samples
synced 2025-11-09 01:26:42 +08:00
Migrate unit-test to RegisterNatives.
This commit is contained in:
@@ -47,6 +47,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation project(":base")
|
||||||
implementation libs.appcompat
|
implementation libs.appcompat
|
||||||
implementation libs.material
|
implementation libs.material
|
||||||
implementation libs.androidx.constraintlayout
|
implementation libs.androidx.constraintlayout
|
||||||
|
|||||||
@@ -8,49 +8,25 @@ project("unittest")
|
|||||||
|
|
||||||
include(AppLibrary)
|
include(AppLibrary)
|
||||||
|
|
||||||
|
find_package(base REQUIRED CONFIG)
|
||||||
find_package(googletest REQUIRED CONFIG)
|
find_package(googletest REQUIRED CONFIG)
|
||||||
find_package(junit-gtest REQUIRED CONFIG)
|
find_package(junit-gtest REQUIRED CONFIG)
|
||||||
|
|
||||||
add_app_library(adder OBJECT adder.cpp)
|
add_app_library(adder OBJECT adder.cpp)
|
||||||
|
|
||||||
# Creates and names a library, sets it as either STATIC
|
add_app_library(
|
||||||
# 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.
|
|
||||||
|
|
||||||
add_app_library( # Sets the name of the library.
|
|
||||||
unittest
|
unittest
|
||||||
|
|
||||||
# Sets the library as a shared library.
|
|
||||||
SHARED
|
SHARED
|
||||||
|
|
||||||
# Provides a relative path to your source file(s).
|
|
||||||
$<TARGET_OBJECTS:adder>
|
$<TARGET_OBJECTS:adder>
|
||||||
native-lib.cpp)
|
native-lib.cpp
|
||||||
|
)
|
||||||
# Searches for a specified prebuilt library and stores the path as a
|
|
||||||
# variable. Because CMake includes system libraries in the search path by
|
|
||||||
# default, you only need to specify the name of the public NDK library
|
|
||||||
# you want to add. CMake verifies that the library exists before
|
|
||||||
# completing its build.
|
|
||||||
|
|
||||||
find_library( # Sets the name of the path variable.
|
|
||||||
log-lib
|
|
||||||
|
|
||||||
# Specifies the name of the NDK library that
|
|
||||||
# you want CMake to locate.
|
|
||||||
log)
|
|
||||||
|
|
||||||
# Specifies libraries CMake should link to your target library. You
|
|
||||||
# can link multiple libraries, such as libraries you define in this
|
|
||||||
# build script, prebuilt third-party libraries, or system libraries.
|
|
||||||
|
|
||||||
target_link_libraries( # Specifies the target library.
|
target_link_libraries( # Specifies the target library.
|
||||||
unittest
|
unittest
|
||||||
|
PRIVATE
|
||||||
# Links the target library to the log library
|
base::base
|
||||||
# included in the NDK.
|
log
|
||||||
${log-lib})
|
)
|
||||||
|
|
||||||
add_app_library(app_tests SHARED adder_test.cpp)
|
add_app_library(app_tests SHARED adder_test.cpp)
|
||||||
target_link_libraries(app_tests
|
target_link_libraries(app_tests
|
||||||
|
|||||||
@@ -1,8 +1,25 @@
|
|||||||
|
#include <base/macros.h>
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
|
||||||
#include "adder.h"
|
#include "adder.h"
|
||||||
|
|
||||||
extern "C" JNIEXPORT jint JNICALL
|
jint Add(JNIEnv*, jobject, jint a, jint b) { return add((int)a, (int)b); }
|
||||||
Java_com_example_unittest_MainActivity_add(JNIEnv*, jobject, jint a, jint b) {
|
|
||||||
return add((int)a, (int)b);
|
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _Nonnull vm,
|
||||||
}
|
void* _Nullable) {
|
||||||
|
JNIEnv* env;
|
||||||
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
jclass c = env->FindClass("com/example/unittest/MainActivity");
|
||||||
|
if (c == nullptr) return JNI_ERR;
|
||||||
|
|
||||||
|
static const JNINativeMethod methods[] = {
|
||||||
|
{"add", "(II)I", reinterpret_cast<void*>(Add)},
|
||||||
|
};
|
||||||
|
int rc = env->RegisterNatives(c, methods, arraysize(methods));
|
||||||
|
if (rc != JNI_OK) return rc;
|
||||||
|
|
||||||
|
return JNI_VERSION_1_6;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user