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 code), 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 to provide something.by di
, consumers should be marked withby di
, marked properties should be injected by dependency injection.
In the previous case:
User
has been marked with@Provides
, it meansUser
provides its constructor as a dependency provider, and this provider needs aString
provided to construct aUser
.UserService
has a constructor which needs aString
and it also provides this type insideUserService
.UserService.user
can be injected through providedUser
constructor and provided parametername
.- For
UserService
call-site, the only thing needs to do is construct it like a normal constructor call, and access its member directly.
Check the Advance Usage document for more, we have a separate page to show the detailed usage 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 gradle plugin portal:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' // apply it after android & kotlin plugins id 'io.github.tiktok.knit.plugin' version "${latestKnitVersion}" }
Through maven central:
buildscript { repositories { mavenCentral() } dependencies { classpath("io.github.tiktok.knit:knit-plugin:$latestKnitVersion") } } // 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 is isolated from the 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
knit
rather 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:
jar
task will generate ajarWithKnit
task.shadowJar
task will generate ashadowJarWithKnit
task.
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.