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

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

Open
VincentJoshuaET opened this issue Sep 1, 2021 · 13 comments

Comments

@VincentJoshuaET
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

I can't use the new plugins block with them

Why not?

@VincentJoshuaET
Copy link
Author

VincentJoshuaET commented Sep 4, 2021

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
Copy link
Owner

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:

resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.android.library") {
useModule("com.android.tools.build:gradle:${requested.version}")
}
if (requested.id.id == "com.android.application") {
useModule("com.android.tools.build:gradle:${requested.version}")
}
}
}

@VincentJoshuaET
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

VincentJoshuaET commented Sep 6, 2021

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

@cortinico
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@VincentJoshuaET
Copy link
Author

VincentJoshuaET commented Sep 7, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants