导航组件重复 NavArgs 问题

Issue with Navigation Component Duplicate NavArgs

我有一个片段:

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

谁能帮我解决这个问题?

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

有两个solutions/workarounds。

  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 设置为开放 class 并且 SomeFragment1 可以使用空实现扩展 SomeFragment。

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