Replace [DllImport] with [LibraryImport] for runtime P/Invokes#10763
Replace [DllImport] with [LibraryImport] for runtime P/Invokes#10763jonathanpeppers merged 12 commits intomainfrom
Conversation
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…sistently Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…n to 12 Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
|
@copilot build failed: |
…ruct parameter Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Fixed in 34970c1. Added |
|
@copilot we need to update apk sizes. there is documentation how to do it in Documentation/project-docs/ApkSizeRegressionChecks.md, so I believe you should be able to follow it to update the baselines:
Also, propose how to turn Documentation/project-docs/ApkSizeRegressionChecks.md into a SKILL.md that we could add to this repo. Feel free to commit it to this PR branch if you know how to do it. |
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
I've added a SKILL.md proposal in Regarding the baseline update: I cannot access the Azure Pipelines CI artifacts directly from this environment. The test results with the new
Would you be able to download the artifacts from the CI and either commit them or share them so I can update the baselines? |
|
I reverted the skill -- just because I don't believe it will work: I don't see how it will know to go to the right AzDO instance, figure out the right artifact, etc. We can bring this skill back, but we should test it locally -- can add it in a PR with a screenshot/example? We could make the skill include the |
There was a problem hiding this comment.
Pull request overview
This PR converts all [DllImport] P/Invoke declarations to [LibraryImport] for CoreCLR runtime interop, enabling source-generated marshalling code instead of runtime IL stub generation. This improves AOT compilation and reduces JIT overhead for .NET CoreCLR apps on Android.
Changes:
- Converted 18 P/Invokes in RuntimeNativeMethods.cs plus additional P/Invokes in Logger.cs, JNIEnvInit.cs, AndroidEnvironment.cs, JNIEnv.cs, and NativeAOT runtime files to use LibraryImport
- Changed
StringBuilderparameters tostringin gref/lref logging methods (safe for CoreCLR as native code treats these as const) - Added
[assembly: DisableRuntimeMarshalling]to NativeAOT project for struct reference parameters - Updated APK size baselines to reflect the ~4KB increase from generated marshalling code
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Mono.Android/Android.Runtime/RuntimeNativeMethods.cs | Converted 18 xa-internal-api P/Invokes to LibraryImport with StringMarshalling.Utf8 and UnmanagedCallConv attributes |
| src/Mono.Android/Android.Runtime/JNIEnvInit.cs | Converted xamarin_app_init P/Invoke to LibraryImport |
| src/Mono.Android/Android.Runtime/Logger.cs | Converted __android_log_print (liblog) P/Invoke to LibraryImport |
| src/Mono.Android/Android.Runtime/AndroidEnvironment.cs | Converted exit (libc) P/Invoke to LibraryImport |
| src/Mono.Android/Android.Runtime/JNIEnv.cs | Converted 3 libjnigraphics.so bitmap P/Invokes to LibraryImport |
| src/Mono.Android/Android.Runtime/AndroidRuntime.cs | Changed StringBuilder to string for gref/lref logging stack traces (safe for CoreCLR as native code doesn't modify) |
| src/Microsoft.Android.Runtime.NativeAOT/AssemblyInfo.cs | Added DisableRuntimeMarshalling assembly attribute required for LibraryImport with ref struct parameters |
| src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/Logging.cs | Converted __android_log_print P/Invoke to LibraryImport with fully-qualified CallConvCdecl |
| src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs | Converted 2 xa-internal-api P/Invokes to LibraryImport (missing UnmanagedCallConv attributes) |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64XFormsDotNet.MonoVM.apkdesc | Updated APK size baseline (+4KB) |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/Base/BuildReleaseArm64SimpleDotNet.MonoVM.apkdesc | Updated APK size baseline (+4KB) |
src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs
Show resolved
Hide resolved
src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs
Show resolved
Hide resolved
|
@copilot apply changes based on the comments in this thread |
…eclarations Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Added the missing |
Summary
Convert ALL
[DllImport]to[LibraryImport]for P/Invokes across the codebase.Changes
xa-internal-apiP/Invokesxamarin_app_init__android_log_print(liblog)exit(libc)libjnigraphics.sobitmap P/InvokesStringBuilder→string)LangVersion10 → 12 (required for LibraryImport source generator)[assembly: DisableRuntimeMarshalling](required for LibraryImport with ref struct parameter)Build Fixes
LangVersionto 12 forLibraryImportsource generator supportnew[] { typeof(...) })[assembly: DisableRuntimeMarshalling]for NativeAOT project (required when passing structs by reference with LibraryImport)[UnmanagedCallConv]attributes to NativeAOT P/Invokes for consistencyOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.