Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation not working in release apk android java

Tags:

java

android

I am having a weird problem with my android app. When I run the app in emulator, it works fine but when I generate a signed apk and install it on my device, it shuts down on startup giving me this error:

Caused by: android.view.InflateException: Binary XML file line #14 in com.app.pickup:layout/activity_main: Binary XML file line #18 in com.app.pickup:layout/content_main: Error inflating class fragment
                                                                                                    Caused by: android.view.InflateException: Binary XML file line #18 in com.app.pickup:layout/content_main: Error inflating class fragment
                                                                                                    Caused by: java.lang.IllegalArgumentException: No @Navigator.Name annotation found for r

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main" />


</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/content_main" />


    <ImageView
        android:id="@+id/navBtn"
        android:layout_width="50dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginStart="20dp"
        android:layout_marginTop="10dp"
        app:srcCompat="@drawable/hamburger_icon" />

    <ImageView
        android:id="@+id/header_logo"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_marginTop="25dp"
        android:importantForAccessibility="no"
        android:src="@drawable/logo_heading" />


</FrameLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

This is how I am setting it up in MainActivity.java

mAppBarConfiguration = new AppBarConfiguration.Builder(
                    R.id.nav_active_requests, R.id.nav_available_requests, R.id.nav_completed_requests,
                    R.id.nav_scheduled_requests, R.id.nav_share, R.id.nav_contact, R.id.nav_block, R.id.nav_logout)
                    .setOpenableLayout(drawerLayout)
                    .build();


            //NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
            NavigationUI.setupWithNavController(navigationView, navController);

            navController.setGraph(R.navigation.worker_nav_items);

            navigationView.inflateMenu(R.menu.worker_nav_menu);

****** UPDATE ******

After replacing fragment with FragmentContainerView, it still gives me the same error.

Caused by: android.view.InflateException: Binary XML file line #14 in com.app.pickup:layout/activity_main: Binary XML file line #18 in com.app.pickup:layout/content_main: Error inflating class fragment
                                                                                                        Caused by: android.view.InflateException: Binary XML file line #18 in com.app.pickup:layout/content_main: Error inflating class fragment
                                                                                                        Caused by: java.lang.IllegalArgumentException: No @Navigator.Name annotation found for r

Note that this error only happens in release apk. When I run the app on emulator, it works fine.

like image 336
Salman Avatar asked Mar 03 '26 22:03

Salman


1 Answers

After spending hours on debugging and reading tons of articles, adding these 2 lines proguard rules fixed it for me

-keepattributes RuntimeVisibleAnnotations
-keep class * extends androidx.navigation.Navigator
like image 123
Salman Avatar answered Mar 06 '26 10:03

Salman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!