summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2017-01-26 15:01:59 +0100
committerKonstantin Käfer <mail@kkaefer.com>2017-01-27 18:45:16 +0100
commit14d5c336d72f3af54e3f72295e901591e219b5a9 (patch)
tree9443a4317e9e6b57693c0df32c96cc7728c64f03
parent2629f22a43628d93f65141a69e4b01999e19bcbd (diff)
downloadqtlocation-mapboxgl-14d5c336d72f3af54e3f72295e901591e219b5a9.tar.gz
[android] call AndroidRuntime's registerFrameworkNatives() method to set up an Android runtime environment
Instead of mocking android.util.Log, we're registering the real libraries so that we can use the full runtime environment
-rw-r--r--platform/android/src/test/main.jni.cpp59
1 files changed, 17 insertions, 42 deletions
diff --git a/platform/android/src/test/main.jni.cpp b/platform/android/src/test/main.jni.cpp
index f79d7671cb..f96dd6aa5e 100644
--- a/platform/android/src/test/main.jni.cpp
+++ b/platform/android/src/test/main.jni.cpp
@@ -1,7 +1,5 @@
#include "../jni.hpp"
-#include <android/log.h>
-#include <jni/jni.hpp>
#include <jni/jni.hpp>
#include <mbgl/util/logging.hpp>
@@ -9,10 +7,6 @@
#include <vector>
-#pragma clang diagnostic ignored "-Wunused-parameter"
-
-#define MAKE_NATIVE_METHOD(name, sig) jni::MakeNativeMethod<decltype(name), name>( #name, sig )
-
namespace {
// Main class (entry point for tests from dalvikvm)
@@ -48,31 +42,26 @@ struct Main {
} // namespace
-// JNI Bindings to stub the android.util.Log implementation
-
-static jboolean isLoggable(JNIEnv* env, jni::jobject* clazz, jni::jstring* tag, jint level) {
- return true;
-}
-
-static jint println_native(JNIEnv* env, jni::jobject* clazz, jint bufID, jint priority, jni::jstring* jtag, jni::jstring* jmessage) {
- if (jtag == nullptr || jmessage == nullptr) {
- return false;
- }
-
- std::string tag = jni::Make<std::string>(*env, jni::String(jtag));
- std::string message = jni::Make<std::string>(*env, jni::String(jmessage));
-
- return __android_log_print(priority, tag.c_str(), "%s", message.c_str());
-}
-
-static jint logger_entry_max_payload_native(JNIEnv* env, jni::jobject* clazz) {
- return static_cast<jint>(4068);
-}
-
+// We're declaring the function, which is libandroid_runtime.so.
+// It is defined in AndroidRuntime.cpp:
+// https://github.com/android/platform_frameworks_base/blob/master/core/jni/AndroidRuntime.cpp
+// Once this symbol goes away, we'll have to revisit.
+// This method loads and registers all of the Android-native frameworks so that we can use
+// android.util.Log, android.graphics.Bitmap and so on.
+// Setting the weak attribute tells the linker that this symbol is loaded at runtime and avoids
+// a missing symbol error.
+namespace android {
+struct AndroidRuntime {
+ static int startReg(JNIEnv* env) __attribute__((weak));
+};
+} // namespace android
// Main entry point
+extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
+ // Load Android-native jni bindings
+ jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6);
+ android::AndroidRuntime::startReg(&env);
-extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
// Load the main library jni bindings
mbgl::Log::Info(mbgl::Event::JNI, "Registering main JNI Methods");
mbgl::android::registerNatives(vm);
@@ -80,22 +69,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
// Load the test library jni bindings
mbgl::Log::Info(mbgl::Event::JNI, "Registering test JNI Methods");
- jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6);
-
jni::RegisterNatives(env, jni::Class<Main>::Find(env),
jni::MakeNativeMethod<decltype(Main::runAllTests), &Main::runAllTests>("runAllTests"));
- // Bindings for system classes
- struct Log { static constexpr auto Name() { return "android/util/Log"; } };
- try {
- jni::RegisterNatives(env, jni::Class<Log>::Find(env),
- MAKE_NATIVE_METHOD(isLoggable, "(Ljava/lang/String;I)Z"),
- MAKE_NATIVE_METHOD(logger_entry_max_payload_native, "()I"),
- MAKE_NATIVE_METHOD(println_native, "(IILjava/lang/String;Ljava/lang/String;)I")
- );
- } catch (jni::PendingJavaException ex) {
- env.ThrowNew(jni::JavaErrorClass(env), "Could not register Log mocks");
- }
-
return JNI_VERSION_1_6;
}