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

java.lang.NoSuchMethodError #323

Closed
liujingxing opened this issue Nov 6, 2021 · 1 comment
Closed

java.lang.NoSuchMethodError #323

liujingxing opened this issue Nov 6, 2021 · 1 comment

Comments

@liujingxing
Copy link
Owner

liujingxing commented Nov 6, 2021

新建core module,并依赖RxHttp,如下

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

dependencies {
    implementation "com.squareup.okhttp3:okhttp:4.9.1"
    implementation 'com.github.liujingxing.rxhttp:rxhttp:2.7.2'
    kapt 'com.github.liujingxing.rxhttp:rxhttp-compiler:2.7.2'
}

接着新建Http类,如下:

object Http {

    @DefaultDomain
    const val base_url = "https://www.wanandroid.com/"

    suspend fun test() {
        RxHttp.get("/article/list/0/json")
            .toFlow<String>()
            .catch { }
            .collect { }
    }
}

此时,执行assembleRelease命令,将core module打包为core-release.aar,并将该aar放至app module(主module)的libs文件下并依赖,如下:

repositories {
   flatDir {
       dirs 'libs'
   }
}

dependencies {
    implementation(name: 'core-release', ext: 'aar')

    //因aar会丢失一些远程依赖库,故需要手动依赖
    implementation "com.squareup.okhttp3:okhttp:4.9.1"
    implementation 'com.github.liujingxing.rxhttp:rxhttp:2.7.2'
    implementation 'com.google.code.gson:gson:2.8.7'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
}

app moduleMainActivity调用Http.test()方法,如下:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            lifecycleScope.launch {
                Http.test()
            }
        }
    }

Android 6.0的手机上运行并点击Button按钮,就崩溃,具体日志如下:

QQ20211107-005326@2x

其它设备未发现此问题

@liujingxing
Copy link
Owner Author

liujingxing commented Nov 6, 2021

我这没有Android 6.0设备,但我通过执行assembleDebug命令,生成app-debug.apk,将其安装到Android 11的设备上复现了这个问题,必现的。

直接运行或通过assembleRelease命令,生成app-release.apk,并未发现此问题

进过一番摸索,这并非是RxHttp的问题,只要core module里使用了任意第三方库的接口静态方法,都会有这个问题,巧的是,RxHttp.get方法内部刚好调用了Param.get方法,而Param是接口类,Param.get是该接口的静态方法,所以就出现了上面的问题;

初步猜测是拆包所引起的问题,因为我查看app-debug.apk后,没有在dex文件找到Param.get方法,而app-release.apk里的dex文件是能找到Param.get方法的,所以app-release.apk没有问题

下个版本将Param接口类改为抽象类以兼容这种情况

另外,我还发现,通过以下方式依赖aar,该问题也可以得到解决

implementation files('libs/xxx.aar')

并删除以下代码

repositories {
    flatDir {
        dirs 'libs'
    }
}

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

1 participant