Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Filer writing and originating elements concept. #93

Merged
merged 1 commit into from
May 25, 2017

Conversation

JakeWharton
Copy link
Member

These stem from javac annotation processors inside which Kotlin cannot be generated.

These stem from javac annotation processors inside which Kotlin cannot be generated.
Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all red diffs are the best diffs

@swankjesse swankjesse merged commit d24e4af into master May 25, 2017
@max4t
Copy link

max4t commented Jun 2, 2017

Hi, if you don't mind me asking : why removing the writeTo(Filer)?
I've created an annotation processor for android which generates kotlin files to Filer.createResource(SOURCE_OUTPUT, ...) and it works like a charm.
I was hoping to use writeTo(Filer) when switching to kotlinPoet

@JakeWharton
Copy link
Member Author

JakeWharton commented Jun 2, 2017 via email

@max4t
Copy link

max4t commented Jun 2, 2017

javac can't for sure but i'm using android studio 3 (canary) and the default kotlin buildchain compile the generated kotlin files in Filer.createResource(SOURCE_OUTPUT, ...)

@JakeWharton
Copy link
Member Author

JakeWharton commented Jun 2, 2017 via email

@max4t
Copy link

max4t commented Jun 2, 2017

Here is a sample :

  • In app/build/generated/source/kapt/debug/com/example/extension/MakerExtension.kt (generated):
...
fun Maker.getDummy() = "Test String"
...
  • In app/src/main/java/com/example/MainActivity.kt:
@MakerAnnotation
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        (findViewById(R.id.txt_view) as TextView).text = Maker().getDummy()
    }
}

P.S.: the MakerAnnotation is what generates MakerExtension.kt
P.S.2: in the processor, i have :

private fun getWriter() : Writer = processingEnv.filer.createResource(
            StandardLocation.SOURCE_OUTPUT, "com.example.extension", "MakerExtension.kt").openWriter()

@JakeWharton
Copy link
Member Author

JakeWharton commented Jun 2, 2017 via email

@max4t
Copy link

max4t commented Jun 2, 2017

Here is the sample
The class MainActivity set the text of the textView using the generated function

@JakeWharton
Copy link
Member Author

JakeWharton commented Jun 2, 2017 via email

@JakeWharton JakeWharton deleted the jw/2017-05-25/delete-filer branch June 2, 2017 18:49
@max4t
Copy link

max4t commented Jun 2, 2017

Thanks that would be great.

It works because kapt.generateStubs = true in app/build.gradle otherwise you get an missing reference.
I don't remember exactly how it works but if i recall correctly kapt generated the references (the stubs) without content so that javac doesn't complain and then all is linked at the end or something like that

@JakeWharton
Copy link
Member Author

JakeWharton commented Jun 2, 2017 via email

@max4t
Copy link

max4t commented Jun 2, 2017

It seems like by default kapt1 is used. kapt3 is available by adding apply plugin: 'kotlin-kapt'

With apply plugin: 'kotlin-kapt', this block (in app/build.gradle):

kapt {
    generateStubs = true
}

is useless (as announced in the blog paragraph New Experimental Annotation Processing)

@hotchemi
Copy link

Hi @JakeWharton @max4t any update here?
I just found official Kotlin code generation example and it seems works well with the latest Kotlin and kapt ver. https://github.com/JetBrains/kotlin-examples/tree/master/gradle/kotlin-code-generation

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.

None yet

5 participants