Compiler sometimes can't find generated navigation files - android

This is a pretty bizarre one.
Occasionally, after some arbitrary change has been made to the nav graph XML, the build fails because it can't find two particular functions (nav directions) that should have been generated.
The error is like this:
main/java/com/project/upsell/UpsellFragment.kt: (99, 82): Unresolved reference: toMainFragment
main/java/com/project/upsell/UpsellVM: (35, 53): Unresolved reference: toSuccessFrag
I can "fix" it by renaming the functions, but the next time XML gets touched, the error recurs and the same "fix" is repeated. The same issue occurs in my environment, my teammate's environment and our Jenkins pipeline. Sometimes it builds fine locally but breaks in Jenkins.
The only clue I can offer is that it started happening shortly after bringing Compose into the project.
Some build info:
ext {
dagger_version = '2.28.3'
nav_version = '2.3.5'
fragment_version = '1.3.6'
coroutine_version = '1.5.0'
compose_version = '1.0.1'
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.google.gms:google-services:4.3.8'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.10"
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
classpath 'com.google.firebase:perf-plugin:1.4.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'kotlinx-serialization'
kotlinOptions {
useIR = true
freeCompilerArgs += ['-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi',
'-Xuse-experimental=kotlinx.coroutines.FlowPreview',
'-Xopt-in=androidx.paging.ExperimentalPagingApi',
'-Xuse-experimental=kotlinx.coroutines.InternalCoroutinesApi',
'-Xopt-in=kotlin.ExperimentalStdlibApi']
}
composeOptions {
kotlinCompilerVersion = "1.5.10"
kotlinCompilerExtensionVersion '1.0.0-rc01'
}
buildFeatures {
dataBinding = true
compose = true
}
Any insight would be appreciated

Fist try Invalidate Cache and Restart.
If that doesn't work, Add package name in your manifest, if it's missing.
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
...

Related

Hilt implement issue - Expected #HiltAndroidApp to have a value

I'm having a issue implement Hilt on android app...
The error is:
Expected #HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
Project gradle file
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
//Hilt
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
}
}
Model gradle file
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
dependencies {
def dagger_hilt_android = "2.38.1"
implementation("com.google.dagger:hilt-android:$dagger_hilt_android")
kapt("com.google.dagger:hilt-android-compiler:$dagger_hilt_android")
}
It is an issue with Kotlin-gradle-plugin: 1.5.20 as mentioned here https://github.com/google/dagger/issues/2684. Try upgrading your plugin to 1.5.21, and it will work.
Mohmd.h.bh,
Try applying this plugin in your build.gradle of your Android Gradle
apply plugin: 'com.google.dagger.hilt.android'
android {
// .......
}
I don't think you need to change the version of the kotlin-gradle-plugin
Regards

How do I fix Android Studio Error "Cannot access class... Check your module classpath for missing or conflicting dependencies"

I am practing how to using SafeArgs and some other kotlin tools.
Even after adding the SafeArgs plugin and classpath I still get an error saying..
Cannot access class 'com.example.testingtaskmanager.ui.updatefragment.UpdateFragmentArgs'. Check your module classpath for missing or conflicting dependencies
Here's the added SafeArgs plugin:
id 'androidx.navigation.safeargs.kotlin'
Here's the added SafeArgs classpath:
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
I have even tried the other SafeArgs Plugin, it still gave the same result.
Here's the other safeargs I tries to use:
id "androidx.navigation.safeargs"
For clarity, here's a photo of the error;
EDIT;
more of the plugin from build.gradle App/ Module level
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
//
// id "androidx.navigation.safeargs"
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-parcelize'
}
more of the plugin from build.gradle Project Level
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
//
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
if anyone still has the same problem it's easy to fix it just add this to your build.gradl
android {
.
.
.
buildFeatures{
dataBinding true
viewBinding true
}
sourceSets {
main {
kotlin {
srcDirs += 'build/generated/source/navigation-args'
}
}
}
}

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

Databinding annotation processor kapt warning

In my app module's build.gradle, I have added
dependencies {
kapt('com.android.databinding:compiler:3.1.2')
...
}
but I'm still receiving the compiler warning for
app: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.android.databinding:compiler:3.1.2'.
Everything functions, I just hate having warnings hanging around.
Any help is much appreciated!
I had same warnings until I upgraded to the latest Android Gradle build plugin and Kotlin. Now they are gone. Here is the configuration I use.
project.gradle
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
module.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
...
dataBinding {
enabled = true
}
}
dependencies {
// no kapt declaration for databinding here
}
Hope it helps.
Add following in you app build.gradle
kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
This will do the trick.
$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle
Also, add this to your module build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
You are missing apply plugin: 'kotlin-kapt' i think.

Firebase unresolved supertype with Kotlin on Android

I'm experiencing a problem in my project that uses Firebase core and messaging v11.4.2.
The gradle sync works perfectly but then I get this error when compiling:
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class com.google.android.gms.internal.zzctr, unresolved supertypes: com.google.android.gms.internal.zzee
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ‘:app:compileDebugKotlin’.
Compilation error. See log for more details
I've tried both with Kotlin version 1.1.51 and 1.2.0-beta-88; Gradle plugins v2.3.3 and 3.0.0
Any help is welcomed, thanks a lot!
This is how I've configured the project:
app build.gradle
// tried adding and removing the -kapt and -android-extensions. Didn't help.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
debug {
/// added this to be sure the class was not being left out
minifyEnabled false
shrinkResources false
useProguard false
}
...
dependencies {
// Firebase Core
implementation "com.google.firebase:firebase-core:${rootProject.ext.firebaseVersion}"
// Firebase Cloud Messaging
implementation "com.google.firebase:firebase-messaging:${rootProject.ext.firebaseVersion}"
}
...
// Keep this as the last line or the build will fail
apply plugin: 'com.google.gms.google-services'
* project build.gradle *
buildscript {
// App
ext.compileSdkVersion = 26
ext.minSdkVersion = 18
ext.buildToolsVersion = '26.0.2'
ext.targetSdkVersion = 26
// Kotlin beta, stable version doesn't compile either
ext.kotlin_version = '1.2.0-beta-88'
// Android
ext.androidSupportVersion = '26.1.0'
//TODO: implement LifecycleOwner interface from Architecture Components.
ext.lifecycleVersion = '1.0.0-beta2'
// Architecture, RxJava, Injection
ext.daggerVersion = '2.11'
ext.butterKnifeVersion = '8.8.1'
ext.rxJavaVersion = '2.1.0'
ext.rxAndroidVersion = '2.0.1'
// Google/Firebase Cloud Message
ext.firebaseVersion = '11.4.2'
// Libraries shared between modules (TODO)
}
repositories {
maven { url 'https://maven.google.com' } // Google Maven Repository
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2'} // Kotlin beta
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
//// tried this too: classpath 'com.google.firebase:firebase-plugins:1.1.1'
}
allprojects {
repositories {
jcenter()
mavenLocal()
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2'}
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
flatDir {
dirs 'libs'
}
}
Turns out the problem was an incompatible version of the Google Wallet library, being implemented by another dependency of the project and conflicting with the one imported by the com.google.gms.google-services plugin.
For some reason gradle didn't state this problem and I had to go through the code until I found the place where Wallet was trying to access a class that was successfully importing but at the same time it couldn't be found.
Simply by updating the version in the other module everything was fixed :)

Categories

Resources