openrewrite / rewrite-gradle-plugin

OpenRewrite's Gradle plugin.
Apache License 2.0
56 stars 34 forks source link

Support Android sourcesets as defined by the Android Gradle Plugin #289

Open wbonnefond opened 4 months ago

wbonnefond commented 4 months ago

What problem are you trying to solve?

The OpenRewrite gradle plugin does not work with Android projects because it has no knowledge of sourcesets defined by the Android Gradle Plugin (AGP). When running a recipe on an Android project all source files are parsed as Quark's and recipes have no effect.

Describe the solution you'd like

It would be great if the plugin could support Android projects as they are a big part of the Java and Kotlin ecosystem. Many Android projects are large multi-module gradle projects and OpenRewrite would be a great tool to help with refactors and migrations.

Have you considered any alternatives or workarounds?

As far as I can tell, there isn't a workaround.

Additional context

Are you interested in contributing this feature to OpenRewrite?

I've explored adding support, but I don't have enough context on the inner workings of the plugin to be very effective. But I am willing to work with someone to help validate and test against a large Android project (700+ modules)

timtebeek commented 4 months ago

Hi @wbonnefond ; good seeing you here again & thanks for the offer to help! We should have some rudimentary support already based on this earlier discussion (https://github.com/openrewrite/rewrite-gradle-plugin/issues/290#issuecomment-1996858591) but it's clear from your post that more could be done. From these lines down there's some handling of Kotlin which might help you cross reference to what you have https://github.com/openrewrite/rewrite-gradle-plugin/blob/38e9a29b5aea93847eb118cd2f83164e53a8e125/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java#L661-L665

But perhaps it's good to have a minimized example of how you've configured your sourcesets, such that we can work out what needs to be done to support that going forward.

wbonnefond commented 4 months ago

Hey @timtebeek, so this block doesn't actually work for Android gradle modules. They are, by definition, not kotlin multiplatform modules. For android modules it would need to look for one of com.android.library, com.android.application, or com.android.test plugins, and then get the sourcesets from there. Android modules also have a notion of build variants which can have different sourcesets.

This is a very rough WIP. It's been a long time since I've written java code, so I opted to do most of it in kotlin. But it will definitely need to be reworked to reduce duplication with the existing parse code: