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

Add activity inject module #6

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.squareup.kotlinpoet.ClassName
import org.jetbrains.kotlin.name.FqName

internal object PixnewsActivityClassName {
private const val ACTIVITY_PACKAGE = "ru.pixnews.foundation.di.ui.base.activity"
internal const val ACTIVITY_PACKAGE = "ru.pixnews.anvil.codegen.activity.inject.wiring"
internal val activityMapKey = ClassName(ACTIVITY_PACKAGE, "ActivityMapKey")
internal val activityScope = ClassName(ACTIVITY_PACKAGE, "ActivityScope")
internal val contributesActivity = FqName("ru.pixnews.foundation.di.ui.base.activity.ContributesActivity")
internal val contributesActivity = FqName("ru.pixnews.anvil.codegen.activity.inject.ContributesActivity")
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ class ContributesActivityCodeGeneratorTest {
@BeforeAll
@Suppress("LOCAL_VARIABLE_EARLY_DECLARATION")
fun setup() {
val activityDiStubs = """
package ru.pixnews.foundation.di.ui.base.activity
val wiringActivityDiStubs = """
package ${PixnewsActivityClassName.ACTIVITY_PACKAGE}
import android.app.Activity
import dagger.MapKey
import kotlin.reflect.KClass

public abstract class ActivityScope private constructor()
public annotation class ActivityMapKey(val activityClass: KClass<out Activity>)
""".trimIndent()

val contributeActivityAnnotationStub = """
package ${PixnewsActivityClassName.contributesActivity.parent().asString()}
public annotation class ContributesActivity
""".trimIndent()

Expand All @@ -58,15 +62,16 @@ class ContributesActivityCodeGeneratorTest {
package com.test

import android.app.Activity
import ru.pixnews.foundation.di.ui.base.activity.ContributesActivity
import ${PixnewsActivityClassName.contributesActivity.asString()}

@ContributesActivity
class TestActivity : Activity()
""".trimIndent()

compilationResult = compileAnvil(
sources = arrayOf(
activityDiStubs,
wiringActivityDiStubs,
contributeActivityAnnotationStub,
androidActivityStub,
testActivity,
),
Expand Down
11 changes: 9 additions & 2 deletions activity/inject/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

plugins {
id("ru.pixnews.anvil.codegen.build-logic.project.kotlin.library")
id("ru.pixnews.anvil.codegen.build-logic.project.test")
id("ru.pixnews.anvil.codegen.build-logic.project.android.library")
id("ru.pixnews.anvil.codegen.build-logic.project.publish")
}

Expand All @@ -16,5 +15,13 @@ version = anvilCodegenVersions.getSubmoduleVersionProvider(
envVariableName = "ANVIL_CODEGEN_ACTIVITY_INJECT_VERSION",
).get()

android {
namespace = "ru.pixnews.anvil.codegen.activity"
}

dependencies {
api(libs.anvil.annotations)
api(libs.anvil.annotations.optional)
api(libs.dagger)
api(libs.inject)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.activity.inject

/**
* Annotate a Activity class with this to automatically contribute it to the ActivityScope multibinding.
* Equivalent to the following declaration in an application module:
*```
* @Module
* @ContributesTo(ActivityScope::class)
* abstract class MainActivityModule {
* @Binds
* @IntoMap
* @ActivityMapKey(MainActivity::class)
* @SingleIn(ActivityScope::class)
* abstract fun bindsMainInjector(target: MembersInjector<MainActivity>): MembersInjector<out Activity>
* }
*```
* The generated code created via the anvil-codegen module.
*/
public annotation class ContributesActivity
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.activity.inject.wiring

import android.app.Activity

public fun interface ActivityInjector {
public fun inject(activity: Activity)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.activity.inject.wiring

import android.app.Activity
import dagger.MapKey
import kotlin.reflect.KClass

/**
* A Dagger multi-binding key used for registering a [Activity] into the top level dagger graphs.
*/
@MapKey
public annotation class ActivityMapKey(val activityClass: KClass<out Activity>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.activity.inject.wiring

public abstract class ActivityScope private constructor()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.activity.inject.wiring

import android.app.Activity
import dagger.MembersInjector

public class DefaultActivityInjector(
private val providers: Map<Class<out Activity>, MembersInjector<out Activity>>,
) : ActivityInjector {
@Suppress("UNCHECKED_CAST")
override fun inject(activity: Activity) {
providers[activity::class.java]?.also {
(it as MembersInjector<Activity>).injectMembers(activity)
} ?: error("No member injector for $activity")
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

plugins {
alias(libs.plugins.android.library) apply false
alias(libs.plugins.gradle.maven.publish.plugin.base) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.kapt) apply false
Expand Down
9 changes: 9 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ org.gradle.parallel=false
org.gradle.caching=true
org.gradle.daemon.performance.enable-monitoring=false

# Kotlin
kotlin.code.style=official

# Kapt
kapt.include.compile.classpath=false

#Android
android.useAndroidX=true
android.enableJetifier=false
android.defaults.buildfeatures.resvalues=false
android.defaults.buildfeatures.shaders=false
android.library.defaults.buildfeatures.androidresources=false

systemProp.org.gradle.s3.endpoint=https://storage.yandexcloud.net
4 changes: 3 additions & 1 deletion gradle/build-logic/project/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ plugins {
group = "ru.pixnews.anvil.codegen.build-logic.project"

dependencies {
implementation(libs.kotlin.plugin)
implementation(libs.agp.plugin.api)
runtimeOnly(libs.agp.plugin)
implementation(libs.gradle.maven.publish.plugin)
implementation(libs.kotlin.plugin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

import com.android.build.api.dsl.LibraryExtension
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9

plugins {
id("com.android.library")
kotlin("android")
}

extensions.configure<LibraryExtension>("android") {
compileSdk = versionCatalogs.named("libs").findVersion("androidCompileSdk").get().displayName.toInt()
defaultConfig {
minSdk = versionCatalogs.named("libs").findVersion("androidMinSdk").get().displayName.toInt()
}

compileOptions {
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = false
}

kotlin {
explicitApi = ExplicitApiMode.Warning
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
languageVersion = KOTLIN_1_9
apiVersion = KOTLIN_1_9
freeCompilerArgs.addAll("-Xjvm-default=all")
}
target {
publishLibraryVariants = listOf("release")
}
}

buildTypes {
getByName("release") {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.vanniktech.maven.publish.AndroidSingleVariantLibrary
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import ru.pixnews.anvil.codegen.buildlogic.project.publish.createAnvilCodegenVersionsExtension
Expand All @@ -12,7 +13,6 @@ import ru.pixnews.anvil.codegen.buildlogic.project.publish.createAnvilCodegenVer
* Convention plugin with publishing defaults
*/
plugins {
kotlin("jvm") apply false
id("com.vanniktech.maven.publish.base")
}

Expand Down Expand Up @@ -40,12 +40,24 @@ mavenPublishing {
}
}
}
configure(
KotlinJvm(
javadocJar = JavadocJar.None(),
sourcesJar = true,
),
)

plugins.withId("org.jetbrains.kotlin.jvm") {
configure(
KotlinJvm(
javadocJar = JavadocJar.None(),
sourcesJar = true,
),
)
}
plugins.withId("com.android.library") {
configure(
AndroidSingleVariantLibrary(
variant = "release",
sourcesJar = true,
publishJavadocJar = false,
),
)
}

signAllPublications()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
kotlin("jvm") apply false
kotlin("jvm")
}

val libs = versionCatalogs.named("libs")
Expand Down
9 changes: 9 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[versions]
androidMinSdk = "14"
androidCompileSdk = "33"

kotlin = "1.9.22"

agp = "8.2.1"
anvil = "2.4.8"
auto-service = "1.1.1"
assertk = "0.28.0"
Expand All @@ -16,13 +20,15 @@ spotless = "6.23.3"

[libraries]
anvil-annotations-optional = { group = "com.squareup.anvil", name = "annotations-optional", version.ref = "anvil" }
anvil-annotations = { group = "com.squareup.anvil", name = "annotations", version.ref = "anvil" }
anvil-compiler-api = { group = "com.squareup.anvil", name = "compiler-api", version.ref = "anvil" }
anvil-compiler-utils = { group = "com.squareup.anvil", name = "compiler-utils", version.ref = "anvil" }
auto-service-annotations = { group = "com.google.auto.service", name = "auto-service-annotations", version.ref = "auto-service" }
auto-service-compiler = { group = "com.google.auto.service", name = "auto-service", version.ref = "auto-service" }
assertk = { group = "com.willowtreeapps.assertk", name = "assertk", version.ref = "assertk" }
dagger = { group = "com.google.dagger", name = "dagger", version.ref = "dagger" }
dagger-compiler = { group = "com.google.dagger", name = "dagger-compiler", version.ref = "dagger" }
inject = { group = "javax.inject", name = "javax.inject", version = "1" }
junit-bom = { group = "org.junit", name = "junit-bom", version.ref = "junit5" }
junit-jupiter = { group = "org.junit.jupiter", name = "junit-jupiter" }
junit-jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api" }
Expand All @@ -32,6 +38,8 @@ kotlin-compile-testing = { group = "dev.zacsweers.kctfork", name = "core", versi
kotlinpoet = { group = "com.squareup", name = "kotlinpoet", version.ref = "kotlinpoet" }

# Dependencies of the included build-logic
agp-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
agp-plugin-api = { module = "com.android.tools.build:gradle-api", version.ref = "agp" }
detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" }
detekt-plugin = { group = "io.gitlab.arturbosch.detekt", name = "detekt-gradle-plugin", version.ref = "detekt" }
diktat-plugin = { group = "com.saveourtool.diktat", name = "diktat-gradle-plugin", version.ref = "diktat" }
Expand All @@ -40,6 +48,7 @@ spotless-plugin = { group = "com.diffplug.spotless", name = "spotless-plugin-gra
gradle-maven-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "gradle-maven-publish-plugin" }

[plugins]
android-library = { id = "com.android.library", version.ref = "agp" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
gradle-maven-publish-plugin-base = { id = "com.vanniktech.maven.publish.base", version.ref = "gradle-maven-publish-plugin" }
Expand Down
Loading