Convert Figma logo to code with AI

square logoanvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.

1,311
81
1,311
54

Top Related Projects

17,430

A fast dependency injector for Android and Java.

8,925

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.

7,751

Uber's cross-platform mobile architecture framework.

Mavericks: Android on Autopilot

Quick Overview

Anvil is a Ruby gem that provides a simple way to create and manage database migrations for Ruby applications. It offers a lightweight, flexible approach to schema management, allowing developers to version control their database structure and easily apply changes across different environments.

Pros

  • Simple and intuitive API for creating and managing migrations
  • Supports multiple database adapters (PostgreSQL, MySQL, SQLite)
  • Integrates well with existing Ruby projects and frameworks
  • Provides a command-line interface for easy migration management

Cons

  • Less feature-rich compared to some more established migration tools
  • Limited documentation and community support
  • May require additional setup for complex database scenarios
  • Not as widely adopted as alternatives like ActiveRecord migrations

Code Examples

Creating a new migration:

Anvil.create_migration "CreateUsersTable"

Defining a migration:

Anvil.migrate :create_users_table do
  up do
    create_table :users do |t|
      t.string :name
      t.string :email
      t.timestamps
    end
  end

  down do
    drop_table :users
  end
end

Running migrations:

Anvil.migrate!

Getting Started

  1. Install the gem:

    gem install anvil
    
  2. Initialize Anvil in your project:

    require 'anvil'
    
    Anvil.configure do |config|
      config.migrations_path = './db/migrations'
      config.database_url = 'postgres://localhost/myapp_development'
    end
    
  3. Create and run migrations:

    Anvil.create_migration "AddEmailToUsers"
    
    # Edit the migration file in ./db/migrations
    
    Anvil.migrate!
    

Competitor Comparisons

17,430

A fast dependency injector for Android and Java.

Pros of Dagger

  • More mature and widely adopted in the Android development community
  • Supports a broader range of injection scenarios, including field injection
  • Offers runtime dependency injection, which can be beneficial for certain use cases

Cons of Dagger

  • Steeper learning curve due to more complex API and concepts
  • Slower compile times, especially for large projects
  • Requires more boilerplate code compared to Anvil

Code Comparison

Dagger:

@Module
public class AppModule {
    @Provides
    @Singleton
    public NetworkApi provideNetworkApi() {
        return new NetworkApiImpl();
    }
}

Anvil:

@ContributesTo(AppScope::class)
object AppModule {
    @Provides
    @Singleton
    fun provideNetworkApi(): NetworkApi = NetworkApiImpl()
}

Summary

Dagger is a more established and feature-rich dependency injection framework, offering runtime injection and broader support for various injection scenarios. However, it comes with a steeper learning curve and slower compile times. Anvil, on the other hand, focuses on compile-time dependency injection, resulting in faster compile times and less boilerplate code. It's designed specifically for Kotlin and integrates well with other Square libraries. The choice between the two depends on project requirements, team expertise, and performance considerations.

8,925

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform

Pros of Koin

  • Lightweight and easy to set up, with minimal boilerplate code
  • Supports multiplatform projects (Android, iOS, JVM)
  • Provides a DSL for dependency declaration, making it more readable

Cons of Koin

  • Runtime dependency injection, which can lead to runtime errors
  • Less compile-time safety compared to Anvil
  • May have a slight performance overhead due to runtime resolution

Code Comparison

Koin:

val myModule = module {
    single { MyRepository() }
    factory { MyViewModel(get()) }
}

Anvil:

@Module
@ContributesTo(AppScope::class)
object MyModule {
    @Provides
    fun provideRepository(): MyRepository = MyRepository()
}

Koin uses a more concise DSL for dependency declaration, while Anvil relies on annotations and follows a more traditional Dagger-like approach. Koin's syntax is generally more readable, but Anvil provides stronger compile-time guarantees.

Both libraries aim to simplify dependency injection in Kotlin projects, but they take different approaches. Koin focuses on simplicity and ease of use, while Anvil emphasizes compile-time safety and code generation. The choice between them depends on project requirements and developer preferences.

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.

Pros of Toothpick

  • Simpler API and easier to learn for beginners
  • Supports runtime dependency injection, allowing for more flexibility
  • Smaller library size, potentially reducing app size

Cons of Toothpick

  • Less compile-time safety compared to Anvil's approach
  • May have slightly slower performance due to runtime reflection
  • Less extensive documentation and community support

Code Comparison

Toothpick:

@Inject lateinit var coffee: Coffee

class CoffeeMaker @Inject constructor(
    private val heater: Heater,
    private val pump: Pump
)

Anvil:

@Inject lateinit var coffee: Coffee

class CoffeeMaker @ContributesBinding constructor(
    private val heater: Heater,
    private val pump: Pump
): CoffeeMaker

Both Toothpick and Anvil are dependency injection frameworks for Android and Java applications. Toothpick offers a simpler API and runtime flexibility, making it easier for beginners to adopt. However, Anvil provides stronger compile-time safety and potentially better performance due to its compile-time code generation approach. Anvil also benefits from Square's reputation and larger community support. The code examples show similar syntax for basic dependency injection, with Anvil requiring an additional annotation for binding contributions.

7,751

Uber's cross-platform mobile architecture framework.

Pros of RIBs

  • Designed for large-scale mobile app development with complex navigation
  • Promotes better separation of business logic from UI components
  • Provides a more structured approach to state management and data flow

Cons of RIBs

  • Steeper learning curve due to its complex architecture
  • May be overkill for smaller projects or simpler applications
  • Less flexibility in terms of UI framework integration

Code Comparison

RIBs (Kotlin):

class ExampleRouter(
    interactor: Interactor<ExampleInteractor.ExamplePresenter, ExampleRouting>,
    component: ExampleBuilder.Component
) : ViewRouter<ExampleView, ExampleInteractor, ExampleBuilder.Component>(interactor, component)

Anvil (Java):

@ContributesTo(AppScope.class)
@Module
public class ExampleModule {
    @Provides
    static ExampleService provideExampleService() {
        return new ExampleServiceImpl();
    }
}

Key Differences

  • RIBs focuses on a tree-structured architecture for complex navigation, while Anvil is primarily a dependency injection framework
  • RIBs is more opinionated about overall app structure, whereas Anvil provides more flexibility in architectural choices
  • Anvil is generally easier to integrate into existing projects, while RIBs often requires a more significant architectural shift

Mavericks: Android on Autopilot

Pros of Mavericks

  • More active development with recent updates and contributions
  • Broader scope for general iOS app architecture and development
  • Larger community and more widespread adoption in the iOS ecosystem

Cons of Mavericks

  • Potentially steeper learning curve due to its comprehensive nature
  • May introduce more complexity for simpler projects
  • Less focused on specific UI components compared to Anvil

Code Comparison

Mavericks (Swift):

class ViewModel: MavericksViewModel<MyState> {
    fun incrementCounter() = setState { copy(counter = counter + 1) }
}

Anvil (Kotlin):

class MyViewModel @AssistedInject constructor(
    @Assisted state: MyState
) : RealMvRxViewModel<MyState>(state) {
    fun incrementCounter() = setState { copy(counter = counter + 1) }
}

Both frameworks use similar patterns for state management and view models, but Mavericks is designed for iOS (Swift) while Anvil is for Android (Kotlin). Mavericks offers a more streamlined syntax, while Anvil integrates with Dagger for dependency injection.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Anvil

Maven Central CI

"When all you have is an anvil, every problem looks like a hammer." - Abraham Maslow

Anvil is a Kotlin compiler plugin to make dependency injection with Dagger easier by automatically merging Dagger modules and component interfaces. In a nutshell, instead of manually adding modules to a Dagger component and making the Dagger component extend all component interfaces, these modules and interfaces can be included in a component automatically:

@Module
@ContributesTo(AppScope::class)
class DaggerModule { .. }

@ContributesTo(AppScope::class)
interface ComponentInterface {
  fun getSomething(): Something
  fun injectActivity(activity: MyActivity)
}

// The real Dagger component.
@MergeComponent(AppScope::class)
interface AppComponent

The generated AppComponent interface that Dagger sees looks like this:

@Component(modules = [DaggerModule::class])
interface AppComponent : ComponentInterface

Notice that AppComponent automatically includes DaggerModule and extends ComponentInterface.

Setup

The plugin consists of a Gradle plugin and Kotlin compiler plugin. The Gradle plugin automatically adds the Kotlin compiler plugin and annotation dependencies. It needs to be applied in all modules that either contribute classes to the dependency graph or merge them:

plugins {
  id 'com.squareup.anvil' version "${latest_version}"
}

Or you can use the old way to apply a plugin:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "com.squareup.anvil:gradle-plugin:${latest_version}"
  }
}

apply plugin: 'com.squareup.anvil'

Quick Start

There are three important annotations to work with Anvil.

@ContributesTo can be added to Dagger modules and component interfaces that should be included in the Dagger component. Classes with this annotation are automatically merged by the compiler plugin as long as they are on the compile classpath.

@MergeComponent is used instead of the Dagger annotation @Component. Anvil will generate the Dagger annotation and automatically include all modules and component interfaces that were contributed the same scope.

@MergeSubcomponent is similar to @MergeComponent and should be used for subcomponents instead.

Scopes

Scope classes are only markers. The class AppScope from the sample could look like this:

abstract class AppScope private constructor()

These scope classes help Anvil make a connection between the Dagger component and which Dagger modules and other component interfaces to include.

Scope classes are independent of the Dagger scopes. It's still necessary to set a scope for the Dagger component, e.g.

@Singleton
@MergeComponent(AppScope::class)
interface AppComponent

Contributed bindings

The @ContributesBinding annotation generates a Dagger binding method for an annotated class and contributes this binding method to the given scope. Imagine this example:

interface Authenticator

class RealAuthenticator @Inject constructor() : Authenticator

@Module
@ContributesTo(AppScope::class)
abstract class AuthenticatorModule {
  @Binds abstract fun bindRealAuthenticator(authenticator: RealAuthenticator): Authenticator
}

This is a lot of boilerplate if you always want to use RealAuthenticator when injecting Authenticator. You can replace this entire Dagger module with the @ContributesBinding annotation. The equivalent would be:

interface Authenticator

@ContributesBinding(AppScope::class)
class RealAuthenticator @Inject constructor() : Authenticator

@ContributesBinding also supports qualifiers. You can annotate the class with any qualifier and the generated binding method will preserve the qualifier, e.g.

@ContributesBinding(AppScope::class)
@Named("Prod")
class RealAuthenticator @Inject constructor() : Authenticator

// Will generate:
@Binds @Named("Prod") 
abstract fun bindRealAuthenticator(authenticator: RealAuthenticator): Authenticator

Contributed multibindings

Similar to contributed bindings, @ContributesMultibinding will generate a multibindings method for (all/an) annotated class(es). Qualifiers are supported the same way as normal bindings.

@ContributesMultibinding(AppScope::class)
@Named("Prod")
class MainListener @Inject constructor() : Listener

// Will generate this binding method.
@Binds @IntoSet @Named("Prod")
abstract fun bindMainListener(listener: MainListener): Listener

If the class is annotated with a map key annotation, then Anvil will generate a maps multibindings method instead of adding the element to a set:

@MapKey
annotation class BindingKey(val value: String)

@ContributesMultibinding(AppScope::class)
@BindingKey("abc")
class MainListener @Inject constructor() : Listener

// Will generate this binding method.
@Binds @IntoMap @BindingKey("abc")
abstract fun bindMainListener(listener: MainListener): Listener

Exclusions

Dagger modules and component interfaces can be excluded in two different levels.

One class can always replace another one. This is especially helpful for modules that provide different bindings for instrumentation tests, e.g.

@Module
@ContributesTo(
    scope = AppScope::class,
    replaces = [DevelopmentApplicationModule::class]
)
object DevelopmentApplicationTestModule {
  @Provides
  fun provideEndpointSelector(): EndpointSelector = TestingEndpointSelector
}

The compiler plugin will find both classes on the classpath. Adding both modules DevelopmentApplicationModule and DevelopmentApplicationTestModule to the Dagger graph would lead to duplicate bindings. Anvil sees that the test module wants to replace the other and ignores it. This replacement rule has a global effect for all applications which are including the classes on the classpath.

Applications can exclude Dagger modules and component interfaces individually without affecting other applications.

@MergeComponent(
  scope = AppScope::class,
  exclude = [
    DaggerModule::class
  ]
)
interface AppComponent

In a perfect build graph it’s unlikely that this feature is needed. However, due to legacy modules, wrong imports and deeply nested dependency chains applications might need to make use of it. The exclusion rule does what it implies. In this specific example DaggerModule wishes to be contributed to this scope, but it has been excluded for this component and thus is not added.

Dagger Factory Generation

Anvil allows you to generate Factory classes that usually the Dagger annotation processor would generate for @Provides methods, @Inject constructors and @Inject fields. The benefit of this feature is that you don't need to enable the Dagger annotation processor in this module. That often means you can skip KAPT and the stub generating task. In addition Anvil generates Kotlin instead of Java code, which allows Gradle to skip the Java compilation task. The result is faster builds.

Gradle DSL
// build.gradle
anvil {
  generateDaggerFactories = true // default is false
}
Gradle Properties
# gradle.properties
com.squareup.anvil.generateDaggerFactories=true # default is false

In our codebase we measured that modules using Dagger build 65% faster with this new Anvil feature compared to using the Dagger annotation processor:

Stub generationKaptJavacKotlincSum
Dagger12.97640.3778.57110.24172.165
Anvil006.96517.74824.713

For full builds of applications we measured savings of 16% on average.

Benchmark Dagger Factories

This feature can only be enabled in Gradle modules that don't compile any Dagger component. Since Anvil only processes Kotlin code, you shouldn't enable it in modules with mixed Kotlin / Java sources either.

When you enable this feature, don't forget to remove the Dagger annotation processor. You should keep all other dependencies.

Extending Anvil

Every codebase has its own dependency injection patterns where certain code structures need to be repeated over and over again. Here Anvil comes to the rescue and you can extend the compiler plugin with your own CodeGenerator. For usage please take a look at the compiler-api artifact

Advantages of Anvil

Adding Dagger modules to components in a large modularized codebase with many application targets is overhead. You need to know where components are defined when creating a new Dagger module and which modules to add when setting up a new application. This task involves many syncs in the IDE after adding new module dependencies in the build graph. The process is tedious and cumbersome. With Anvil you only add a dependency in your build graph and then you can immediately test the build.

Aligning the build graph and Dagger's dependency graph brings a lot of consistency. If code is on the compile classpath, then it's also included in the Dagger dependency graph.

Modules implicitly have a scope, if provided objects are tied to a scope. Now the scope of a module is clear without looking at any binding.

With Anvil you don't need any composite Dagger module anymore, which only purpose is to combine multiple modules to avoid repeating the setup for multiple applications. Composite modules easily become hairballs. If one application wants to exclude a module, then it has to repeat the setup. These forked graphs are painful and confusing. With Dagger you want to make the decision which modules fulfill dependencies as late as possible, ideally in the application module. Anvil makes this approach a lot easier by generating the code for included modules. Composite modules are redundant. You make the decision which bindings to use by importing the desired module in the application module.

Performance

Anvil is a convenience tool. Similar to Dagger it doesn't improve build speed compared to writing all code manually before running a build. The savings are in developer time.

The median overhead of Anvil is around 4%, which often means only a few hundred milliseconds on top. The overhead is marginal, because Kotlin code is still compiled incrementally and Kotlin compile tasks are skipped entirely, if nothing has changed. This doesn't change with Anvil.

Benchmark

On top of that, Anvil provides actual build time improvements by replacing the Dagger annotation processor in many modules if you enable Dagger Factory generation.

Kotlin compiler plugin

We investigated whether other alternatives like a bytecode transformer and an annotation processor would be a better option, but ultimately decided against them. For what we tried to achieve a bytecode transformer runs too late in the build process; after the Dagger components have been generated. An annotation processor especially when using KAPT would be too slow. Even though the Kotlin compiler plugin API isn't stable and contains bugs we decided to write a compiler plugin.

Limitations

No Java support

Anvil is a Kotlin compiler plugin, thus Java isn’t supported. You can use Anvil in modules with mixed Java and Kotlin code for Kotlin classes, though.

Correct error types disabled

KAPT has the option to correct non-existent types. This option however changes order of how compiler plugins and KAPT itself are invoked. The result is that Anvil cannot merge supertypes before the Dagger annotation processor runs and abstract functions won't be implemented properly in the final Dagger component.

Anvil will automatically set correctErrorTypes to false to avoid this issue.

Incremental Kotlin compilation breaks Anvil's feature to merge contributions

[!TIP] Anvil now experimentally supports incremental compilation and Gradle's build caching, as of v2.5.0.

This feature is disabled by default. It can be enabled via a Gradle property or the Gradle DSL:

Gradle Properties
# gradle.properties
com.squareup.anvil.trackSourceFiles=true # default is false
Gradle DSL
// build.gradle
anvil {
  trackSourceFiles = true // default is false
}

Anvil merges Dagger component interfaces and Dagger modules during the stub generating task when @MergeComponent is used. This requires scanning the compile classpath for any contributions. Assume the scenario that a contributed type in a module dependency has changed, but the module using @MergeComponent itself didn't change. With Kotlin incremental compilation enabled the compiler will notice that the module using @MergeComponent doesn't need to be recompiled and therefore doesn't invoke compiler plugins. Anvil will miss the new contributed type from the module dependency.

To avoid this issue, Anvil must disable incremental compilation for the stub generating task, which runs right before Dagger processes annotations. Normal Kotlin compilation isn't impacted by this workaround. The issue is captured in KT-54850 Provide mechanism for compiler plugins to add custom information into binaries.

Disabling incremental compilation for the stub generating task could have a negative impact on compile times, if you heavily rely on KAPT. While Anvil can significantly help to improve build times, the wrong configuration and using KAPT in most modules could make things worse. The suggestion is to extract and isolate annotation processors in separate modules and avoid using Anvil in the same modules, e.g. a common practice is to move the Dagger component using @MergeComponent into the final application module with little to no other code in the app module.

Hilt

Hilt is Google's opinionated guide how to dependency injection on Android. It provides a similar feature with @InstallIn for entry points and modules as Anvil. If you use Hilt, then you don't need to use Anvil.

Hilt includes many other features and comes with some restrictions. For us it was infeasible to migrate a codebase to Hilt with thousands of modules and many Dagger components while we only needed the feature to merge modules and component interfaces automatically. We also restrict the usage of the Dagger annotation processor to only specific modules for performance reasons. With Hilt we wouldn't be able to enforce this requirement anymore for component interfaces. The development of Anvil started long before Hilt was announced and the internal version is being used in production for a while.

License

Copyright 2020 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.