Repository navigation
Description
Android framework version
net10.0-android
Affected platform version
.NET 10 / Xamarin.AndroidX.Health.Connect.ConnectClient 1.1.0.2
Description
The AndroidX Health Connect binding exposes Kotlin suspend APIs as methods that require passing a raw Kotlin.Coroutines.IContinuation.
Example from the binding:
client.PermissionController.GetGrantedPermissions(IContinuation continuation)
client.ReadRecords(ReadRecordsRequest request, IContinuation continuation)This is technically callable, but difficult to use correctly from .NET. A .NET consumer has to hand-write a coroutine bridge using TaskCompletionSource, detect IntrinsicsKt.COROUTINE_SUSPENDED, implement IContinuation, unwrap Kotlin Result.Failure, map Java exceptions into CLR exceptions, and optionally bridge cancellation.
For .NET consumers, these APIs should ideally be projected as Task / Task methods.
Current required workaround:
static Task<T> Await<T>(Func<IContinuation, object> action, CancellationToken cancelToken)
{
var continuation = new Continuation<T>(cancelToken);
var result = action(continuation);
if (result != IntrinsicsKt.COROUTINE_SUSPENDED) { continuation.SetResult(result); }
return continuation.Task;
}
sealed class Continuation<T> : Java.Lang.Object, IContinuation
{
readonly TaskCompletionSource<T> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
readonly CancellationTokenRegistration ctr;
public Continuation(CancellationToken cancelToken) =>
ctr = cancelToken.Register(() => tcs.TrySetCanceled(cancelToken));
public Task<T> Task => tcs.Task;
public ICoroutineContext Context => EmptyCoroutineContext.Instance;
public void ResumeWith(Java.Lang.Object result) => SetResult(result);
public void SetResult(object result)
{
if (result is Java.Lang.Object { Class.Name: "kotlin.Result$Failure" } failure)
{
var field = failure.Class.GetDeclaredField("exception");
field.Accessible = true;
tcs.TrySetException(new Exception(field.Get(failure).ToString()));
}
else { tcs.TrySetResult((T)result); }
ctr.Dispose();
}
}Expected behavior:
Kotlin suspend APIs should have generated .NET-friendly overloads such as:
Task<ICollection<string>> GetGrantedPermissionsAsync(CancellationToken cancellationToken = default);
Task<ReadRecordsResponse> ReadRecordsAsync(ReadRecordsRequest request, CancellationToken cancellationToken = default);Actual behavior:
Only the raw continuation-based API is exposed.
Relevant official documentation:
Google’s Health Connect APIs are documented as Kotlin suspend functions. For example:
https://developer.android.com/reference/kotlin/androidx/health/connect/client/HealthConnectClient#readRecords(androidx.health.connect.client.request.ReadRecordsRequest)
Steps to Reproduce:
- Create a .NET Android project targeting net10.0-android36.0.
- Add Xamarin.AndroidX.Health.Connect.ConnectClient version 1.1.0.2.
- Try to call Health Connect suspend APIs such as GetGrantedPermissions or ReadRecords.
- Observe that the API requires IContinuation instead of returning Task.
Workaround:
Manually implement an IContinuation bridge with TaskCompletionSource, detect COROUTINE_SUSPENDED, unwrap kotlin.Result$Failure, and map the Java throwable to a CLR Exception.
Request:
Please consider projecting Kotlin suspend APIs in bound AndroidX packages as Task / Task methods, or provide an official helper/interop pattern for consuming suspend APIs from .NET.
Activity
Add CancellationToken support to SuspendBridge (#137)
jonathanpeppers commentedon Jun 8, 2026
I have an implementation of this in the project here:
Right now, I'm wondering if this should actually be addressed in dotnet/java-interop so these are always generated -- and if the code example you have above is what should be used.
tipa commentedon Jul 15, 2026
Update after reproducing this on a physical Android 10 device:
The
(T)resultcast in the workaround above is incorrect for Java interface types.ResumeWithcan receive a managedJava.Lang.Objectwhose underlying Java type implements the requested interface. For example, denying all Health Connect permissions returnskotlin.collections.EmptySet. That object implementsJava.Util.ICollection, but its managed wrapper cannot be converted using a normal C# cast, resulting in:Java/JNI-aware conversion is required, for example:
with an appropriate constraint such as:
Alternatively, the continuation can retain the result as
Java.Lang.Objectand accept an explicit unboxing/conversion delegate. The implementation linked in jonathanpeppers/Microsoft.AndroidX.Compose already follows that second approach and therefore does not repeat the invalid generic cast.So the workaround in the original issue should not be used as-is for generated
Task<T>projections. Any generated implementation needs JNI-aware result conversion.