【问题标题】:Issue with Navigation Component Duplicate NavArgs导航组件重复 NavArgs 的问题
【发布时间】:2019-11-22 06:19:35
【问题描述】:

我有一个片段:

class SomeFragment {
    private val args by navArgs<SomeFragmentArgs>()
}

这个片段用在两个导航图中:

first_nav.xml

....
<fragment
    android:id="@+id/initialFragment"
    android:name="com.example.InitialFragment"
    android:label="Initial Fragment">
    <action
        android:id="@+id/action_to_some_fragment"
        app:destination="@id/someFragment" />
</fragment>
<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="Some Label">
    <argument
        android:name="someType"
        app:argType="com.example.someType"/>
</fragment>
....

second_nav.xml

....
<fragment
    android:id="@+id/initialFragment2"
    android:name="com.example.InitialFragment2"
    android:label="Initial Fragment">
    <action
        android:id="@+id/action_to_some_fragment"
        app:destination="@id/someFragment" />
</fragment>
<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="Some Label">
    <argument
        android:name="someType"
        app:argType="com.example.someType"/>
</fragment>
....

但是当我为发布 R8 构建项目时:

R8:程序类型已存在:com.example.SomeFragmentArgs$Companion

谁能帮我解决这个问题?

【问题讨论】:

  • 您使用的是哪个版本的导航组件?
  • 您在不同图表中的片段具有相同的 id,即使认为它不会相互冲突,因此生成的类将是相同的。因此,在两个图中尝试不同的参数名称(即 firstNavSomeType 和 secondNavSomeType),问题可能会在重建项目时得到解决。
  • 我使用的是 2.1.0。我相信那是最新版本。
  • 他们的事情是,它只会在发布/签名时抛出这个错误。在调试中构建项目时,它很好。但我会尝试你的建议。
  • 你有什么进展吗?我也有同样的问题

标签: android android-architecture-navigation android-safe-args


【解决方案1】:

我也面临这个问题。我们正在使用多个模块化开发,方向类是在不同的模块中生成的。这样到处都有几个 FragmentDirections 并导致 R8 程序类型已经存在,因为它们都有伴生对象。

有两种解决方案/解决方法。

  1. 将此片段隔离为独立的navigation.xml

在你的情况下你可以写一个 some_fragment_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/someFragment"
android:id="@+id/some_fragment_nav">

<fragment
    android:id="@+id/someFragment"
    android:name="com.example.SomeFragment"
    android:label="lable">

    <action
        android:id="@+id/what_ever_action_you_want"
        app:destination="@id/share_this_id_to_ids" />


    <argument
        android:name="someType"
        app:argType="com.example.someType"/>

</fragment>

</navigation>

并使用

<include app:graph="@navigation/some_fragment_nav"/>

在您的两个导航图中。那可行。如果您正在使用多模块开发。您需要通过将您的 id 写入 ids.xml 来共享操作 id。

  1. 将 SomeFragment 复制并粘贴到不同的命名中,例如 SomeFragment1.kt SomeFragment2.kt。也不会出现任何 R8 程序类型。此外,您还可以将 SomeFragment 设为开放类,而 SomeFragment1 可以使用空实现扩展 SomeFragment。

我的个人前景.. 2 比 1 好,因为它们在我看来都是解决方法。直到 jetbrains 改变生成的 FragmentDirection 类的规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2019-01-26
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多