Skip to content

[Mono.Android] Remove always-enabled JAVA_INTEROP conditionals - #13092

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-interop-conditional-cleanup
Oct 13, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
simonrozsival-interop-conditional-cleanup

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Before this change, Mono.Android and Mono.Android.Runtime
unconditionally define JAVA_INTEROP. The remaining conditional
branches therefore obscure which implementations actually ship and
retain unreachable alternatives.

Remove the redundant guards from Android.Runtime.AndroidRuntime and
Android.Runtime.ManagedObjectReferenceManager, the inactive exception
helpers in Android.Runtime.AndroidEnvironment, and the inactive raw JNI
alternatives in Android.Widget.AbsListViewInvoker and
Java.Interop.Runtime.LocalReferenceCount. Remove the two project
defines after auditing all tracked consumers, linked sources, templates,
and generator output. The review follow-up also removes the unread
private AbsListView.id_getAdapter cache and its redundant CS0169
suppressions.

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 as
44f4f5e842b90cfebd01b2eff92f95ed149d3568. Review-follow-up validation is
listed separately below. Existing suites provide coverage; no tests
asserting source absence were added.

Initial cleanup check Result
Baseline and changed Release SDK builds Passed
Explicit net11.0 Mono.Android / Mono.Android.Runtime builds Passed
API 37, 37.1, and 37.2 builds; PublicApiAnalyzers / ApiCompat enabled Passed; ApiCompat reported zero issues
Existing throwable-packaging host tests, CoreCLR and NativeAOT 2 passed, 0 failed, 0 skipped
Full CoreCLR suite, isolated API 35 arm64 emulator 941 passed, 0 failed, 8 skipped
Full NativeAOT suite, same emulator 887 passed, 0 failed, 7 skipped
Active-source equivalence, project XML parsing, git diff --check Passed

TRX inspection confirms exception, transferred-reference, adapter,
reference-manager, GC bridge, conversion, export, and JNI coverage.
ExceptionTest passed 15 CoreCLR / 14 NativeAOT cases;
TransferredReferenceTests passed 76 per runtime;
AdapterTests passed 6 per runtime; and
ManagedObjectReferenceManagerTests passed 9 per runtime.

Initial cleanup commands and results
Command Result
make prepare CONFIGURATION=Release MSBUILD_ARGS=-m:2 Passed
make all CONFIGURATION=Release 'MSBUILD_ARGS=-m:2 -p:NinjaPath=/Users/simon/.copilot/session-state/62065bc8-b121-4727-a6c9-8733abcb6d4d/files/ninja-j2' Baseline and changed builds passed; session-only wrapper invokes the prepared Ninja executable with -j2
./dotnet-local.sh build src/Mono.Android.Runtime/Mono.Android.Runtime.csproj --no-restore -c Release -f net11.0 -m:2 -v:q Baseline and changed builds passed
./dotnet-local.sh build src/Mono.Android/Mono.Android.csproj --no-restore -c Release -f net11.0 -m:2 -v:q Baseline and changed builds passed
./dotnet-local.sh build build-tools/scripts/Prepare.proj -t:BuildExtraApiLevels -c Release -m:2 -v:q Baseline and changed API 37.1 / 37.2 builds passed
NUNIT_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-results 2 passed
ANDROID_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.csproj Passed
ANDROID_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-results 941 passed, 8 skipped
ANDROID_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.csproj Passed
ANDROID_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-results 887 passed, 7 skipped

The device test commands ran from
tests/Mono.Android-Tests/Mono.Android-Tests. The initial clean-tree
workload setup required the additional API reference assemblies; the
existing BuildExtraApiLevels target supplied them and the unchanged
make all retry passed.

Skipped on both runtimes: DoNotLeakWeakReferences (existing ignore),
NewObjectArrayWithBadValues (emulator crash ignore),
InflateCustomView_ShouldNotLeakGlobalRefs (existing leak ignore),
UnregisterFromRuntime (existing ignore), and
References_CreatedReference_GlobalRef / _LocalRef (the built
Java.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 ignored
itself after a task timeout; that networking case was not validated).
NativeAOT additionally skipped GCBridgeRoundEmission, which exercises
the 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

fee7180d77d0643eb80dad1ac0e0604a2b4ce90c removes only the unread private
adapter field and three redundant warning directives. The tested and
committed tree is d654b67730d9f1934a738d6b1e8b5673c614a268.

Follow-up command Result
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.binlog Passed
DOTNET_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.binlog Passed
DOTNET_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 API 37.1 / 37.2 passed; existing warnings, zero errors
./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 Before rebuilding the extra APIs, a targeted comparison of the pre-follow-up API 37.1 assembly against updated API 37 confirmed all 360 AbsListView and 8 invoker method bodies, locals, and exception regions unchanged
./dotnet-local.sh bin/BuildRelease/jnienv-gen.dll --use-java-interop -o=- Passed; output does not call the deleted helper
./dotnet-local.sh bin/BuildRelease/jnienv-gen.dll -o=- Passed; legacy output still references the helper that was already unavailable in the baseline
git diff --check Passed

Public 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_INTEROP while Mono.Android.csproj always defines the symbol.
The sole in-tree _BuildJNIEnv invocation in
build-tools/create-android-api/create-android-api.csproj:158-164
explicitly passes --use-java-interop. Historical default-mode external
usage 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.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Keep the existing Java.Interop implementations unconditionally, remove the inactive alternatives, and drop the unused project defines.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 21:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_INTEROP definitions.
  • 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;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 12, 2026
@simonrozsival
simonrozsival enabled auto-merge (squash) October 12, 2026 21:26
@simonrozsival simonrozsival removed the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 12, 2026
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]>
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Oct 13, 2026
simonrozsival added a commit that referenced this pull request Oct 13, 2026
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.
@simonrozsival
simonrozsival merged commit 3cc4dd8 into main Oct 13, 2026
42 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-interop-conditional-cleanup branch October 13, 2026 13:16
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants