Skip to content

[Xamarin.Android.Tools.Bytecode] Warn on ambiguous Kotlin metadata - #13580

Merged
jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-kotlin-ambiguous-metadata-warning
Nov 18, 2026
Merged

jonathanpeppers merged 3 commits into
mainfrom
jonathanpeppers-kotlin-ambiguous-metadata-warning

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Context: #13577

Note

This PR is stacked on #13577 (stack #13584). Review only the commits in this layer.

KotlinFixups.FindKotlinFunctionMetadata() finds the Kotlin metadata function for each Java method. It had two problems when more than one Kotlin function matched:

  • The JvmSignature check and the constructed-signature check used SingleOrDefault(). With more than one match, it threw. The catch in KotlinFixups.Fixup() then logged Unable to parse Kotlin metadata with a stack trace and skipped all the remaining Kotlin fixups for the class.

  • The fallback type check returned the first match silently. For example, for this Kotlin code, kotlinc emits a synthetic bridge put(Ljava/lang/Object;)V:

    abstract class AmbiguousBase<T> { abstract fun put(value: T) }
    class AmbiguousOverloads : AmbiguousBase<String>() {
        @PublishedApi internal fun put(count: Int) { }
        override fun put(value: String) { }
    }

    Both put functions match the bridge. The internal function is first in the metadata, so the bridge was hidden.

Now all three checks collect every match. If more than one Kotlin function matches, the Java method gets no Kotlin fixups, and it is bound as a usual Java method. For a public or protected method, class-parse writes class-parse: warning XA4268: .... The <ClassParse/> task reports it as a coded MSBuild warning:

warning XA4268: The Kotlin metadata for Java method 'com/example/Widget.put(Ljava/lang/Object;)V' matches more than one Kotlin function: put(I)V, put(Ljava/lang/String;)V. The Kotlin fixups for this method are skipped.

Synthetic methods (such as bridges), private methods, and package-private methods are not bound API. For these methods, class-parse writes only a debug message. Normal Kotlin libraries have many bridges, so a warning for each one would only be noise.

The message is in the English Java.Interop.Localization resources, the same place as the other class-parse and generator messages. So Xamarin.Android.Tools.Bytecode now references Java.Interop.Localization. This assembly is already in the SDK tools folder next to class-parse.dll.

With code that kotlinc produced, the two signature checks did not find more than one match in the cases tested. A real ambiguity was seen only in the fallback check, for bridge methods.

Validation

A new Kotlin test fixture, AmbiguousOverloads.kt, is compiled by the existing Gradle step in kotlin-gradle.targets. Two tests in KotlinFixupsTests use it:

  • The synthetic bridge is not hidden, and there is no warning.
  • When the bridge is made public, there is one XA4268 warning, and the method is not hidden.

Both tests fail on the code before this change.

dotnet test tests/Xamarin.Android.Tools.Bytecode-Tests/Xamarin.Android.Tools.Bytecode-Tests.csproj

Result: Passed 135, Failed 0, Skipped 2 (on top of #13577).

I also ran class-parse.dll on the compiled AmbiguousOverloads.class. It wrote the debug message for the bridge, and it did not throw.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 18:46
@jonathanpeppers jonathanpeppers changed the title jonathanpeppers kotlin ambiguous metadata warning [Xamarin.Android.Tools.Bytecode] Warn on ambiguous Kotlin metadata Nov 16, 2026

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

Name-only JVM signature metadata can cause XA4268 to display an unrelated string instead of the parameter types needed to distinguish overload candidates.

1 open issue
1 file not reviewed
  • src/Java.Interop.Localization/Resources.Designer.cs: Generated file
What changed in this PR

This PR makes Kotlin metadata matching safer in .NET for Android’s bytecode tooling by skipping ambiguous function matches rather than applying potentially incorrect fixups.

Changes:

  • Adds localized warning XA4268 for ambiguous public methods, while keeping synthetic and non-public cases at debug level.
  • Adds a Kotlin fixture and tests for warning emission and synthetic-bridge handling.
  • Documents the warning and suggested remedies.
File Description
tests/​Xamarin.Android.Tools.Bytecode-Tests/​Xamarin.Android.Tools.Bytecode-Tests.csproj Embeds the new compiled Kotlin fixture.
tests/​Xamarin.Android.Tools.Bytecode-Tests/​KotlinFixupsTests.cs Tests ambiguous matches and warning behavior.
tests/​Xamarin.Android.Tools.Bytecode-Tests/​kotlin-gradle/​src/​main/​kotlin/​AmbiguousOverloads.kt Provides overloads with an ambiguous synthetic bridge.
tests/​Xamarin.Android.Tools.Bytecode-Tests/​kotlin-gradle.targets Tracks the fixture’s compiled output.
src/​Xamarin.Android.Tools.Bytecode/​Xamarin.Android.Tools.Bytecode.csproj References the localization project.
src/​Xamarin.Android.Tools.Bytecode/​Kotlin/​KotlinFixups.cs Detects ambiguous matches and reports diagnostics.
src/​Java.Interop.Localization/​Resources.resx Adds the English XA4268 message.
src/​Java.Interop.Localization/​Resources.Designer.cs Exposes the new resource accessor.
Documentation/​docs-mobile/​TOC.yml Adds warning navigation.
Documentation/​docs-mobile/​messages/​xa4268.md Explains XA4268 and remediation options.
Documentation/​docs-mobile/​messages/​index.md Lists the new warning.

Review effort: Balanced

@jonathanpeppers

Copy link
Copy Markdown
Member Author

@dalexsoto review

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed this layer's Kotlin ambiguity handling, diagnostic propagation, localization and SDK packaging, and fixture-generation integration. The resolved descriptor-handling issue is fixed, and the post-supplement final pass found no blocking issue. Approved for this layer only; this does not review or approve the parent #13577.

Base automatically changed from jonathanpeppers-kotlin-nullability-annotations to main November 17, 2026 23:15
jonathanpeppers and others added 3 commits November 17, 2026 18:15
`KotlinFixups.FindKotlinFunctionMetadata()` finds the Kotlin function
for each Java method. When more than one Kotlin function matched:

  * The `JvmSignature` and constructed-signature checks used
    `SingleOrDefault()`, which threw. The outer `catch` then skipped
    all remaining Kotlin fixups for the class.
  * The fallback type check returned the first match. For example,
    kotlinc emits a bridge `put(Ljava/lang/Object;)V` for a generic
    override. Both `put(String)` and an `@PublishedApi internal`
    `put(Int)` match it, and the internal one hid the bridge.

Now all three checks collect the matches. If more than one Kotlin
function matches, the Java method gets no Kotlin fixups. For a public
method, class-parse reports the new warning XA4268. For a synthetic
method, such as a bridge, it writes a debug message only, because the
method is not public API and a warning would be noise.

The message is in `Java.Interop.Localization`, as
`Xamarin.Android.Tools.Bytecode` now references that project.

Co-authored-by: Copilot App <[email protected]>
…methods

Remove comments and test assertions that add no value.

A rubber-duck review found that private and package-private methods
could also cause XA4268. These methods are not bound and get no Kotlin
fixups, so write only a debug message for them, as for synthetic
methods.

Keep the test assertions independent of the English message text, so
they also pass after the message is localized.

Co-authored-by: Copilot App <[email protected]>
For a function such as `@JvmName("renamed") fun original(count: Int)`,
kotlinc writes only the JVM name in the Kotlin metadata, not the
descriptor. `KotlinFunction.FromProtobuf()` then read string-table
entry 0 as `JvmSignature`, for example `xat/bytecode/tests/RenamedOnly;`.
XA4268 showed this text instead of the parameter types.

Set `JvmSignature` to `null` when the descriptor index is 0, as is
already done for the JVM name. `ConstructJvmSignature()` is then used.

Co-authored-by: Copilot App <[email protected]>
@jonathanpeppers
jonathanpeppers force-pushed the jonathanpeppers-kotlin-ambiguous-metadata-warning branch from 49d47fc to 5bfbf14 Compare November 17, 2026 23:15
@jonathanpeppers
jonathanpeppers merged commit dcbd008 into main Nov 18, 2026
38 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers-kotlin-ambiguous-metadata-warning branch November 18, 2026 15:17
simonrozsival added a commit that referenced this pull request Nov 21, 2026
The rebase onto cc7cbf6 kept the pre-rebase MSTest conversions of
files that main had changed since aa688ac, which reverted those
changes and broke the build. For each affected file, convert main's
old and new NUnit versions with the same converter and merge the
difference into the MSTest file:

  * cca255c (#13592): drop the Android lint tests and assertions.
  * 5b32fb1 (#13602): add RelativeDirectoriesUseProjectDirectory and
    the -mt CollectNonEmptyDirectories coverage.
  * 78ef747 (#13655): add OpenGenericProxiesKeepTheReflectionActivationConstructor.
  * 18b8940 (#13585): add the generator nullability/obsolete tests.
  * dcbd008 (#13580): add the AmbiguousFunctionMetadata_* tests.
  * 667e27a (#13621): add the sdkmanager package ID tests.
  * 5ecb8f7 (#13614): drop RunWithLoggingEnablesManagedTimingLogger.
  * 8287b17 (#13680), 4bfcc92 (#13681): restore the re-enabled
    tests and removed TODOs.
  * 5d81a0f (#13666): expect platform-tools 37.0.3.
  * 8442c75 (#13601): assert the pulled device TRX is deleted.

Also drop the EnableNUnitRunner/OutputType lines that 857f463
(#13563) added to Xamarin.Android.Build.Tests and MSBuildDeviceIntegration;
MSTestReferences.projitems sets both.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants