dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

[Xamarin.Android.Build.Tasks] Add @(AndroidGradleProject) action #9270

Open pjcollins opened 3 weeks ago

pjcollins commented 3 weeks ago

Context: https://github.com/CommunityToolkit/Maui.NativeLibraryInterop

Introduces an @(AndroidGradleProject) build action which can be used to build and consume the outputs of Android Gradle projects.

The following metadata is supported on this item:

<AndroidGradleProject Include="path/to/projectdir">
  <Bind></Bind>
  <Configuration>Release</Configuration>
  <ModuleName></ModuleName>
  <OutputPath>$(IntermediateOutputPath)gradle/{ModuleName}-{Hash}</OutputPath>
  <Pack></Pack>
  <CreateAndroidLibrary>true</CreateAndroidLibrary>
</AndroidGradleProject>

The ItemSpec or Include attribute should be the path to the root build.gradle file of the project, next to gradlew wrapper scripts.

If the @(AndroidGradleProject) declares a library module that produces an AAR, that AAR will be added as an @(AndroidLibrary) item automatically.

The Bind, Pack, and Visible metadata will be applied to the @(AndroidLibrary) item if it is created, using the defaults outlined above.

A new $(AndroidPrepareForBuildDependsOn) build extension point has been added to allow customer projects to more easily hook into the beginning of the build process.

pjcollins commented 2 weeks ago

Opening this up for some initial feedback, though I am still working on issues related to dotnet pack.

jpobst commented 2 weeks ago

How does this handle dependencies? Let's say my gradle project depends on org.jetbrains.kotlin:kotlin-stdlib:2.0.0.

Is this:

pjcollins commented 2 weeks ago

How does this handle dependencies? Let's say my gradle project depends on org.jetbrains.kotlin:kotlin-stdlib:2.0.0.

Is this:

  • Dependency is left entirely up to the user, in this case they would need to add our NuGet package to their project.
  • Native dependency is automatically included, which would conflict with any usage of our NuGet package.
  • Something else?

There is currently no dependency management functionality, it's left up to the user to figure out what is needed and what is best. This is a scenario I am hoping we can try to improve in the future, perhaps through recommending some combination of NuGet or AndroidMavenLibrary references. There are also likely different use cases for application vs library projects where we could potentially try to do some automatic dependency inclusion in the former case.

pjcollins commented 2 weeks ago

Looks nice 😄 It might be worth doing a blog post for this to show people how to use it 😄

We have a "v1" of sorts out in https://github.com/CommunityToolkit/Maui.NativeLibraryInterop and https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/native-library-interop , and will be planning to update these with this functionality when it's available

jpobst commented 1 week ago

There is currently no dependency management functionality, it's left up to the user to figure out what is needed and what is best. This is a scenario I am hoping we can try to improve in the future, perhaps through recommending some combination of NuGet or AndroidMavenLibrary references.

If a .pom file is produced by the gradle project, adding it to the AndroidLibrary with the Manifest attribute will opt-into our Java dependency verification which would provide dependency enforcement and NuGet package suggestions:

https://review.learn.microsoft.com/en-us/dotnet/android/binding-libs/advanced-concepts/java-dependency-verification?branch=binding-docs

pjcollins commented 1 week ago

There is currently no dependency management functionality, it's left up to the user to figure out what is needed and what is best. This is a scenario I am hoping we can try to improve in the future, perhaps through recommending some combination of NuGet or AndroidMavenLibrary references.

If a .pom file is produced by the gradle project, adding it to the AndroidLibrary with the Manifest attribute will opt-into our Java dependency verification which would provide dependency enforcement and NuGet package suggestions:

https://review.learn.microsoft.com/en-us/dotnet/android/binding-libs/advanced-concepts/java-dependency-verification?branch=binding-docs

Excellent, I will try to look into this after the initial work lands.