java.lang.NoSuchMethodError: No static method 'myMethod'

I have a extension method:

fun StoresClientFragment?.onClickButtonBack(){
   this?.listener?.onStoresFragmentClickBtnMenu()
}

The app installs without any errors; but when the method is invoked at runtime it gives me the following error:

java.lang.NoSuchMethodError: No static method onClickButtonBack (Lcom/app/common_client/ui/fragment /stores/StoresClientFragment;) V in class Lcom/app/common_client/commons/CommonsAppTypeGroupKt; or its super classes (declaration of 'com.app.common_client.commons.CommonsAppTypeGroupKt' appears in /data/app/com.myapp.client-DWp0y3iNC3tsmBZkowlpfw==/base.apk!classes2.dex).

I have multidex enabled

In build.gradle

android {
    defaultConfig {
        multiDexEnabled = true
    }
}

dependencies {
   implementation 'androidx.multidex:multidex:2.0.1'
}

In Application class

class ApplicationClient : MultiDexApplication(){
}

In the Manifest

<application
        android:name=".commons.application.ApplicationClient"

Note: I am using product flavors and modules, that method is in a src shared by some product flavors

sourceSets {
         flavorA {
             java.srcDirs + = "src/sharedFolder/java" //here is 'onClickButtonBack' method
         }
         flavorB {
             java.srcDirs + = "src/sharedFolder/java"
         }
}

Upvotes: 12

Views: 33613

Answers (6)

Inliner
Inliner

Reputation: 1659

In my case the same error was triggered because there was no Dagger dependency shared from another module. Adding an abstract method providing this dependency to an interface helped.

That interface then is implemented by the main Dagger component of my app and there is a @Binds method in a dagger module that is added to this component.

I guess that way Dagger knows that it should generate a provider method and it also knows where it can get it.

Upvotes: 0

Pasquale Piccolo
Pasquale Piccolo

Reputation: 311

In my case was a Composable function in another module. I solved it adding

android{
    buildFeatures {
       compose = true
    } 
    composeOptions {
       kotlinCompilerExtensionVersion = "1.5.2"
    }
}

to the gradle file of the module.

Upvotes: 9

J. Doe
J. Doe

Reputation: 13063

I had two files named exactly the same, 1 in my main project and 1 in my test package. For some reason this conflicts, after renaming the file in my test package, things worked again.

Upvotes: 0

Alex Styl
Alex Styl

Reputation: 4219

I solved it in my case for changing the package of the file it was trying to get the method from.

My setup was 2 kotlin modules (A depends on B). Both had the same package name and class. When I was trying to use B.method() from A, it seems like it was trying to call A.method() instead.

not an expert on this. just what i noticed

Upvotes: 1

DropDrage
DropDrage

Reputation: 935

Build -> Rebuild Project helped me. It seems to be a bug in Android Studio code caching

Upvotes: 32

I have solved it, however I don't know why, the only thing I did was change the name of the file where the method was.

MyFile.kt by MyNewFile.kt

fun StoresClientFragment? .onClickButtonBack () {
     this? .listener? .onStoresFragmentClickBtnMenu ()
}

I hope it helps someone who is experiencing the same thing, and I would also like if there is someone who knows why that has worked, share it, thank you

Upvotes: 10

Related Questions