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

Centralise the DI code generation logic #3562

Merged
merged 5 commits into from
Sep 30, 2024

Conversation

jmartinesp
Copy link
Member

@jmartinesp jmartinesp commented Sep 30, 2024

Content

Centralise the DI setup in a single setupAnvil() function.

Motivation and context

  • It's easier to maintain.
  • It should make the transition to other implementations easier.

Tests

Build the app & check it runs I guess? 😅

Tested devices

  • Physical
  • Emulator
  • OS version(s): 14

Checklist

  • Changes have been tested on an Android device or Android emulator with API 23
  • UI change has been tested on both light and dark themes
  • Accessibility has been taken into account. See https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md#accessibility
  • Pull request is based on the develop branch
  • Pull request title will be used in the release note, it clearly define what will change for the user
  • Pull request includes screenshots or videos if containing UI changes
  • Pull request includes a sign off
  • You've made a self review of your PR

Copy link
Contributor

github-actions bot commented Sep 30, 2024

📱 Scan the QR code below to install the build (arm64 only) for this PR.
QR code
If you can't scan the QR code you can install the build via this link: https://i.diawi.com/9Y8ucS

@jmartinesp jmartinesp added the PR-Build For changes related to build, tools, CI/CD label Sep 30, 2024
@jmartinesp jmartinesp marked this pull request as ready for review September 30, 2024 08:47
@jmartinesp jmartinesp requested a review from a team as a code owner September 30, 2024 08:47
@jmartinesp jmartinesp requested review from bmarty and removed request for a team September 30, 2024 08:47
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.68%. Comparing base (24c7be6) to head (4685070).
Report is 25 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #3562   +/-   ##
========================================
  Coverage    82.67%   82.68%           
========================================
  Files         1732     1732           
  Lines        40970    40970           
  Branches      4962     4962           
========================================
+ Hits         33873    33874    +1     
  Misses        5341     5341           
+ Partials      1756     1755    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bmarty bmarty added the Run-Maestro Starts a Maestro Cloud session to run integration tests label Sep 30, 2024
@github-actions github-actions bot removed the Run-Maestro Starts a Maestro Cloud session to run integration tests label Sep 30, 2024
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Thanks. Only minor remarks that you may ignore, especially because it's on part that will be updated soon.

You should maybe also update this file: https://github.com/element-hq/element-x-android/blob/develop/tools/templates/files/fileTemplates/Template%20Module%20Feature%20Build%20Gradle%20Impl.kts

anvil(projects.anvilcodegen)
implementation(libs.dagger)
kapt(libs.dagger.compiler)
setupAnvil()
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to pass generateDaggerCode = true and generateDaggerFactoriesUsingAnvil = false here (because generateDaggerFactories.set(true) was not present in the current version of the file)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I think this was a mistake and we were generating the code twice. I removed that and set all the code generation logic in app and it seems to be working fine.

Copy link
Member

Choose a reason for hiding this comment

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

OK, thanks

if (generateDaggerCode) {
applyPluginIfNeeded("org.jetbrains.kotlin.kapt")
// Needed at the top level since dagger code should be generated at a single point for performance
dependencies.add("implementation", libs.dagger)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: can maybe use the form

Suggested change
dependencies.add("implementation", libs.dagger)
dependencies.implementation(libs.dagger)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this requires us to use this kind of impot:

import gradle.kotlin.dsl.accessors._719af408c2a6c8acb7cff251b94eadcc.implementation

I think that's quite prone to breaking, a new Gradle version will probably generate a new internal id.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I did not have to add such import during my test, but maybe I missed something.

if (project.pluginManager.hasPlugin("io.element.android-compose-library")
|| project.pluginManager.hasPlugin("io.element.android-compose-application")) {
// Annotations to generate DI code for Appyx nodes
dependencies.add("implementation", project.project(":anvilannotations"))
Copy link
Member

Choose a reason for hiding this comment

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

Nit: can maybe use the form

Suggested change
dependencies.add("implementation", project.project(":anvilannotations"))
dependencies.implementation(project.project(":anvilannotations"))

) {
val libs = the<LibrariesForLibs>()
// Apply plugins and dependencies
applyPluginIfNeeded("com.squareup.anvil")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
applyPluginIfNeeded("com.squareup.anvil")
applyPluginIfNeeded(libs.plugins.anvil)

with change on applyPluginIfNeeded to

private fun Project.applyPluginIfNeeded(plugin: Provider<PluginDependency>) {
    val pluginId = plugin.get().pluginId
    if (!pluginManager.hasPlugin(pluginId)) {
        pluginManager.apply(pluginId)
    }
}

applyPluginIfNeeded("com.squareup.anvil")

if (generateDaggerCode) {
applyPluginIfNeeded("org.jetbrains.kotlin.kapt")
Copy link
Member

Choose a reason for hiding this comment

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

If you decide to do the change above in you'll need to change here too to

applyPluginIfNeeded(libs.plugins.kapt)

@jmartinesp jmartinesp added the Run-Maestro Starts a Maestro Cloud session to run integration tests label Sep 30, 2024
@jmartinesp jmartinesp enabled auto-merge (squash) September 30, 2024 11:50
@github-actions github-actions bot removed the Run-Maestro Starts a Maestro Cloud session to run integration tests label Sep 30, 2024
Copy link

sonarcloud bot commented Sep 30, 2024

@jmartinesp jmartinesp merged commit eeec7cc into develop Sep 30, 2024
30 checks passed
@jmartinesp jmartinesp deleted the misc/jme/centralise-anvil-setup branch September 30, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR-Build For changes related to build, tools, CI/CD
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants