Skip to content

Commit 32ae6dd

Browse files
Dan AlbertDanAlbert
authored andcommitted
Migrate orderfile to RegisterNatives.
1 parent 8055c54 commit 32ae6dd

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

orderfile/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ android {
2626
}
2727

2828
buildFeatures {
29+
prefab true
2930
viewBinding true
3031
}
3132
}
3233

3334
dependencies {
35+
implementation project(":base")
3436
implementation libs.appcompat
3537
implementation libs.material
3638
implementation libs.androidx.constraintlayout

orderfile/app/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.22.1)
22
project(OrderfileDemo CXX)
33

44
include(AppLibrary)
5+
find_package(base CONFIG REQUIRED)
56

67
# We have setup build variables that you can just comment or uncomment to use.
78
# Make sure to have only one build variable uncommented at a time.
@@ -13,7 +14,7 @@ set(GENERATE_PROFILES ON)
1314
#set(USE_PROFILE "${CMAKE_SOURCE_DIR}/demo.orderfile")
1415

1516
add_app_library(orderfiledemo SHARED orderfile.cpp)
16-
target_link_libraries(orderfiledemo log)
17+
target_link_libraries(orderfiledemo PRIVATE base::base log)
1718

1819
if(GENERATE_PROFILES)
1920
# Generating profiles requires any optimization flag aside from -O0.

orderfile/app/src/main/cpp/orderfile.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <android/log.h>
2+
#include <base/macros.h>
23
#include <errno.h>
34
#include <jni.h>
45
#include <linux/limits.h>
@@ -46,9 +47,26 @@ void DumpProfileDataIfNeeded(const char* temp_dir) {
4647
#endif
4748
}
4849

49-
extern "C" JNIEXPORT void JNICALL
50-
Java_com_example_orderfiledemo_MainActivity_runWorkload(JNIEnv* env,
51-
jobject /* this */,
52-
jstring temp_dir) {
50+
void RunWorkload(JNIEnv* env, jobject /* this */, jstring temp_dir) {
5351
DumpProfileDataIfNeeded(env->GetStringUTFChars(temp_dir, 0));
5452
}
53+
54+
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _Nonnull vm,
55+
void* _Nullable) {
56+
JNIEnv* env;
57+
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
58+
return JNI_ERR;
59+
}
60+
61+
jclass c = env->FindClass("com/example/orderfiledemo/MainActivity");
62+
if (c == nullptr) return JNI_ERR;
63+
64+
static const JNINativeMethod methods[] = {
65+
{"runWorkload", "(Ljava/lang/String;)V",
66+
reinterpret_cast<void*>(RunWorkload)},
67+
};
68+
int rc = env->RegisterNatives(c, methods, arraysize(methods));
69+
if (rc != JNI_OK) return rc;
70+
71+
return JNI_VERSION_1_6;
72+
}

0 commit comments

Comments
 (0)