Knit is a purely static, compile-time safe DI framework that leverages Kotlin language features to provide zero-intermediary dependency injection with exceptional ease of use.
Knit means connecting (dependencies between codes), which is what our framework does.
@Provides
class User(val name: String) // Producer
class UserService(
@Provides val name: String // Producer which can provide `String`
) {
val user: User by di // Consumer which need a `User`
}
fun main() {
val userService = UserService("foo")
val user = userService.user // User("foo")
}There are 2 basic concepts in Knit:
@Provides, producers should be marked with@Provides, it means this member can be used for provide something.by di, consumers should be marked withby di, marked properties should be injected by dependency injection.
In the previous case:
Userhas be marked with@Provides, it meansUserprovides its constructor as a dependency provider, and this provider need aStringprovided to construct aUser.UserServicehas a constructor which need aStringand it also provides this type insideUserService.UserService.usercan be injected through providedUserconstructor and provided parametername.- For
UserServicecall-site, the only thing needs to do is construct it like a normal constructor call, and access its member directly.
Check the Advance Usages document for more, we have a separate page to show the detailed usages and some principles
Knit supports all JVM applications, including Android applications, and here is the latest Knit version ↓
-
Apply the Knit plugin in your app module.
Through Maven Central:
buildscript { repositories { mavenCentral() } dependencies { classpath("io.github.tiktok.knit:knit-plugin:$knitVersion") } } // apply it after android & kotlin plugins apply(plugin: "io.github.tiktok.knit.plugin") -
Add runtime dependencies to the module which wants to use Knit.
dependencies { implementation("io.github.tiktok.knit:knit-android:$latestKnitVersion") }
Knit has isolated with Android transform process, you can apply Knit plugin for other JVM applications.
-
Add classpath to your project's buildscript
build.gradle.buildscript { repositories { mavenCentral() } dependencies { classpath "io.github.tiktok.knit:knit-plugin:$latestKnitVersion" } } -
Apply the following plugin in your application module.
apply(plugin: "io.github.tiktok.knit.plugin")
-
For runtime dependencies, depends on
knitrather thanknit-android.dependencies { implementation("io.github.tiktok.knit:knit:$latestKnitVersion") }
After applying the Knit plugin, Knit will generate some tasks for your jar tasks which named with WithKnit suffix, for example:
jartask will generate ajarWithKnittask.shadowJartask will generate ashadowJarWithKnittask.
We recommend you to use shadowJar to ensure all dependencies are included in the jar file, otherwise you may encounter some issues when Knit can't find the dependency providers.
We have a sample project to show how to use Knit with a shadow jar application, check demo-jvm module for more details.
ByteX is a bytecode transformation framework which can make all bytecode transformation plugin shares the same transform pipeline. With ByteX, Knit can runs incrementally, usually faster than run the whole Android transform process.
-
Make sure you have well configured ByteX in your project.
-
Add classpath to your project's buildscript
build.gradle.buildscript { repositories { mavenCentral() } dependencies { classpath "io.github.tiktok.knit:knit-bytex:$latestKnitVersion" } } -
Apply the following plugin in your app module.
apply plugin: 'tiktok.knit.plugin' Knit { enable true enableInDebug true }
-
Add runtime dependencies to the module which wants to use Knit.
dependencies { implementation("tiktok.knit:knit-android:$latestKnitVersion") }
Copyright 2025 TikTok Ltd.
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.