Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.
/ archknife Public archive

Android annotation library to generate the project's activity/fragment/viewmodel dagger structure.

License

Notifications You must be signed in to change notification settings

AlexanderEggers/archknife

Repository files navigation

Archknife

Download

Archknife is a android annotation library to generate the project's Dagger-2 structure. Dagger usually needs a certain amount of extra work in order to create and keep a structure. This library helps you to focus on the what really matters: Your App!

By using this library you need to use only a handful of annotation you can assign to your activities, fragments, viewmodels, services and broadcast receivers. Nothing more. Due to these annotations all of those classes will be added to the AndroidInjector (Dagger-2 class).

Download

repositories {
  jcenter()
}

dependencies {
  def archknife_version = "1.1.0"

  //includes all library artifacts including several helper classes
  implementation "org.archknife:archknife-core:$archknife_version"
  //just annotations
  implementation "org.archknife:archknife-annotation:$archknife_version"
  //just context helper classes
  implementation "org.archknife:archknife-context:$archknife_version"
  
  kapt "org.archknife:archknife-processor:$archknife_version"
}

How do I use Archknife? (Step-by-step introduction for 1.1.0)

Archknife has five different annotation types.

  1. @ProvideApplication

Like "normal" Dagger-2 implementations, this one also requires you to create an application class. This class needs to be declared inside your manifest in order to work. This library provides you with a base application class that includes most of the required Dagger-2 implementation. Simply extend your class with the ArchknifeApplication. In case you want to add your custom AppInjector class to the default dagger initialisation, you can use the ArchknifeApplicationGen class which requires an AppInjector class as a generic type. The AppInjector is responsible in handling the application (activity/fragment) and dagger lifecycle.

The annotation @ProvideApplication also has an optional (array-based) parameter "externalModuleClasses". These module classes are outside of the classpath for the current project. Due to the processor restrictions, archknife won't be able to pick up any 3rd party module classes. This can be avoided by adding these 3rd party module to this array. Ensure that the modules are annotated with @Module, otherwise the dagger-v2 processor will fail.

@ProvideApplication(externalModuleClasses = [HelperModule::class])
class DemoApp : ArchknifeApplication()
  1. @ProvideActivity

This annotation can be used to attach a certain activity class to Dagger-2. By doing so, you don't need to add this class to any dagger-related class. Addition to that you have two different interfaces. You need to use at least one of those: Injectable or HasAndroidInjector. Injectable should be used if this activity won't have any fragments attached to it and HasAndroidInjector is required if you want (Dagger-2) fragments to be displayed within this activity. Those interfaces are also used to differ between Dagger-2 (using one of the interfaces) and non-Dagger-2 activities within the AppInjector (application lifecycle handler for Dagger-2).

@ProvideActivity
class DemoActivity: AppCompatActivity(), HasAndroidInjector
  1. @ProvideFragment

This annotation is responsible in attaching fragments to Dagger-2. Each @ProvideFragment needs at least one activity class that this fragment is working with. You can simply add the activity within the annotation. Addition to the annotation, your fragment needs to implement the Injectable interface. This is used to differ between Dagger-2 and non-Dagger-2 fragments within the AppInjector (application lifecycle handler for Dagger-2).

@ProvideFragment([DemoActivity::class])
class DemoFragment: Fragment(), Injectable
  1. @ProvideViewModel

This annotation takes not only care to provide the viewmodel class to Dagger-2 but also to provide it to the ViewModelProvider.Factory that is needed to correctly initialise viewmodels inside your app.

@ProvideViewModel
class DemoViewModel: ViewModel()

Here's an example in how to initialise the ViewModel later in your app:

@ProvideActivity
class DemoActivity: AppCompatActivity(), HasSupportFragmentInjector {

  @Inject
  lateinit var factory: ViewModelProvider.Factory
  
  private lateinit var viewModel: DemoViewModel

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(DemoViewModel::class.java)
  }
  
  ...
}
  1. @ProvideModule

From time to time you probably will need a custom module to define certain dependencies. You can read more about @Modules at the Dagger webpage. Therefore this class also needs to use the @Module annotation which is provided by Dagger-2.

@ProvideModule
@Module
class DemoModule
  1. @ProvideService and @ProvideBroadcastReceiver

If you want to add BroadcastReceiver or Services to your app, you can also use ArchKnife to annotate these classes. Keep in mind that both classes needs to execute the AndroidInjection.inject(...). The Service requires this method class in it's onCreate, the BroadcastReceiver in it's onReceive.

Status

Version 1.2.0 is currently under development in the develop branch. The latest stable version is 1.1.0 which can be found in the stable/1.1.x branch.

Comments/bugs/questions/pull requests are always welcome!

Compatibility

  • The library requires at minimum Android 16.
  • This library supports dagger-v2 using 2.27 and higher (version higher than 2.27 are depending on possible breaking changes).

Author

Alexander Eggers - @mordag on GitHub

License

Apache 2.0. See the LICENSE file for details.