Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

  • Fix the net.android.init.gradle.kts resource file to handle nullable case using Kotlin's ?.let {} pattern
  • Remove useless content-checking test from CopyResourceTests.cs
  • Add AgpVersion and GradleVersion properties to AndroidGradleProject for version configuration
  • Add proper end-to-end integration test BindLibraryWithMultipleGradleVersions that tests binding projects with multiple AGP/Gradle version combinations
  • Use existing $(_AGPInitScriptPath) property as escape hatch for custom init scripts

Summary

This PR fixes the generated net.android.init.gradle.kts Kotlin DSL script to be compatible with Gradle 9.x by using Kotlin's null-safe ?.let {} pattern instead of explicitly checking containsKey and then accessing the potentially nullable value.

Escape Hatch Property

Users can now provide their own custom init.gradle.kts file by overriding the existing $(_AGPInitScriptPath) property:

<PropertyGroup>
  <_AGPInitScriptPath>$(MSBuildProjectDirectory)/My.init.gradle.kts</_AGPInitScriptPath>
</PropertyGroup>

When set to a path outside the intermediate output directory, the build will skip copying the built-in resource and use the user's file directly.

Integration Tests

The integration test verifies compatibility across multiple AGP/Gradle versions:

  • AGP 8.5.0 with Gradle 8.7
  • AGP 8.7.0 with Gradle 8.9
  • AGP 8.8.0 with Gradle 8.10.2
  • AGP 8.9.0 with Gradle 8.11.1
  • AGP 9.0.0 with Gradle 9.1 (tests the Gradle 9.x stricter Kotlin type checking fix)
Original prompt

This section details on the original issue you should resolve

<issue_title>Generated net.android.init.gradle.kts incompatible with Gradle 9.x (Kotlin type safety)</issue_title>
<issue_description>## Summary

Microsoft.Android.Sdk generates Kotlin DSL scripts (net.android.init.gradle.kts) that are incompatible with Gradle 9.x due to stricter Kotlin type checking.

Error

When building a .NET Android project with Gradle 9.x, the following error occurs:

e: file:///.../obj/Debug/net10.0-android/gradle/net.android.init.gradle.kts:11:36: 
Argument type mismatch: actual type is 'String?', but 'Any' was expected.

FAILURE: Build failed with an exception.

* Where:
Initialization script '.../obj/Debug/net10.0-android/gradle/net.android.init.gradle.kts' line: 11

* What went wrong:
Script compilation error:

  Line 11: layout.buildDirectory.set(file(gradle.startParameter.projectProperties["netAndroidBuildDirOverride"]))
                                          ^ Argument type mismatch: actual type is 'String?', but 'Any' was expected.

Root Cause

The generated code in net.android.init.gradle.kts line 11:

layout.buildDirectory.set(file(gradle.startParameter.projectProperties["netAndroidBuildDirOverride"]))
  • gradle.startParameter.projectProperties["..."] returns String? (nullable)
  • Gradle 9.x's file() function requires Any (non-nullable)
  • Gradle 9.x has stricter Kotlin type checking and rejects the nullable type

Suggested Fix

Handle the nullable case in the generated Kotlin script:

gradle.startParameter.projectProperties["netAndroidBuildDirOverride"]?.let { 
    layout.buildDirectory.set(file(it)) 
}

Environment

  • Microsoft.Android.Sdk.Darwin: 36.0.0-preview.7.229
  • Gradle: 9.0.0
  • Target Framework: net10.0-android

Impact

This blocks .NET Android/MAUI projects from upgrading to Gradle 9.x and AGP 9.x.

Related

<agent_instructions>If we fix this,

  • We need to detect both old & new gradle formats -- both need to work.

  • We should write a test first that reproduces the issue.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix generated net.android.init.gradle.kts for Gradle 9.x Fix net.android.init.gradle.kts for Gradle 9.x compatibility Jan 28, 2026
Copilot AI requested a review from jonathanpeppers January 28, 2026 20:21
Copilot AI and others added 2 commits January 28, 2026 21:04
…test

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
…nit scripts

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
…new public property

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
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.

Generated net.android.init.gradle.kts incompatible with Gradle 9.x (Kotlin type safety)

2 participants