Repository navigation
[Mono.Android] Remove always-enabled JAVA_INTEROP conditionals - #13092
Conversation
Keep the existing Java.Interop implementations unconditionally, remove the inactive alternatives, and drop the unused project defines. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The default jnienv-gen mode still generates a call to a helper removed by this PR.
2 open findings
What changed in this PR
Removes obsolete JAVA_INTEROP conditionals while preserving active Java.Interop implementations.
Changes:
- Removes project-level
JAVA_INTEROPdefinitions. - Deletes inactive JNI and exception-handling alternatives.
- Makes runtime/reference-manager implementations unconditional.
| File | Description |
|---|---|
src/Mono.Android/Mono.Android.csproj |
Removes the obsolete constant. |
src/Mono.Android.Runtime/Mono.Android.Runtime.csproj |
Removes the obsolete runtime constant. |
src/Mono.Android/Java.Interop/Runtime.cs |
Retains the active reference counter. |
src/Mono.Android/Android.Widget/AbsListView.cs |
Retains Java.Interop adapter invocation. |
src/Mono.Android/Android.Runtime/ManagedObjectReferenceManager.cs |
Makes reference management unconditional. |
src/Mono.Android/Android.Runtime/AndroidRuntime.cs |
Makes runtime integration unconditional. |
src/Mono.Android/Android.Runtime/AndroidEnvironment.cs |
Removes inactive exception helpers. |
🧠 Review effort: Balanced
| } | ||
| #endif // !JAVA_INTEROP | ||
|
|
||
| public static event EventHandler<RaiseThrowableEventArgs>? UnhandledExceptionRaiser; |
There was a problem hiding this comment.
The legacy default generator does still emit that call, but its output was already incompatible with the pre-PR source: at base 290a2824be, this helper is inside #if !JAVA_INTEROP (AndroidEnvironment.cs:77–99), while Mono.Android.csproj:16 unconditionally defines JAVA_INTEROP. This PR therefore does not remove a helper that was compiled into the baseline assembly.
The in-tree regeneration path is _BuildJNIEnv in build-tools/create-android-api/create-android-api.csproj:158–164, which explicitly passes --use-java-interop. I ran both generator modes: that supported invocation emits no call to the deleted helper; the legacy default emits the already-unavailable call. The rebuilt SDK/API gates and full CoreCLR/NativeAOT runtime suites passed on the original cleanup.
I am keeping generator mode/CLI changes out of this independent conditional cleanup rather than reinstating unreachable code. If the concern is compatibility for external users of the historical default mode, that is a separate tool-contract decision; the in-tree invocation alone does not establish whether such users exist. Leaving this thread open for that distinction.
Note
Auto-replied by the GitHub Copilot app.
Drop the unread legacy getAdapter method-ID field and its redundant CS0169 warning suppressions. Callback delegates and adapter invocation bodies remain unchanged. Co-authored-by: Copilot App <[email protected]>
|
@dalexsoto review |
The product generates `Android.Runtime.JNIEnv` through Java.Interop. The unused raw JNI backend is no longer usable and still emits a call to the unavailable exception-lookup helper. Delete that backend instead of maintaining unreachable alternate implementations. Remove the raw JNI struct/invoker, delegates, `Env` wrappers, exception and null-check helpers, and exclusive prototype/throw/null-check metadata. Keep the complete shared JNI function table, current forwarders, params overloads, and hand-bound skips. The authoritative metadata input has no in-tree producer; rename it from `Generator.g.cs` to `JNIEnvEntries.cs` to distinguish it from generated output. **Intentional tooling contract change:** retire the legacy default mode and `--use-java-interop` mode selection. Output is now always Java.Interop. Update the in-tree invocation and directly related docs. There are no shipped runtime behavior, public API, ownership, GC, or nullability changes, and no changes under `external/Java.Interop`. Independent follow-up to the discussion on [#13092](#13092 (comment)), not a prerequisite or stacked PR. Base: `c36449d35ab022bd78230c474ad63d254cd8d2e8` (`main`). Validated commit: `4593581d1ab37051d0e26ef8675c3e28b4968acb`; tested source tree: `a8ed4e32ce0a824fa52dccbc0df99854d08f3f16`. ### Output equivalence Before edits, build the generator in this worktree and save `--use-java-interop` output. After edits, build and run without the flag. `cmp` passes for file output, `-o=-`, and the actual generated `src/Mono.Android/Android.Runtime/JNIEnv.g.cs`. No generated output was hand-edited or included in this PR. Generation/comparison commands (baseline commands run before edits): ```sh ARTIFACTS=/Users/simon/.copilot/session-state/a7bac364-f7fc-4856-8a8d-8854f5b64b8f/files dotnet bin/BuildDebug/jnienv-gen.dll --use-java-interop -o "$ARTIFACTS/JNIEnv.baseline.cs" dotnet bin/BuildDebug/jnienv-gen.dll --use-java-interop -o=- > "$ARTIFACTS/JNIEnv.baseline.stdout.cs" dotnet bin/BuildDebug/jnienv-gen.dll -o "$ARTIFACTS/JNIEnv.cleanup.cs" dotnet bin/BuildDebug/jnienv-gen.dll -o=- > "$ARTIFACTS/JNIEnv.cleanup.stdout.cs" cmp "$ARTIFACTS/JNIEnv.baseline.cs" "$ARTIFACTS/JNIEnv.cleanup.cs" cmp "$ARTIFACTS/JNIEnv.baseline.stdout.cs" "$ARTIFACTS/JNIEnv.cleanup.stdout.cs" cmp src/Mono.Android/Android.Runtime/JNIEnv.g.cs "$ARTIFACTS/JNIEnv.baseline.cs" ``` All commands passed. | Output | Bytes | SHA-256 before and after | |---|---:|---| | File / actual `JNIEnv.g.cs` | 40,237 | `3e3d86192ef85b16540a17b8e0b8fb708854727c030521423698e3de80e98bec` | | Stdout (`-o=-`) | 40,238 | `9b9508c4b60fe515e21e306e36314294d5593b17142eaa2864118cdf239361d7` | LF newlines and stdout's additional trailing newline are preserved. Executable checks also pass for short/long output options, `--root` with default output and with stdout, unchanged-file timestamps, and write failures returning 1 with the original exception diagnostic.


Before this change,
Mono.AndroidandMono.Android.Runtimeunconditionally define
JAVA_INTEROP. The remaining conditionalbranches therefore obscure which implementations actually ship and
retain unreachable alternatives.
Remove the redundant guards from
Android.Runtime.AndroidRuntimeandAndroid.Runtime.ManagedObjectReferenceManager, the inactive exceptionhelpers in
Android.Runtime.AndroidEnvironment, and the inactive raw JNIalternatives in
Android.Widget.AbsListViewInvokerandJava.Interop.Runtime.LocalReferenceCount. Remove the two projectdefines after auditing all tracked consumers, linked sources, templates,
and generator output. The review follow-up also removes the unread
private
AbsListView.id_getAdaptercache and its redundant CS0169suppressions.
The previously active method implementations remain unchanged. Public
APIs, reference counters, exception handling, adapter ownership, and
other conditional symbols are preserved. This does not remove the live
Java.Interop dependency or change
JAVA_INTEROP1.Validation
The initial cleanup used the SDK rebuilt from this branch, not another
worktree. Its tested source tree is
87a6a8efb9e4808134a3f53b151fbd5a317d21f2, which is the tree committed as44f4f5e842b90cfebd01b2eff92f95ed149d3568. Review-follow-up validation islisted separately below. Existing suites provide coverage; no tests
asserting source absence were added.
net11.0Mono.Android / Mono.Android.Runtime buildsgit diff --checkTRX inspection confirms exception, transferred-reference, adapter,
reference-manager, GC bridge, conversion, export, and JNI coverage.
ExceptionTestpassed 15 CoreCLR / 14 NativeAOT cases;TransferredReferenceTestspassed 76 per runtime;AdapterTestspassed 6 per runtime; andManagedObjectReferenceManagerTestspassed 9 per runtime.Initial cleanup commands and results
make prepare CONFIGURATION=Release MSBUILD_ARGS=-m:2make all CONFIGURATION=Release 'MSBUILD_ARGS=-m:2 -p:NinjaPath=/Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/ninja-j2'-j2./dotnet-local.sh build src/Mono.Android.Runtime/Mono.Android.Runtime.csproj --no-restore -c Release -f net11.0 -m:2 -v:q./dotnet-local.sh build src/Mono.Android/Mono.Android.csproj --no-restore -c Release -f net11.0 -m:2 -v:q./dotnet-local.sh build build-tools/scripts/Prepare.proj -t:BuildExtraApiLevels -c Release -m:2 -v:qNUNIT_MSBUILD_ARGS=-m:2 ./dotnet-local.sh test bin/TestRelease/net10.0/Xamarin.Android.Build.Tests.dll --filter 'Name~TrimmableTypeMap_PackagesAndroidJavaProxyThrowable' --logger 'trx;LogFileName=interop-host-proxy.trx' --results-directory /Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/host-resultsANDROID_SERIAL=emulator-5598 ./dotnet-local.sh build -t:Install -c Release -p:RuntimeIdentifier=android-arm64 -p:PublishAot=false '-p:AdbTarget=-s emulator-5598' -m:2 -v:q tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csprojANDROID_SERIAL=emulator-5598 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:RuntimeIdentifier=android-arm64 -p:PublishAot=false '-p:AdbTarget=-s emulator-5598' --device emulator-5598 --report-trx --results-directory /Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/coreclr-device-resultsANDROID_SERIAL=emulator-5598 ./dotnet-local.sh build -t:Install -c Release -p:RuntimeIdentifier=android-arm64 -p:PublishAot=true '-p:AdbTarget=-s emulator-5598' -m:2 -v:q tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csprojANDROID_SERIAL=emulator-5598 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Release -p:RuntimeIdentifier=android-arm64 -p:PublishAot=true '-p:AdbTarget=-s emulator-5598' --device emulator-5598 --report-trx --results-directory /Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/nativeaot-device-resultsThe device test commands ran from
tests/Mono.Android-Tests/Mono.Android-Tests. The initial clean-treeworkload setup required the additional API reference assemblies; the
existing
BuildExtraApiLevelstarget supplied them and the unchangedmake allretry passed.Skipped on both runtimes:
DoNotLeakWeakReferences(existing ignore),NewObjectArrayWithBadValues(emulator crash ignore),InflateCustomView_ShouldNotLeakGlobalRefs(existing leak ignore),UnregisterFromRuntime(existing ignore), andReferences_CreatedReference_GlobalRef/_LocalRef(the builtJava.Interop does not use P/Invoke-based JNI invokes).
CoreCLR additionally skipped
java_lang_Object_Is_Java_Lang_Object(existing CoreCLR ignore) and
CancelRequestViaProxy(the test ignoreditself after a task timeout; that networking case was not validated).
NativeAOT additionally skipped
GCBridgeRoundEmission, which exercisesthe managed CoreCLR bridge. Built-in runtime category exclusions remain
in effect. The disposable emulator, its test app, and its isolated AVD
were cleaned up after validation.
Review follow-up
fee7180d77d0643eb80dad1ac0e0604a2b4ce90cremoves only the unread privateadapter field and three redundant warning directives. The tested and
committed tree is
d654b67730d9f1934a738d6b1e8b5673c614a268.DOTNET_CLI_DO_NOT_USE_MSBUILD_SERVER=1 ./dotnet-local.sh build src/Mono.Android/Mono.Android.csproj --no-restore --disable-build-servers -c Release -f net11.0 -m:1 -v:q -bl:/Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/review-mono-isolated.binlogDOTNET_CLI_DO_NOT_USE_MSBUILD_SERVER=1 ./dotnet-local.sh build src/Mono.Android.Runtime/Mono.Android.Runtime.csproj --no-restore --disable-build-servers -c Release -f net11.0 -m:1 -v:q -bl:/Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/review-runtime-isolated.binlogDOTNET_CLI_DO_NOT_USE_MSBUILD_SERVER=1 ./dotnet-local.sh build build-tools/scripts/Prepare.proj -t:BuildExtraApiLevels --disable-build-servers -c Release -m:1 -v:q -bl:/Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/review-extra-api-isolated.binlog./dotnet-local.sh /Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/adapter-il-check/bin/Debug/net11.0/adapter-il-check.dll bin/Release/lib/xamarin.android/xbuild-frameworks/Microsoft.Android/37.1/Mono.Android.dll bin/Release/lib/xamarin.android/xbuild-frameworks/Microsoft.Android/37/Mono.Android.dll./dotnet-local.sh bin/BuildRelease/jnienv-gen.dll --use-java-interop -o=-./dotnet-local.sh bin/BuildRelease/jnienv-gen.dll -o=-git diff --checkPublic API gates remain enabled. The IL comparison resolves metadata
operands by member/type names and normalizes branch targets rather than
comparing shifted metadata-token bytes. It uses the existing Mono.Cecil
dependency in a session-only inspection tool; no product code or permanent
tests were added for it. No full SDK or device suite was rerun for this
private unread-field-only follow-up. The initial runtime results above
are prior evidence, not a claim of a fresh device run at the new commit.
The generator review concern is pre-existing rather than introduced by
this deletion: at base
290a2824be, the helper is inside#if !JAVA_INTEROPwhileMono.Android.csprojalways defines the symbol.The sole in-tree
_BuildJNIEnvinvocation inbuild-tools/create-android-api/create-android-api.csproj:158-164explicitly passes
--use-java-interop. Historical default-mode externalusage is a separate tool-contract question; this PR does not retire that
CLI mode, and the corresponding review thread remains open for that
distinction.
No issue is closed by this structural cleanup.
Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include: