Why Hilt version 2.38.1 not found? - android

When I am trying to sync my build.gradle file, I am facing some error like this: Plugin [id: 'com.google.dagger.hilt.android', version: '2.38.1', apply: false] was not found in any of the following sources:
I am getting this error only when I am using the Hilt version 2.38.1 which is also currently listed on android official documentation, but when I am using the version '2.41' without changing any other thing, it works! My question is to ask why and how changing the version affects the sync and why version 2.38.1 was not found and 2.41 was.
My guess is something related to repositories kind of thing or the scope where we are looking for these things but I am not sure about any of these, so can anyone explain these?
My project-level build.gradle looks like this:
buildscript {
ext {
compose_ui_version = '1.1.1'
}
}
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'com.google.dagger.hilt.android' version '2.38.1' apply false
}
In my app level build.gradle:
I added these 2 plugins
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
And these dependencies:
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"

Related

Can we know each new gradle plugin format from the old way of classpath plugin definition?

I was having problem trying to find how can I add a Dagger Plugin in the latest Android Studio Generated Plugin, as I don't know how what's the new way of defining
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.41'.
I have to search much before finding it is now
id 'com.google.dagger.hilt.android' version '2.41' apply false
I find no direct way to deduce the new way from the old one. Hence asking there, how can one know how to define a plugin is defined in the new gradle?
I check older Android Studio generated project, we have plugins defined below
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'
And the newer Android Studio generated project, plugins are now as below
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'dagger.hilt.android.plugin'
id 'kotlin-kapt'
}
Most of it looks like a 1-1 direct change, but 'kotlin-android' becomes 'org.jetbrains.kotlin.android'. Is there a way for us to be sure of what it is?
Similarly for the classpath, it used to be
dependencies {
classpath "com.android.tools.build:gradle:7.3.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.41'
}
and now
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'com.google.dagger.hilt.android' version '2.41' apply false
}
You'll see that
"com.android.tools.build:gradle:7.3.1" replace by 2 of them 'com.android.application' version '7.3.1' and 'com.android.library' version '7.3.1'
org.jetbrains.kotlin:kotlin-gradle-plugin becomes org.jetbrains.kotlin.android
com.google.dagger:hilt-android-gradle-plugin becomes com.google.dagger.hilt.android
I cannot deduce the new way of name from the old way. Is there a way to determine the new naming?
I don't think there is a way to deduce it. From the official doc for writing plugins, you can register any id for your plugin:
gradlePlugin {
plugins {
create("simplePlugin") {
id = "org.example.greeting"
implementationClass = "org.example.GreetingPlugin"
}
}
}
and use it in another project:
plugins {
id("org.example.greeting") version "1.0-SNAPSHOT"
}
For now, I always look up the usage from the plugin's guide or from https://plugins.gradle.org. Even the same plugin may have more than one id.

How to add classpath hilt-android-gradle-plugin in android studio chipmunk and higher

earlier we could add dagger-hilt using this in root build.gradle file:
buildscript {
...
dependencies {
...
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
how can we add this in android studio chipmunk and higher
id 'com.google.dagger.hilt.android' version '2.43' apply false
You can use this code
First, add a Plugin in Root level Gradle like below
plugins {
// other plugins...
id 'com.google.dagger.hilt.android' version '2.43.2' apply false
}
And then add Plugin in App level Gradle like below
plugins {
// other plugins...
id 'com.android.application'
id 'com.google.dagger.hilt.android'
}
For reference please read this documentation:
https://dagger.dev/hilt/gradle-setup

How to Migrate build.gradle from Groovy to Kotlin DSL in Android Studio Bumblebee?

I want to migrate build.gradle file from Groovy to Kotlin, since I updated my Android Studio to Bumblebee, I'm not sure how to do it.
This is how build.gradle looks when I create a new project.
I'm not sure on how to migrate plugins{} part.
buildscript {
ext {
compose_version = '1.0.1'
}
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I got the answer in the documentation here https://developer.android.com/studio/build#kts.
Here's how it's written in Kotlin.
plugins {
id("com.android.application") version "7.1.0-beta02" apply false
id("com.android.library") version "7.1.0-beta02" apply false
id("org.jetbrains.kotlin.android") version "1.5.30" apply false
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
The following steps worked for me.
Step-1: Create the plugins tag and give it the id values. delete the buildscript tag.
Step-2: Open the pluginManagement tag in the settings.gradle file and make some tags and values.
Step-3: In the last step, update the values defined at the top in the build.gradle file as id.
Optional: Since it is in the build.gradle file, it must be deleted.
repositories {
mavenCentral()
}

Android Studio Bumblebee Navigation can't access FragmentDirections for action

I create such action before Android Studio bumblebee update and it was working fine.
fragment_button.setOnClickListener{
val action = FeedFragmentDirections.actionFeedFragmentToCountryFragment()
Navigation.findNavController(it).navigate(action)
}
But currently FragmentDirections are not accessible.I don't know where the problem comes from, I guess I can't manage to add navigation.
My build.gradle:
buildscript {
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0")
}
}
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false
id 'com.google.gms.google-services' version '4.3.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
My build.gradle App
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}
android {
dataBinding {
enabled = true
}
dependencies {
def navVersion = '2.4.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"
}
I am retracting my previous answer!
After a bit of research, I realized the plugin block in your project-level build.gradle has the purpose of assigning dependencies that can be used by subprojects!
https://docs.gradle.org/current/userguide/plugins.html#sec:subprojects_plugins_dsl
Instead of declaring:
plugins {
...
id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false
...
}
You want to add id 'androidx.navigation.safeargs.kotlin' to the app-level plugins block so it can be used directly in your project.
Have you tried cleaning and re-building your project?
This may be a similar case to: Safeargs library doesnt generate direction class
If not, given that you are having trouble accessing the FragmentDirections class, it may be due to the default addition of the non-transitive R class property in the Bumblebee update.
In the gradle.properties[app] file you will find:
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
A good summation of the change and solutions to migrating R classes can be found here:
https://blog.blundellapps.co.uk/speed-up-your-build-non-transitive-r-files/
Best of luck!
I solved my problem by cleaning my project and rebuilding and adding.
Thank you very much for your support
Build.gradle
id ("androidx.navigation.safeargs")
build
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10'
def nav_version = "2.3.0"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
plugins {
id 'com.android.application' version '7.1.0' apply false
id 'com.android.library' version '7.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'com.google.gms.google-services' version '4.3.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
okay so after bumblebee update gradle structure is changed.
but as mentioned above you can still add
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.1")
}
}
before the plugins{} in project level gradle.
but the better alternative is to just use
id 'androidx.navigation.safeargs.kotlin' version '2.4.1' apply false
in plugins{} at project level gradle
both of above will cover first step you still need to add
id 'androidx.navigation.safeargs.kotlin'
in plugins{} at app level gradle
**if you had already added these but now they are not resolving or cannot access fragment direction the reason might be because you have update you gradle to 7.1.0 and safe-args-plugin:2.4.0 is not compatible with that so make sure to use safe-args-plugin:2.4.1 or 2.5.0-alpha01 with gradle 7.1.0

Expected #AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin?

When I am using Hilt in android with Room I got this kinda error.
The full log is here:
home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/MyApplication.java:7: error: [Hilt]
public class MyApplication extends android.app.Application {
^
Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
[Hilt] Processing did not complete. See error above for details./home/someone/Desktop/Calculator/app/build/tmp/kapt3/stubs/debug/com/hamidjonhamidov/calculator/ui/main/MainActivity.java:7: error: [Hilt]
Anyone knows solution for this?
I had this issue after upgrading Kotlin to 1.5.20.
Adding kapt.use.worker.api=false in gradle.properties worked for me the problem
Checkout dagger issue Support for Kotlin 1.5.20
Fortunately, there is simple solution.
In build.gradle in database scheme, we should use arguments += instead of arguments = .
defaultConfig{
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
Or/And in buld.gradle
You should apply plugin like:
apply plugin 'dagger.hilt.android.plugin'
This solved the problem)
UPDATE
Upgrade Hilt to v28.1.0 and Kotlin to v1.5.21 should fix this issue
OLD ANSWER
If you are on kotlin 1.5.20, answer of Mr-wil will decrease build speed as said in official doc:
To improve the speed of builds that use kapt, you can enable the
Gradle worker API for kapt tasks. Using the worker API lets Gradle run
independent annotation processing tasks from a single project in
parallel, which in some cases significantly decreases the execution
time.
Instead, set:
kapt {
javacOptions {
// These options are normally set automatically via the Hilt Gradle plugin, but we
// set them manually to workaround a bug in the Kotlin 1.5.20
option("-Adagger.fastInit=ENABLED")
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}
}
source
This generic error message can also appear in many circumstances. As a more generic check, ensure that your module's build.gradle file, ensure that you has:
apply plugin: 'dagger.hilt.android.plugin'
at the top.
This was due to a bug in Kotlin 1.5.20. It is fixed in Kotlin 1.5.21.
So all you need to do is to upgrade to the latest version of Kotlin.
I had the same problem and it seems like there is a problem in kotlin-kapt plugin. If you guys have tried out all the above answers and didn't get resolved, please try the below code in your build.gradle(module-level) outside the dependencies{} block
kapt {
javacOptions {
option("-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true")
}
}
in the build.gradle of your Android Gradle modules apply the plugin:
apply plugin: 'com.android.application'
apply plugin: 'dagger.hilt.android.plugin'
android {
// ...
}
see detail here
I'm still facing the same issue in 2022
I have solved the problem by adding
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
to build.gradle project and adding
id 'dagger.hilt.android.plugin'
to plugins in build.gradle app
In my case it solved by declaring plugin:
plugins {
id("dagger.hilt.android.plugin")
}
Check if all these are added.In my case I had missed adding one of the below line.
In project level build.gradle
buildscript {
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:2.44"
}
}
In app level build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.44"
implementation "androidx.hilt:hilt-common:1.0.0"
kapt "com.google.dagger:hilt-android-compiler:2.44"
}
-In app level build.gradle---add the following in newer versions
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
In project level build.gradle
buildscript {
ext.kotlin_version = "1.7.21"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

Categories

Resources