简体   繁体   English

Jetpack Compose 项目在我向其添加匿名 class 后无法构建

[英]The Jetpack Compose project doesn't build after I add an anonymous class to it

I'm trying to add Jetpack Compose to my project, but after making all changes to my build.gradle file I'm not able to build it.我正在尝试将 Jetpack Compose 添加到我的项目中,但在对我的build.gradle文件进行所有更改后,我无法构建它。 And the reason is having anonymous classes in the project.原因是项目中有匿名类。

Instead of describing my project, build.gradle file and all changes I made, I used the android-compose-codelabs project as a sandbox.我没有描述我的项目、 build.gradle文件和我所做的所有更改,而是将android-compose-codelabs项目用作沙箱。

So basically, I clone the BasicsCodelab project and can successfully build it, but after adding this block at the end of onCreate method ( MainActivity.kt ):所以基本上,我克隆了 BasicsCodelab 项目并可以成功构建它,但是在onCreate方法( MainActivity.kt )末尾添加这个块之后:

val listener = object : SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String): Boolean {
        return false
    }
    override fun onQueryTextChange(newText: String): Boolean {
        return true
    }
}
SearchView(this).setOnQueryTextListener(listener)

and clearing the project I'm unable to build it anymore.并清除我无法再构建的项目。 So, adding an anonymous class to your project (with Jetpack Compose) leads to build failure.因此,将匿名 class 添加到您的项目(使用 Jetpack Compose)会导致构建失败。

Error:错误:

LiveLiterals$MainActivityKt.class

    com.android.tools.r8.errors.b: Space characters in SimpleName 
    'Boolean$fun-onQueryTextSubmit$class-$no name provided$$val-listener$fun-onCreate$class-MainActivity' 
    are not allowed prior to DEX version 040

I assume the no name provided part is the reason (should be no-name-provided ?) but I'm not that familiar with building pipeline to figure out the problem.我认为no name provided的部分是原因(应该是no-name-provided ?)但我不太熟悉构建管道来找出问题所在。 I tried to disable R8 but no result.我试图禁用 R8 但没有结果。

This was discussed a few times in Kotlinlang Slack this week, notably this thread and this thread .本周在 Kotlinlang Slack 中讨论了几次,特别是这个线程这个线程

This is a known bug tracked internally, though I don't see a public bug report for it in the issue tracker.这是一个内部跟踪的已知错误,尽管我在问题跟踪器中没有看到关于它的公开错误报告。 The workaround is to make a regular named subclass, not an anonymous class:解决方法是创建一个常规的命名子类,而不是匿名的 class:

class MyQueryTextListener : SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String): Boolean {
        return false
    }
    override fun onQueryTextChange(newText: String): Boolean {
        return true
    }
}

val listener = MyQueryTextListener()

SearchView(this).setOnQueryTextListener(listener)

The issue is fixed in version 1.0.0-alpha02 of Jetpack Compose.此问题已在 Jetpack Compose 1.0.0-alpha02版中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM