Repository navigation
[Xamarin.Android.Tools.Bytecode] Warn on ambiguous Kotlin metadata - #13580
Conversation
There was a problem hiding this comment.
🟡 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
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
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.
`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]>
49d47fc to
5bfbf14
Compare
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]>

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
JvmSignaturecheck and the constructed-signature check usedSingleOrDefault(). With more than one match, it threw. ThecatchinKotlinFixups.Fixup()then loggedUnable to parse Kotlin metadatawith 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:Both
putfunctions match the bridge. Theinternalfunction 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: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.Localizationresources, the same place as the other class-parse and generator messages. SoXamarin.Android.Tools.Bytecodenow referencesJava.Interop.Localization. This assembly is already in the SDKtoolsfolder next toclass-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 inkotlin-gradle.targets. Two tests inKotlinFixupsTestsuse it:Both tests fail on the code before this change.
Result: Passed 135, Failed 0, Skipped 2 (on top of #13577).
I also ran
class-parse.dllon the compiledAmbiguousOverloads.class. It wrote the debug message for the bridge, and it did not throw.