openrewrite / rewrite-gradle-plugin

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

Rewrite not working with Kotlin Android application #290

Open Garf1eldGit opened 5 months ago

Garf1eldGit commented 5 months ago

Hi! I added a plugin to a project with 1 module, in the root build.gradle I installed the plugin version and the library version for Kotlin. After synchronizing the project, I ran the command ./gradlew rewriteRun, but nothing changed. What am I doing wrong?

Command output

./gradlew rewriteRun

> Configure project :
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.8.20` that might work differently than in the requested version `1.9.0`.

> Task :rewriteRun
Validating active recipes
Scanning sources in project :app
Using active styles []
Scanning sources in project :
Using active styles []
All sources parsed, running active recipes: com.yourorg.RenameTypeAliasExample, org.openrewrite.kotlin.format.AutoFormat

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.2/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 5s

rewrite.yml

---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.RenameTypeAliasExample
displayName: Rename type alias example
recipeList:
  - org.openrewrite.kotlin.RenameTypeAlias:
      aliasName: OldAlias
      newName: NewAlias
      fullyQualifiedAliasedType: org.junit.Assume

Source code that needs to change (typealias OldAlias)

package com.example.dagger_hilt

import android.app.Application
import com.github.terrakok.cicerone.Cicerone
import dagger.hilt.android.HiltAndroidApp

typealias OldAlias = Cicerone.Companion

@HiltAndroidApp
class App : Application() {

    private val cicerone = OldAlias.create()
    val router get() = cicerone.router
    val navigatorHolder get() = cicerone.getNavigatorHolder()

    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
    }

    companion object {
        internal lateinit var INSTANCE: App
            private set
    }
}

plugin "org.openrewrite.rewrite" version "6.8.0" "org.openrewrite:rewrite-kotlin:1.11.0"

timtebeek commented 5 months ago

Hi @Garf1eldGit ; Thanks for chiming in here! Could you share more about how your project is structured, and what plugins and configuration specifically make it a Kotlin (Android) app?

I'm thinking the problem lies in the rewrite-gradle-plugin failing to pick up your Kotlin sources; we have some logic towards that here and further down. https://github.com/openrewrite/rewrite-gradle-plugin/blob/106441ad01dd41424472e01d153ea773a5ea8215/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java#L651-L654

Hopefully from any config you share we can then work out what we need to do to parse your project properly, such that you can run recipes as well.

Garf1eldGit commented 5 months ago

I'm trying to integrate a plugin into an Android project, I use gradle and kts as the build system

image

UPD: I checked using a debugger, the source sets are really empty

image