Skip to content

Commit

Permalink
feat: import Jenny for JNI
Browse files Browse the repository at this point in the history
Signed-off-by: DazeCake <[email protected]>
  • Loading branch information
DazeCake committed Mar 25, 2024
1 parent 2ca454f commit bdb685f
Show file tree
Hide file tree
Showing 8 changed files with 1,293 additions and 5 deletions.
51 changes: 49 additions & 2 deletions agent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.android.build.gradle.internal.tasks.factory.dependsOn
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
kotlin("kapt")
}

android {
Expand Down Expand Up @@ -32,6 +33,16 @@ android {
}
}
}
// make sure java compile is executed before ndk compile
// because we rely on Jenny generating cpp code
applicationVariants.all { variant ->
variant.externalNativeBuildProviders.forEach {
it.configure {
it.dependsOn(variant.javaCompileProvider.name)
}
}
return@android
}

buildTypes {
release {
Expand All @@ -44,7 +55,7 @@ android {
}
externalNativeBuild {
cmake {
path("CMakeLists.txt")
path("src/main/cpp/CMakeLists.txt")
}
}
compileOptions {
Expand All @@ -64,6 +75,35 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

// Jenny https://github.com/LanderlYoung/Jenny
compileOnly(libs.jenny.annotation)
kapt(libs.jenny.compiler)
}

kapt {
arguments {
// pass arguments to jenny
// arg("jenny.threadSafe", "false")
arg("jenny.errorLoggerFunction", "jennySampleErrorLog")
// arg("jenny.outputDirectory", project.buildDir.absolutePath+"/test")
arg("jenny.headerOnlyProxy", "true")
arg("jenny.useJniHelper", "true")
// arg("jenny.fusionProxyHeaderName", "JennyFusionProxy.h")
}
}

tasks.register<Copy>("copyGeneratedCpp") {
val generatedDir = "generated/source/kapt/debug"
val buildDir = project.layout.buildDirectory.get().asFile
copy {
from(File(buildDir, "${generatedDir}/jenny/proxy"))
into(project.file("src/main/cpp/gen"))
}
// copy {
// from(File(buildDir, "${generatedDir}/jenny/glue/header"))
// into(project.file("src/main/cpp"))
// }
}

tasks.register<Exec>("pushAgent") {
Expand All @@ -85,4 +125,11 @@ tasks.register<Exec>("runAgent") {
"/",
"com.dazecake.autodroidx.Main"
)
}.dependsOn("pushAgent")
}.dependsOn("pushAgent")

afterEvaluate {
// copy file to source after jenny generate
tasks.named("kaptDebugKotlin") {
finalizedBy("copyGeneratedCpp")
}
}
6 changes: 5 additions & 1 deletion agent/CMakeLists.txt → agent/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
cmake_minimum_required(VERSION 3.22.1)

include_directories(
gen
)

add_library(
ScriptLoader
SHARED
src/main/cpp/ScriptLoader.cpp
ScriptLoader.cpp
)

find_library(
Expand Down
26 changes: 26 additions & 0 deletions agent/src/main/cpp/ScriptLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <jni.h>
#include <string>
#include "android/log.h"
#include "LogProxy.h"
#include "jenny_fusion_proxies.h"

//
// Created by DazeCake on 2024/3/21.
Expand All @@ -9,5 +12,28 @@ extern "C"
JNIEXPORT jstring JNICALL
Java_com_dazecake_autodroidx_loader_JNI_stringFromJNI(JNIEnv *env, jobject thiz) {
std::string hello = "Hello from C++";
jenny::LocalRef<jstring> str(env->NewStringUTF("Hello from C call Kt log"));
jennySampleErrorLog(env, "Native error log from C++");
LogProxy::i(str);
return env->NewStringUTF(hello.c_str());
}

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void **>(&env),
JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
::jenny::Env::attachJvm(vm);

auto ok = jenny::initAllProxies(env);

assert(ok);

return JNI_VERSION_1_6;
}

void jennySampleErrorLog(JNIEnv *env, const char *error) {
__android_log_write(ANDROID_LOG_ERROR, "AutoDroidX-C", error);
env->ExceptionDescribe();
}
Loading

0 comments on commit bdb685f

Please sign in to comment.