cortinico / kotlin-android-template

Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️
MIT License
1.72k stars 241 forks source link

Where should I add e.g. Hilt Android and AndroidX Navigation Safe Args Gradle Plugins? #48

Open VincentJoshuaET opened 2 years ago

VincentJoshuaET commented 2 years ago

Should I add them inside buildSrc folder?

plugins {
    `kotlin-dsl`
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation(group = "com.android.tools.build", name = "gradle", version = "7.0.1")
    implementation(kotlin(module = "gradle-plugin", version = "1.5.30"))
    implementation(group = "com.google.dagger", name = "hilt-android-gradle-plugin", version = "2.38.1")
    implementation(group = "androidx.navigation", name = "navigation-safe-args-gradle-plugin", version = "2.3.5")
}

Thanks

cortinico commented 2 years ago

Should I add them inside buildSrc folder?

AGP and Kotlin bump, you can do them if you wish.

As for hilt and safe-args, I won't add them for now. I'm thinking about having a more opinionated branch where some small setup is also included.

VincentJoshuaET commented 2 years ago

Should I add them inside buildSrc folder?

AGP and Kotlin bump, you can do them if you wish.

As for hilt and safe-args, I won't add them for now. I'm thinking about having a more opinionated branch where some small setup is also included.

Where do you suggest I add them? I can't use the new plugins block with them

cortinico commented 2 years ago

I can't use the new plugins block with them

Why not?

VincentJoshuaET commented 2 years ago

I can't use the new plugins block with them

Why not?

Screenshot_20210904-172103_Samsung Internet.png

https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_markers

https://issuetracker.google.com/issues/64551265

Google's fault

Fixed for the AGP, but not for Navigation or Dagger Hilt

cortinico commented 2 years ago

You can always overcome the lack of a plugin marker by specifying a custom resolution strategy. That's actually what this template was doing long before this issue was fixed for AGP:

https://github.com/cortinico/kotlin-android-template/blob/d66f85561128698f3b83b78395167a6753cd2e34/settings.gradle.kts#L2-L11

VincentJoshuaET commented 2 years ago

Does not seem to work for Hilt:

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            when (requested.id.id) {
                "androidx.navigation.safeargs.kotlin" -> {
                    useModule("androidx.navigation:navigation-safe-args-gradle-plugin:${requested.version}")
                }
                "dagger.hilt.android.plugin" -> {
                    useModule("com.google.dagger:hilt-android-gradle-plugin:${requested.version}")
                }
            }
        }
    }
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
cortinico commented 2 years ago

The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.

You're missing the necessary dependencies in order to make Hilt work: https://stackoverflow.com/a/62882856/3765748

VincentJoshuaET commented 2 years ago

I have those already. I dont have this issue if I use the code in my original post.

cortinico commented 2 years ago

I have those already. I dont have this issue if I use the code in my original post.

Don't you mind sharing your project and/or your build.gradle[.kts] files?

VincentJoshuaET commented 2 years ago

I cannot post the whole files, but here are the important ones.

I omitted the unrelated dependencies. This is working as expected. Once I implement the logic in pluginManagement and remove the Hilt and AndroidX Navigation Safe Args plugins from buildSrc, and set their versions to the root build.gradle.kts file, it fails.

build.gradle.kts:

plugins {
    `android-app` apply false
    `android-library` apply false
    `kotlin-android` apply false
    `kotlin-kapt` apply false
    `kotlin-serialization` version PluginVersions.Kotlin apply false
    `kotlin-parcelize` apply false
    `hilt-android` apply false
    `androidx-navigation-safe-args` apply false
}

allprojects {
    group = ""
    repositories {
        google()
        mavenCentral()
    }
}

buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation(group = "com.android.tools.build", name = "gradle", version = "7.0.2")
    implementation(kotlin(module = "gradle-plugin", version = "1.5.30"))
    implementation(group = "com.google.dagger", name = "hilt-android-gradle-plugin", version = "2.38.1")
    implementation(group = "androidx.navigation", name = "navigation-safe-args-gradle-plugin", version = "2.3.5")
}

app/build.gradle.kts

plugins {
    `android-app`
    `kotlin-android`
    `kotlin-kapt`
    `kotlin-serialization`
    `kotlin-parcelize`
    `hilt-android`
    `androidx-navigation-safe-args`
}

android {
    compileSdk = Android.CompileSdkVersion
    buildToolsVersion = Android.BuildToolsVersion
    ndkVersion = Android.NdkVersion

    defaultConfig {
        applicationId = ""
        minSdk = Android.MinSdk
        targetSdk = Android.TargetSdk
        versionCode = Android.VersionCode
        versionName = Android.VersionName
    }

    signingConfigs {
        register("release") {
            enableV1Signing = true
            enableV2Signing = true
            enableV3Signing = true
            enableV4Signing = true
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix = ".debug"
        }
        release {
            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            ndk {
                debugSymbolLevel = "FULL"
            }
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
        freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
    }

    buildFeatures {
        viewBinding = true
    }

    lint {
        isCheckDependencies = true
    }
}

dependencies {
    implementation(libs.androidx.hilt.work)
    kapt(libs.androidx.hilt.compiler)
    implementation(libs.hilt.android)
    kapt(libs.hilt.compiler)
}

kapt {
    correctErrorTypes = true
}

hilt {
    enableAggregatingTask = true
}
cortinico commented 2 years ago

Seems like you're using precompiled script plugins right? How comes you have those implementation dependencies inside buildSrc/build.gradle.kts?

VincentJoshuaET commented 2 years ago

Originally I had those in the buildscript block in the root directory build.gradle.kts, but I didn't know where to move them hence this post.

darkpandawarrior commented 10 months ago

Has this been resolved? Can someone give the solution on how to setup dagger hilt & navigation in this template?