Helper
Helper

Reputation: 125

Xamarin + Mvvmcross + Multidex crash when change on/offline

Greetings colleagues from the world xamarin. I need help.

I developed an application in Xamarin with Mvvmcross 4.4 and Firebase. In the Android project I had to implement multidex. The issue that occurs is when the application is running and the connection is lost (airplane mode, wifi off, etc) or vice versa.

In the custom list to build the multidex, all the main dependencies to Firebase, the application itself and the framework, are placed in the main file.

The reported crash however expresses this:

java.lang.RuntimeException: Unable to instantiate receiver com.developer.appname.Reachability: java.lang.ClassNotFoundException: Didn't find class "com.developer.appname.Reachability" on path: DexPathList[[zip file "/data/app/com.developer.appname-1/base.apk"],nativeLibraryDirectories=[/data/app/com.developer.appname-1/lib/arm, /vendor/lib, /system/lib]]
android.app.ActivityThread.handleReceiver()ActivityThread.java:2926
android.app.ActivityThread.access$1800()ActivityThread.java:172
android.app.ActivityThread$H.handleMessage()ActivityThread.java:1499
android.os.Handler.dispatchMessage()Handler.java:102
android.os.Looper.loop()Looper.java:145
android.app.ActivityThread.main()ActivityThread.java:5832
java.lang.reflect.Method.invoke(Native Method)
java.lang.reflect.Method.invoke()Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run()ZygoteInit.java:1399
com.android.internal.os.ZygoteInit.main()ZygoteInit.java:1194
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.developer.appname.Reachability" on path: DexPathList[[zip file "/data/app/com.developer.appname-1/base.apk"],nativeLibraryDirectories=[/data/app/com.developer.appname-1/lib/arm, /vendor/lib, /system/lib]]
dalvik.system.BaseDexClassLoader.findClass()BaseDexClassLoader.java:56
java.lang.ClassLoader.loadClass()ClassLoader.java:511
java.lang.ClassLoader.loadClass()ClassLoader.java:469
android.app.ActivityThread.handleReceiver()ActivityThread.java:2921
... 9 more

Reachability checks the state of the connection and is used from the core:

public class Reachability : BroadcastReceiver, IReachability { ... }

<application android:name=".appname" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:allowBackup="true">
    <receiver android:name=".Reachability">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

If the application is launched offline it is detected correctly and proceeds to display the expected data from cache. The same happens when the app is launched and there is connection that the data is displayed in real time from firebase. Emphasizing, it only fails when the connection change to on or off, according to the previous state.

Thank you very much, any help is welcome.

Upvotes: 0

Views: 185

Answers (1)

York Shen
York Shen

Reputation: 9084

Unable to instantiate receiver com.developer.appname.Reachability: java.lang.ClassNotFoundException: Didn't find class "com.developer.appname.Reachability"

I have reproduced your problem and found a solution. As SushiHangover said, Xamarin auto-generates class ids if you do not specify one via an Attribute. Xamarin uses Attributes in code to fill in the Manifest when building. Here is the document about how to create a BraoadcastReceiver with Attributes.

When you use Attributes for your BroadcastReceiver :

[BroadcastReceiver(Enabled = true, Exported = false)]
[IntentFilter(new[] { Android.Net.ConnectivityManager.ConnectivityAction })]
public class Reachability : BroadcastReceiver { ... }

Register your BroadcastReceiver :

Reachability mReceiver = new Reachability ();
RegisterReceiver(mReceiver, new IntentFilter(ConnectivityManager.ConnectivityAction));

It works fine.

Upvotes: 1

Related Questions