@@ -48,9 +48,9 @@ invoke_external_native_api (void (*callback)(void));
4848
4949/********* implementation *********/
5050
51- static char * g_bundle_path = NULL ;
52- static char * g_executable = NULL ;
53-
51+ static const char * g_bundle_path = NULL ;
52+ static MonoDomain * g_domain = NULL ;
53+ static MonoAssembly * g_assembly = NULL ;
5454
5555#define LOG_INFO (fmt , ...) __android_log_print(ANDROID_LOG_DEBUG, "DOTNET", fmt, ##__VA_ARGS__)
5656#define LOG_ERROR (fmt , ...) __android_log_print(ANDROID_LOG_ERROR, "DOTNET", fmt, ##__VA_ARGS__)
@@ -231,8 +231,9 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data)
231231}
232232
233233static int
234- mono_droid_runtime_init (const char * bundle_path , int local_date_time_offset )
234+ mono_droid_runtime_init (const char * bundle_path , const char * executable , int local_date_time_offset )
235235{
236+ LOG_INFO ("mono_droid_runtime_init (Mono) called with executable: %s" , executable );
236237 // NOTE: these options can be set via command line args for adb or xharness, see AndroidSampleApp.csproj
237238
238239 // uncomment for debug output:
@@ -316,6 +317,18 @@ mono_droid_runtime_init (const char* bundle_path, int local_date_time_offset)
316317#endif // FULL_AOT
317318#endif // FORCE_INTERPRETER
318319
320+ g_domain = mono_jit_init_version ("dotnet.android" , "mobile" );
321+ if (g_domain == NULL ) {
322+ LOG_ERROR ("mono_jit_init_version failed" );
323+ return -1 ;
324+ }
325+
326+ g_assembly = mono_droid_load_assembly (executable , NULL );
327+ if (g_assembly == NULL ) {
328+ LOG_ERROR ("mono_droid_load_assembly failed" );
329+ return -1 ;
330+ }
331+
319332 return rv ;
320333}
321334
@@ -327,23 +340,17 @@ free_resources ()
327340 free (g_bundle_path );
328341 g_bundle_path = NULL ;
329342 }
330- if (g_executable )
343+ if (g_assembly )
331344 {
332- free ( g_executable );
333- g_executable = NULL ;
345+ mono_assembly_close ( g_assembly );
346+ g_assembly = NULL ;
334347 }
335348}
336349
337350static int
338- mono_droid_execute_assembly (const char * executable , int managed_argc , char * managed_argv [])
339- {
340- MonoDomain * domain = mono_jit_init_version ("dotnet.android" , "mobile" );
341- assert (domain );
342-
343- MonoAssembly * assembly = mono_droid_load_assembly (executable , NULL );
344- assert (assembly );
345-
346- LOG_INFO ("Calling mono_jit_exec: %s" , executable );
351+ mono_droid_execute_assembly (MonoDomain * domain , MonoAssembly * assembly , int managed_argc , char * managed_argv [])
352+ {
353+ LOG_INFO ("Calling mono_jit_exec" );
347354 int rv = mono_jit_exec (domain , assembly , managed_argc , managed_argv );
348355 LOG_INFO ("Exit code: %d." , rv );
349356
@@ -386,40 +393,40 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
386393 strncpy_str (env , testresults_dir , j_testresults_dir , sizeof (testresults_dir ));
387394 strncpy_str (env , entryPointLibName , j_entryPointLibName , sizeof (entryPointLibName ));
388395
389- g_bundle_path = (char * )malloc (sizeof (char ) * (strlen (file_dir ) + 1 )); // +1 for '\0'
390- if (g_bundle_path == NULL )
396+ size_t file_dir_len = strlen (file_dir );
397+ char * bundle_path_tmp = (char * )malloc (sizeof (char ) * (file_dir_len + 1 )); // +1 for '\0'
398+ if (bundle_path_tmp == NULL )
391399 {
392400 LOG_ERROR ("Failed to allocate memory for bundle_path" );
393401 return -1 ;
394402 }
395- strncpy (g_bundle_path , file_dir , strlen (file_dir ) + 1 );
396-
397- g_executable = (char * )malloc (sizeof (char ) * (strlen (entryPointLibName ) + 1 )); // +1 for '\0'
398- if (g_executable == NULL )
399- {
400- LOG_ERROR ("Failed to allocate memory for executable" );
401- return -1 ;
402- }
403- strncpy (g_executable , entryPointLibName , strlen (entryPointLibName ) + 1 );
403+ strncpy (bundle_path_tmp , file_dir , file_dir_len + 1 );
404+ g_bundle_path = bundle_path_tmp ;
404405
405406 setenv ("HOME" , g_bundle_path , true);
406407 setenv ("TMPDIR" , cache_dir , true);
407408 setenv ("TEST_RESULTS_DIR" , testresults_dir , true);
408409
409- return mono_droid_runtime_init (g_bundle_path , current_local_time );
410+ return mono_droid_runtime_init (g_bundle_path , entryPointLibName , current_local_time );
410411}
411412
412413int
413414Java_net_dot_MonoRunner_execEntryPoint (JNIEnv * env , jobject thiz , jstring j_entryPointLibName , jobjectArray j_args )
414415{
415416 LOG_INFO ("Java_net_dot_MonoRunner_execEntryPoint (Mono):" );
416417
417- if (( g_bundle_path == NULL ) || ( g_executable == NULL ) )
418+ if (g_bundle_path == NULL )
418419 {
419420 LOG_ERROR ("Bundle path or executable name not set" );
420421 return -1 ;
421422 }
422423
424+ if (g_domain == NULL || g_assembly == NULL )
425+ {
426+ LOG_ERROR ("Mono domain or assembly not initialized" );
427+ return -1 ;
428+ }
429+
423430 int args_len = (* env )-> GetArrayLength (env , j_args );
424431 int managed_argc = args_len + 1 ;
425432 char * * managed_argv = (char * * )malloc (managed_argc * sizeof (char * ));
@@ -430,7 +437,7 @@ Java_net_dot_MonoRunner_execEntryPoint (JNIEnv* env, jobject thiz, jstring j_ent
430437 managed_argv [i + 1 ] = (char * )((* env )-> GetStringUTFChars (env , j_arg , NULL ));
431438 }
432439
433- int rv = mono_droid_execute_assembly (g_executable , managed_argc , managed_argv );
440+ int rv = mono_droid_execute_assembly (g_domain , g_assembly , managed_argc , managed_argv );
434441
435442 for (int i = 0 ; i < args_len ; ++ i )
436443 {
0 commit comments