Skip to content

Commit 79a8af4

Browse files
committed
add a cpp callback demo
1 parent 0203999 commit 79a8af4

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
plugins {
4-
id 'com.android.application' version '8.13.0' apply false
5-
id 'com.android.library' version '8.13.0' apply false
4+
id 'com.android.application' version '8.13.1' apply false
5+
id 'com.android.library' version '8.13.1' apply false
66
id 'org.jetbrains.kotlin.android' version '2.2.10' apply false
77
id 'com.google.devtools.ksp' version '2.2.10-2.0.2' apply false
88
id 'org.jetbrains.kotlin.plugin.compose' version '2.2.10' apply false

buildSrc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
// implementation localGroovy()
1010
implementation 'org.jetbrains.kotlin:kotlin-stdlib:2.2.0'
1111
// 添加了这个,就可以看 Android Gradle 插件的源码了
12-
implementation 'com.android.tools.build:gradle-api:8.13.0'
12+
implementation 'com.android.tools.build:gradle-api:8.13.1'
1313
// implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.3.40"
1414
implementation 'com.google.code.gson:gson:2.13.1'
1515
implementation 'com.android.tools:common:31.11.1'

subs/cpp_native/src/main/cpp/cpp_native.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string>
33
#include "androidlog.h"
44
#include "bspatch.h"
5+
#include <thread>
56

67
using namespace std;
78

@@ -36,4 +37,55 @@ Java_com_example_cpp_1native_internal_PatchUtil_patchAPK(JNIEnv *env, jclass cla
3637
env->ReleaseStringUTFChars(old_apk_file, argv[1]);
3738
env->ReleaseStringUTFChars(new_apk_file, argv[2]);
3839
env->ReleaseStringUTFChars(patch_file, argv[3]);
39-
}
40+
}
41+
42+
static JavaVM* gVm = nullptr; // 全局保存,供子线程 attach
43+
44+
// so 被加载时会把 JavaVM 存下来
45+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
46+
gVm = vm;
47+
return JNI_VERSION_1_6;
48+
}
49+
50+
// 子线程里真正干活的函数
51+
void worker(JNIEnv* env, jobject gCallback) {
52+
// 1. 模拟耗时 2 秒
53+
std::this_thread::sleep_for(std::chrono::seconds(2));
54+
55+
// 2. 找到接口类
56+
jclass clazz = env->GetObjectClass(gCallback);
57+
jmethodID onResult = env->GetMethodID(clazz,
58+
"onResult",
59+
"(Ljava/lang/String;)V");
60+
61+
// 3. 拼结果
62+
jstring msg = env->NewStringUTF("C++ 线程执行完毕");
63+
64+
// 4. 回调 Java
65+
env->CallVoidMethod(gCallback, onResult, msg);
66+
67+
// 5. 清理局部引用
68+
env->DeleteLocalRef(msg);
69+
env->DeleteLocalRef(clazz);
70+
}
71+
72+
extern "C"
73+
JNIEXPORT void JNICALL
74+
Java_com_example_cpp_1native_internal_NativeHouse_doHeavyWorkAsync(JNIEnv *env, jobject thiz,
75+
jobject callback) {
76+
// 1. callback 是局部变量,跨线程必须提升为全局引用
77+
jobject gCallback = env->NewGlobalRef(callback);
78+
if (gCallback == nullptr) return;
79+
// 2. 起 C++ 线程
80+
std::thread([gCallback]() {
81+
JNIEnv* env = nullptr;
82+
// 3. 子线程 attach
83+
if (gVm->AttachCurrentThread(&env, nullptr) != 0) return;
84+
worker(env, gCallback); // 真正干活
85+
// 4. 别忘了 detach,否则线程退出会崩
86+
gVm->DetachCurrentThread();
87+
// 5. 全局引用用完要释放
88+
env->DeleteGlobalRef(gCallback);
89+
}).detach();
90+
}
91+

subs/cpp_native/src/main/java/com/example/cpp_native/app/NativeRoot.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ object NativeRoot {
1414

1515
fun test() {
1616
lg(nativeHouse.nativeMessage)
17+
18+
nativeHouse.doHeavyWorkAsync {
19+
lg("result is $it")
20+
}
1721
}
1822

1923
private fun lg(msg: String) {

subs/cpp_native/src/main/java/com/example/cpp_native/internal/NativeHouse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ public class NativeHouse {
77
}
88

99
public native String getNativeMessage();
10+
11+
// 3. 声明 native:把耗时任务丢给 C++,并传一个 callback 实例
12+
public native void doHeavyWorkAsync(ResultCallback cb);
13+
14+
15+
public interface ResultCallback {
16+
void onResult(String msg); // 也可以带 int、byte[] 等多参数
17+
}
1018
}

0 commit comments

Comments
 (0)