diff --git a/build-tools/create-packs/Microsoft.Android.Runtime.proj b/build-tools/create-packs/Microsoft.Android.Runtime.proj index 99969ba29f6..43b89066856 100644 --- a/build-tools/create-packs/Microsoft.Android.Runtime.proj +++ b/build-tools/create-packs/Microsoft.Android.Runtime.proj @@ -39,8 +39,10 @@ projects that use the Microsoft.Android framework in .NET 6+. <_AndroidRuntimePackAssemblies Include="$(_MonoAndroidNETDefaultOutDir)Mono.Android.Runtime.dll" /> <_AndroidRuntimePackAssemblies Include="$(_MonoAndroidNETOutputRoot)$(AndroidLatestStableApiLevel)\Mono.Android.Export.dll" /> - <_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.debug.so" /> - <_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.release.so" /> + <_AndroidRuntimePackAssets Condition=" Exists('$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.debug.so') " Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.debug.so" /> + <_AndroidRuntimePackAssets Condition=" Exists('$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.release.so') " Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.release.so" /> + <_AndroidRuntimePackAssets Condition=" Exists('$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libnet-android.debug.so') " Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libnet-android.debug.so" /> + <_AndroidRuntimePackAssets Condition=" Exists('$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libnet-android.release.so') " Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libnet-android.release.so" /> <_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libxamarin-debug-app-helper.so" /> <_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libxamarin-native-tracing.so" /> <_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libunwind_xamarin.a" /> diff --git a/build-tools/installers/create-installers.targets b/build-tools/installers/create-installers.targets index 0a63d3bfcf0..272ff354bd5 100644 --- a/build-tools/installers/create-installers.targets +++ b/build-tools/installers/create-installers.targets @@ -137,6 +137,10 @@ <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_fastdev_net6.jar" ExcludeFromLegacy="true" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_net6.dex" ExcludeFromLegacy="true" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_fastdev_net6.dex" ExcludeFromLegacy="true" /> + <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_clr.jar" ExcludeFromLegacy="true" /> + <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_fastdev_clr.jar" ExcludeFromLegacy="true" /> + <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_clr.dex" ExcludeFromLegacy="true" /> + <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)java_runtime_fastdev_clr.dex" ExcludeFromLegacy="true" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)manifestmerger.jar" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)proguard-android.txt" /> <_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)protobuf-net.dll" /> diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets index 10382ed3cc4..3b9038a84d5 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets @@ -232,6 +232,10 @@ _ResolveAssemblies MSBuild target. <_ExcludedNativeLibraries Condition=" '$(_AndroidIncludeSystemGlobalizationNative)' != 'true' " Include="libSystem.Globalization.Native" /> <_ExcludedNativeLibraries Condition=" '$(_AndroidEnableNativeStackTracing)' != 'true' " Include="libxamarin-native-tracing" /> + <_ExcludedNativeLibraries Condition=" '$(_AndroidUseCLR)' != 'True' " Include="libnet-android.debug" /> + <_ExcludedNativeLibraries Condition=" '$(_AndroidUseCLR)' != 'True' " Include="libnet-android.release" /> + <_ExcludedNativeLibraries Condition=" '$(_AndroidUseCLR)' == 'True' " Include="libmono-android.debug" /> + <_ExcludedNativeLibraries Condition=" '$(_AndroidUseCLR)' == 'True' " Include="libmono-android.release" /> - + <_RuntimeJar>$(MSBuildThisFileDirectory)\java_runtime_net6.jar <_RuntimeDex>$(MSBuildThisFileDirectory)\java_runtime_net6.dex + + + <_RuntimeJar>$(MSBuildThisFileDirectory)\java_runtime_clr.jar + <_RuntimeDex>$(MSBuildThisFileDirectory)\java_runtime_clr.dex + diff --git a/src/java-runtime/java-runtime.targets b/src/java-runtime/java-runtime.targets index 38eb73117d9..2cdec89f63c 100644 --- a/src/java-runtime/java-runtime.targets +++ b/src/java-runtime/java-runtime.targets @@ -7,14 +7,14 @@ $(OutputPath)java_runtime.dex $(IntermediateOutputPath)release $(IntermediateOutputPath)release.txt - ..\..\src-ThirdParty\bazel\java\mono\android\debug\MultiDexLoader.java;java\mono\android\debug\BuildConfig.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\release-net6\BuildConfig.java + ..\..\src-ThirdParty\bazel\java\mono\android\debug\MultiDexLoader.java;java\mono\android\debug\BuildConfig.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\clr\MonoPackageManager.java <_RuntimeOutput Include="$(OutputPath)java_runtime_fastdev.jar"> $(OutputPath)java_runtime_fastdev.jar $(OutputPath)java_runtime_fastdev.dex $(IntermediateOutputPath)fastdev $(IntermediateOutputPath)fastdev.txt - ..\..\src-ThirdParty\bazel\java\mono\android\release\MultiDexLoader.java;java\mono\android\release\BuildConfig.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\debug-net6\BuildConfig.java + ..\..\src-ThirdParty\bazel\java\mono\android\release\MultiDexLoader.java;java\mono\android\release\BuildConfig.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\clr\MonoPackageManager.java <_RuntimeOutput Include="$(OutputPath)java_runtime_net6.jar"> @@ -22,14 +22,29 @@ $(OutputPath)java_runtime_net6.dex $(IntermediateOutputPath)release-net6 $(IntermediateOutputPath)release-net6.txt - ..\..\src-ThirdParty\bazel\java\mono\android\debug\MultiDexLoader.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\debug\BuildConfig.java;java\mono\android\release\BuildConfig.java + ..\..\src-ThirdParty\bazel\java\mono\android\debug\MultiDexLoader.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\debug\BuildConfig.java;java\mono\android\release\BuildConfig.java;java\mono\android\clr\MonoPackageManager.java <_RuntimeOutput Include="$(OutputPath)java_runtime_fastdev_net6.jar"> $(OutputPath)java_runtime_fastdev_net6.jar $(OutputPath)java_runtime_fastdev_net6.dex $(IntermediateOutputPath)fastdev-net6 $(IntermediateOutputPath)fastdev-net6.txt - ..\..\src-ThirdParty\bazel\java\mono\android\release\MultiDexLoader.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\release\BuildConfig.java;java\mono\android\debug\BuildConfig.java + ..\..\src-ThirdParty\bazel\java\mono\android\release\MultiDexLoader.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\release\BuildConfig.java;java\mono\android\debug\BuildConfig.java;java\mono\android\clr\MonoPackageManager.java + + + <_RuntimeOutput Include="$(OutputPath)java_runtime_clr.jar"> + $(OutputPath)java_runtime_clr.jar + $(OutputPath)java_runtime_clr.dex + $(IntermediateOutputPath)release-clr + $(IntermediateOutputPath)release-clr.txt + ..\..\src-ThirdParty\bazel\java\mono\android\debug\MultiDexLoader.java;java\mono\android\debug-net6\BuildConfig.java;java\mono\android\debug\BuildConfig.java;java\mono\android\release\BuildConfig.java;java\mono\android\MonoPackageManager.java + + <_RuntimeOutput Include="$(OutputPath)java_runtime_fastdev_clr.jar"> + $(OutputPath)java_runtime_fastdev_clr.jar + $(OutputPath)java_runtime_fastdev_clr.dex + $(IntermediateOutputPath)fastdev-clr + $(IntermediateOutputPath)fastdev-clr.txt + ..\..\src-ThirdParty\bazel\java\mono\android\release\MultiDexLoader.java;java\mono\android\release-net6\BuildConfig.java;java\mono\android\release\BuildConfig.java;java\mono\android\debug\BuildConfig.java;java\mono\android\MonoPackageManager.java diff --git a/src/java-runtime/java/mono/android/MonoPackageManager.java b/src/java-runtime/java/mono/android/MonoPackageManager.java index fa6ed7be7fc..cd8de5b95d0 100644 --- a/src/java-runtime/java/mono/android/MonoPackageManager.java +++ b/src/java-runtime/java/mono/android/MonoPackageManager.java @@ -55,7 +55,7 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack } // - // Should the order change here, src/monodroid/jni/SharedConstants.hh must be updated accordingly + // Should the order change here, src/native/runtime-base/shared-constants.hh must be updated accordingly // String[] appDirs = new String[] {filesDir, cacheDir, dataDir}; boolean haveSplitApks = runtimePackage.splitSourceDirs != null && runtimePackage.splitSourceDirs.length > 0; diff --git a/src/java-runtime/java/mono/android/clr/MonoPackageManager.java b/src/java-runtime/java/mono/android/clr/MonoPackageManager.java new file mode 100644 index 00000000000..6ece4b145a0 --- /dev/null +++ b/src/java-runtime/java/mono/android/clr/MonoPackageManager.java @@ -0,0 +1,123 @@ +package mono; + +import java.io.*; +import java.lang.String; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Calendar; +import java.util.Locale; +import java.util.HashSet; +import java.util.zip.*; +import java.util.Arrays; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.res.AssetManager; +import android.os.Build; +import android.util.Log; +import mono.android.Runtime; +import mono.android.DebugRuntime; +import mono.android.BuildConfig; + +public class MonoPackageManager { + + static Object lock = new Object (); + static boolean initialized; + + static android.content.Context Context; + + public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks) + { + synchronized (lock) { + if (context instanceof android.app.Application) { + Context = context; + } + if (!initialized) { + android.content.IntentFilter timezoneChangedFilter = new android.content.IntentFilter ( + android.content.Intent.ACTION_TIMEZONE_CHANGED + ); + context.registerReceiver (new mono.android.app.NotifyTimeZoneChanges (), timezoneChangedFilter); + + Locale locale = Locale.getDefault (); + String language = locale.getLanguage () + "-" + locale.getCountry (); + String filesDir = context.getFilesDir ().getAbsolutePath (); + String cacheDir = context.getCacheDir ().getAbsolutePath (); + String dataDir = getNativeLibraryPath (context); + ClassLoader loader = context.getClassLoader (); + String runtimeDir = getNativeLibraryPath (runtimePackage); + int localDateTimeOffset; + + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + localDateTimeOffset = OffsetDateTime.now().getOffset().getTotalSeconds(); + } else { + localDateTimeOffset = (Calendar.getInstance ().get (Calendar.ZONE_OFFSET) + Calendar.getInstance ().get (Calendar.DST_OFFSET)) / 1000; + } + + // + // Should the order change here, src/native-clr/include/constants.hh must be updated accordingly + // + String[] appDirs = new String[] {filesDir, cacheDir, dataDir}; + boolean haveSplitApks = runtimePackage.splitSourceDirs != null && runtimePackage.splitSourceDirs.length > 0; + + System.loadLibrary("monodroid"); + + Runtime.initInternal ( + language, + apks, + runtimeDir, + appDirs, + localDateTimeOffset, + loader, + MonoPackageManager_Resources.Assemblies, + isEmulator (), + haveSplitApks + ); + + mono.android.app.ApplicationRegistration.registerApplications (); + + initialized = true; + } + } + } + + // We need to detect the emulator in order to determine the maximum gref count. + // The official Android emulator requires a much lower maximum than actual + // devices. Hopefully other emulators don't need the treatment. If they do, we + // can add their detection here. We should perform the absolute minimum of + // checking in order to save time. + static boolean isEmulator() + { + String val = Build.HARDWARE; + + // This detects the official Android emulator + if (val.contains ("ranchu") || val.contains ("goldfish")) + return true; + + return false; + } + + public static void setContext (Context context) + { + // Ignore; vestigial + } + + static String getNativeLibraryPath (Context context) + { + return getNativeLibraryPath (context.getApplicationInfo ()); + } + + static String getNativeLibraryPath (ApplicationInfo ainfo) + { + return ainfo.nativeLibraryDir; + } + + public static String[] getAssemblies () + { + return MonoPackageManager_Resources.Assemblies; + } + + public static String[] getDependencies () + { + return MonoPackageManager_Resources.Dependencies; + } +} diff --git a/src/native-clr/host/host-jni.cc b/src/native-clr/host/host-jni.cc index 3e9af6e868a..33b9180eff3 100644 --- a/src/native-clr/host/host-jni.cc +++ b/src/native-clr/host/host-jni.cc @@ -25,10 +25,10 @@ JNICALL Java_mono_android_Runtime_register (JNIEnv *env, [[maybe_unused]] jclass } JNIEXPORT void JNICALL -Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava, - jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader, - jobjectArray assembliesJava, jboolean isEmulator, - jboolean haveSplitApks) +Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApksJava, + jstring runtimeNativeLibDir, jobjectArray appDirs, jint localDateTimeOffset, jobject loader, + jobjectArray assembliesJava, jboolean isEmulator, + jboolean haveSplitApks) { } diff --git a/src/native-clr/include/host/host-jni.hh b/src/native-clr/include/host/host-jni.hh index 2104c2b1846..4904644ebd8 100644 --- a/src/native-clr/include/host/host-jni.hh +++ b/src/native-clr/include/host/host-jni.hh @@ -19,10 +19,10 @@ extern "C" { /* * Class: mono_android_Runtime - * Method: init + * Method: initInternal * Signature: (Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;ILjava/lang/ClassLoader;[Ljava/lang/String;IZZ)V */ - JNIEXPORT void JNICALL Java_mono_android_Runtime_init (JNIEnv *, jclass, jstring, jobjectArray, jstring, jobjectArray, jint, jobject, jobjectArray, jboolean, jboolean); + JNIEXPORT void JNICALL Java_mono_android_Runtime_initInternal (JNIEnv *, jclass, jstring, jobjectArray, jstring, jobjectArray, jint, jobject, jobjectArray, jboolean, jboolean); /* * Class: mono_android_Runtime diff --git a/src/native-clr/java-interop/CMakeLists.txt b/src/native-clr/java-interop/CMakeLists.txt index 702401df40c..a731f0110e0 100644 --- a/src/native-clr/java-interop/CMakeLists.txt +++ b/src/native-clr/java-interop/CMakeLists.txt @@ -1,4 +1,4 @@ -set(LIB_NAME xa-java-interop-clr) +set(LIB_NAME xa-java-interop) set(LIB_ALIAS xa::java-interop-clr) set(JAVA_INTEROP_SOURCES diff --git a/src/native-clr/libnet-android.map.txt b/src/native-clr/libnet-android.map.txt index f7e3c7c9fdf..9c8a580bc34 100644 --- a/src/native-clr/libnet-android.map.txt +++ b/src/native-clr/libnet-android.map.txt @@ -2,7 +2,7 @@ LIBNET_ANDROID { global: JNI_OnLoad; Java_mono_android_Runtime_dumpTimingData; - Java_mono_android_Runtime_init; + Java_mono_android_Runtime_initInternal; Java_mono_android_Runtime_notifyTimeZoneChanged; Java_mono_android_Runtime_propagateUncaughtException; Java_mono_android_Runtime_register; diff --git a/src/native-clr/runtime-base/CMakeLists.txt b/src/native-clr/runtime-base/CMakeLists.txt index 7e42498b5e4..6f2c07bb299 100644 --- a/src/native-clr/runtime-base/CMakeLists.txt +++ b/src/native-clr/runtime-base/CMakeLists.txt @@ -1,4 +1,4 @@ -set(LIB_NAME runtime-base-clr) +set(LIB_NAME runtime-base) set(LIB_ALIAS xa::runtime-base-clr) set(XA_RUNTIME_BASE_SOURCES @@ -43,13 +43,5 @@ target_link_libraries( xa::xamarin-app-clr ) -if(DEBUG_BUILD) - set_target_properties( - ${LIB_NAME} - PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" - ) -endif() - xa_add_compile_definitions(${LIB_NAME}) xa_add_include_directories(${LIB_NAME}) diff --git a/src/native-clr/runtime-base/android-system.cc b/src/native-clr/runtime-base/android-system.cc index fcf6a82b640..75dc394cdbd 100644 --- a/src/native-clr/runtime-base/android-system.cc +++ b/src/native-clr/runtime-base/android-system.cc @@ -11,13 +11,13 @@ auto AndroidSystem::lookup_system_property (std::string_view const& name, size_t &value_len) noexcept -> const char* { value_len = 0; -#if defined (DEBUG) - BundledProperty *p = lookup_system_property (name); - if (p != nullptr) { - value_len = p->value_len; - return p->name; - } -#endif // DEBUG || !ANDROID +// #if defined (DEBUG) +// BundledProperty *p = lookup_system_property (name); +// if (p != nullptr) { +// value_len = p->value_len; +// return p->name; +// } +// #endif // DEBUG || !ANDROID if (application_config.system_property_count == 0) { return nullptr; diff --git a/src/native-clr/shared/CMakeLists.txt b/src/native-clr/shared/CMakeLists.txt index 9c176e6ac65..c1a85951663 100644 --- a/src/native-clr/shared/CMakeLists.txt +++ b/src/native-clr/shared/CMakeLists.txt @@ -1,4 +1,4 @@ -set(LIB_NAME xa-shared-bits-clr) +set(LIB_NAME xa-shared-bits) set(LIB_ALIAS xa::shared-clr) set(XA_SHARED_SOURCES @@ -11,7 +11,6 @@ add_library( ${LIB_NAME} STATIC ${XA_SHARED_SOURCES} - ${XA_SHARED_CXX_ABI_SOURCES} ) add_library(${LIB_ALIAS} ALIAS ${LIB_NAME}) @@ -44,14 +43,6 @@ macro(lib_target_options TARGET_NAME) ${XA_COMMON_CXX_ARGS} ) - if(DEBUG_BUILD) - set_target_properties( - ${TARGET_NAME} - PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" - ) - endif() - xa_add_compile_definitions(${TARGET_NAME}) xa_add_include_directories(${TARGET_NAME}) endmacro() diff --git a/src/native-clr/startup/CMakeLists.txt b/src/native-clr/startup/CMakeLists.txt index 773eda8cf3a..2f6951c6275 100644 --- a/src/native-clr/startup/CMakeLists.txt +++ b/src/native-clr/startup/CMakeLists.txt @@ -1,4 +1,4 @@ -set(LIB_NAME xamarin-startup-clr) +set(LIB_NAME xamarin-startup) set(LIB_ALIAS xa::xamarin-startup-clr) set(XAMARIN_STARTUP_SOURCES @@ -8,11 +8,12 @@ add_clang_check_sources("${XAMARIN_STARTUP_SOURCES}") add_library( ${LIB_NAME} - SHARED + STATIC ${XAMARIN_STARTUP_SOURCES} ) add_library(${LIB_ALIAS} ALIAS ${LIB_NAME}) +set_static_library_suffix(${LIB_NAME}) target_include_directories( ${LIB_NAME}