@@ -99,24 +99,33 @@ namespace xamarin::android {
9999 Helpers::abort_application (" DSO loader class not initialized properly." sv);
100100 }
101101
102- JNIEnv *jni_env = get_jnienv ();
102+ auto get_file_name = [](std::string_view const & full_name, bool is_path) -> std::string_view {
103+ if (!is_path) {
104+ return full_name;
105+ }
106+
107+ std::string_view name = full_name;
108+ size_t last_slash = name.find_last_of (' /' );
109+ if (last_slash != std::string_view::npos) [[likely]] {
110+ last_slash++;
111+ if (last_slash <= name.length ()) {
112+ name.remove_prefix (last_slash);
113+ }
114+ }
115+
116+ return name;
117+ };
118+
103119 // System.loadLibrary call is going to be slow anyway, so we can spend some more time generating an
104120 // undecorated library name here instead of at build time. This saves us a little bit of space in
105121 // `libxamarin-app.so` and makes the build code less complicated.
106- auto get_undecorated_name = [](std::string_view const & full_name, bool is_path) -> std::string_view {
122+ auto get_undecorated_name = [&get_file_name ](std::string_view const & full_name, bool is_path) -> std::string_view {
107123 std::string_view name;
108124
109125 if (!is_path) {
110126 name = full_name;
111127 } else {
112- name = full_name;
113- size_t last_slash = name.find_last_of (' /' );
114- if (last_slash != std::string_view::npos) [[likely]] {
115- last_slash++;
116- if (last_slash <= name.length ()) {
117- name.remove_prefix (last_slash);
118- }
119- }
128+ name = get_file_name (full_name, is_path);
120129 }
121130
122131 constexpr std::string_view lib_prefix { " lib" };
@@ -141,6 +150,7 @@ namespace xamarin::android {
141150 const std::string undecorated_lib_name { get_undecorated_name (name, name_is_path) };
142151 log_debug (LOG_ASSEMBLY, " Undecorated library name: {}" , undecorated_lib_name);
143152
153+ JNIEnv *jni_env = get_jnienv ();
144154 jstring lib_name = jni_env->NewStringUTF (undecorated_lib_name.c_str ());
145155 if (lib_name == nullptr ) [[unlikely]] {
146156 // It's an OOM, there's nothing better we can do
@@ -152,13 +162,16 @@ namespace xamarin::android {
152162 jni_env->ExceptionDescribe ();
153163 jni_env->ExceptionClear ();
154164 log_debug (LOG_ASSEMBLY, " Java exception cleared" );
165+ // TODO: should we abort? Return `nullptr`? `dlopen` still has a chance to succeed, even if loadLibrary
166+ // failed but it won't call `JNI_OnLoad` etc, so the result might be less than perfect.
155167 }
168+
156169 // This is unfortunate, but since `System.loadLibrary` doesn't return the class handle, we must get it this
157170 // way :(
158171 // We must use full name of the library, because dlopen won't accept an undecorated one without kicking up
159172 // a fuss.
160- log_debug (LOG_ASSEMBLY, " Attempting to get library {} handle after System.loadLibrary" , name);
161- return log_and_return (dlopen (name.data (), RTLD_NOLOAD), name);
173+ log_debug (LOG_ASSEMBLY, " Attempting to get library {} handle after System.loadLibrary. Will try to load using '{}' " , name, get_file_name (name, name_is_path) );
174+ return log_and_return (dlopen (get_file_name ( name, name_is_path) .data (), RTLD_NOLOAD), name);
162175 }
163176
164177 private:
0 commit comments