java.interop icon indicating copy to clipboard operation
java.interop copied to clipboard

error BG0000: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Open tuyen-vuduc opened this issue 1 year ago • 8 comments

Errror when creating binding library for io.ktor:ktor-utils-jvm:2.3.8

error BG0000: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
         at Mono.Collections.Generic.Collection`1.get_Item(Int32 index)
         at Java.Interop.Tools.JavaTypeSystem.ManagedApiImporter.ParseMethod(MethodDefinition method, JavaTypeModel declaringType, ApiImporterOptions options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.JavaTypeSystem/Adap
      ters/ManagedApiImporter.cs:line 195
         at Java.Interop.Tools.JavaTypeSystem.ManagedApiImporter.ParseClass(TypeDefinition type, JavaTypeCollection collection, ApiImporterOptions options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.JavaTypeSystem/Adapter
      s/ManagedApiImporter.cs:line 123
         at Java.Interop.Tools.JavaTypeSystem.ManagedApiImporter.ParseType(TypeDefinition type, JavaTypeCollection collection, ApiImporterOptions options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.JavaTypeSystem/Adapters
      /ManagedApiImporter.cs:line 44
         at Java.Interop.Tools.JavaTypeSystem.ManagedApiImporter.Parse(AssemblyDefinition assembly, JavaTypeCollection collection, TypeDefinitionCache resolver, ApiImporterOptions options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Int
      erop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs:line 22
         at generator.JavaTypeResolutionFixups.Fixup(String xmlFile, String outputXmlFile, DirectoryAssemblyResolver resolver, String[] references, TypeDefinitionCache cache, CodeGeneratorOptions opt) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/
      tools/generator/Java.Interop.Tools.Generator.Transformation/JavaTypeResolutionFixups.cs:line 32
         at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options, DirectoryAssemblyResolver resolver) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/tools/generator/CodeGenerator.cs:line 116
         at Xamarin.Android.Binder.CodeGenerator.Run(CodeGeneratorOptions options) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/tools/generator/CodeGenerator.cs:line 50
         at Xamarin.Android.Binder.CodeGenerator.Main(String[] args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/tools/generator/CodeGenerator.cs:line 33

tuyen-vuduc avatar Jan 11 '25 12:01 tuyen-vuduc

Can you provide a minimal repro? I do not see this error with the following project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net9.0-android</TargetFramework>
    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
    <RootNamespace>issue_1292</RootNamespace>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <IsTrimmable>true</IsTrimmable>
  </PropertyGroup>
  
  <ItemGroup>
    <AndroidMavenLibrary Include="io.ktor:ktor-utils-jvm" Version="2.3.8" />
    <AndroidMavenLibrary Include="org.slf4j:slf4j-api" Version="1.7.36" />
    <AndroidMavenLibrary Include="io.ktor:ktor-io-jvm" Version="2.3.8" />
  </ItemGroup>
  
  <ItemGroup>
    <PackageReference Include="Xamarin.Kotlin.StdLib" Version="2.0.21.1" />
    <PackageReference Include="Xamarin.Kotlin.StdLib.Common" Version="2.0.21.1" />
    <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk7" Version="2.0.21.1" />
    <PackageReference Include="Xamarin.Kotlin.StdLib.Jdk8" Version="2.0.21.1" />
    <PackageReference Include="Xamarin.KotlinX.Coroutines.Android" Version="1.9.0.1" />
    <PackageReference Include="Xamarin.KotlinX.Coroutines.Core.Jvm" Version="1.9.0.1" />
    <PackageReference Include="Xamarin.KotlinX.Coroutines.Jdk8" Version="1.9.0.1" />
  </ItemGroup>
</Project>

jpobst avatar Jan 13 '25 19:01 jpobst

Looking at the relevant code:

https://github.com/dotnet/java-interop/blob/4f06201b598ac3816184ea7a0b71f540f6abe9d6/src/Java.Interop.Tools.JavaTypeSystem/Adapters/ManagedApiImporter.cs#L194-L195

This appears to be a case where:

  • We are trying to import a referenced Android binding .dll.
  • A method in that .dll has a [Register] signature that specifies that the method has N parameters, but the method actually has a different number of parameters.

Example: here (I) means the method takes a single int parameter, but the managed method actually takes no parameters.

[Register ("doThing", "(I)", "")]
public unsafe void DoThing () { ... }

jpobst avatar Jan 13 '25 19:01 jpobst

@jpobst Here is the binding source code.

BTW, I attempted to debug this source, but failed following the guidelines. Can you advise?

io.ktorktor-utils-jvm.2.3.8.zip

tuyen-vuduc avatar Jan 15 '25 22:01 tuyen-vuduc

I get this error trying to build this project:

C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.0.145\tools\Xamarin.Android.Common.targets(893,3): error XACDJ7028: System.IO.FileNotFoundException: Could not find file 'C:\Users\jopobst\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.36\6c62681a2f655b49963a5983b8b0950a6120ae14\slf4j-api-1.7.36.jar'.
File name: 'C:\Users\jopobst\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.36\6c62681a2f655b49963a5983b8b0950a6120ae14\slf4j-api-1.7.36.jar'
   at System.IO.FileInfo.get_Length()
   at Xamarin.Android.Tasks.MonoAndroidHelper.SizeAndContentFileComparer.GetHashCode(FileInfo obj)
   at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value, Int32& location)
   at System.Collections.Generic.HashSet`1.Add(T item)
  ...

I'm not sure how this file is supposed to get to that path, though I do have a 1.7.30 version there.

jpobst avatar Jan 17 '25 18:01 jpobst

@jpobst Can you install Gradle and/or Android Studio? The other dependencies require gradle to download Android artifacts.

tuyen-vuduc avatar Jan 18 '25 03:01 tuyen-vuduc

@jpobst Have you been able to check it out? Any chances to guide me to debug on my machine?

tuyen-vuduc avatar Jan 20 '25 16:01 tuyen-vuduc

@jpobst Can you install Gradle and/or Android Studio? The other dependencies require gradle to download Android artifacts.

I already have Android Studio on my machine, so it apparently requires something more than that.

Any chances to guide me to debug on my machine?

To debug locally, you would need to:

  • Clone this repository
  • Get Java.Interop.sln building on the command line
  • Open generator.slnf in VS
  • Get the failing command line from your failing build (likely using binlog)
  • Debug in VS using that startup command line

jpobst avatar Jan 21 '25 18:01 jpobst

@jpobst I managed to build and run this repo. however, I tried to replicate the issue, it shows error differently. Can you guide how we can by pass this error for common libs? And/or jump into JavaInterop source during the build in VS?

Image

tuyen-vuduc avatar Jun 15 '25 23:06 tuyen-vuduc