title: Can't connect to MQTT server on android
body: <p>I'm trying to write a simple code to publish and subscribe to a MQTT server on Android Java. When my app tries to run onCreat(), it keeps crashing. I tried so many solutions in the internet, but I can't figure this out and fix this, please help.</p>
  <p>Here is my code and error log:</p>
  <p>This is my MainActivity.java</p>
  <pre class="lang-java prettyprint-override"><code>import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import org.eclipse.paho.android.service.MqttAndroidClient;
  import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
  import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  import org.eclipse.paho.client.mqttv3.IMqttToken;
  import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  import org.eclipse.paho.client.mqttv3.MqttException;
  import org.eclipse.paho.client.mqttv3.MqttMessage;
  import java.io.UnsupportedEncodingException;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.util.Log;
  
  public class MainActivity extends AppCompatActivity {
  
      private MqttAndroidClient mqttAndroidClient;
      private static final String TAG = &quot;PahoMqttClient&quot;;
  
      @Override
      protected void onCreate ( Bundle savedInstanceState ) {
          super.onCreate ( savedInstanceState );
          setContentView ( R.layout.activity_main );
          getMqttClient ( getApplicationContext(),&quot;tcp//test.mosquitto.org:1883&quot;,&quot;esp32_hello&quot; );
      }
  
      public MqttAndroidClient getMqttClient( Context context, String brokerUrl, String clientId) {
          mqttAndroidClient = new MqttAndroidClient(context, brokerUrl, clientId);
          try {
              IMqttToken token = mqttAndroidClient.connect(getMqttConnectionOption());
              token.setActionCallback(new IMqttActionListener() {
                  @Override
                  public void onSuccess(IMqttToken asyncActionToken) {
                      mqttAndroidClient.setBufferOpts(getDisconnectedBufferOptions());
                      Log.d( &quot;TAG&quot;, &quot;Success&quot;);
                  }
                  @Override
                  public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                      Log.d(&quot;TAG&quot;, &quot;Failure &quot; + exception.toString());
                  }
              });
          } catch (MqttException e) {
              e.printStackTrace();
          }
          return mqttAndroidClient;
      }
      public void disconnect(@NonNull MqttAndroidClient client) throws MqttException {
          IMqttToken mqttToken = client.disconnect();
          mqttToken.setActionCallback(new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken iMqttToken) {
                  Log.d(TAG, &quot;Successfully disconnected&quot;);
              }
  
              @Override
              public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                  Log.d(TAG, &quot;Failed to disconnected &quot; + throwable.toString());
              }
          });
      }
  
      @NonNull
      private DisconnectedBufferOptions getDisconnectedBufferOptions() {
          DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions();
          disconnectedBufferOptions.setBufferEnabled(true);
          disconnectedBufferOptions.setBufferSize(100);
          disconnectedBufferOptions.setPersistBuffer(false);
          disconnectedBufferOptions.setDeleteOldestMessages(false);
          return disconnectedBufferOptions;
      }
  
      @NonNull
      private MqttConnectOptions getMqttConnectionOption() {
          MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
          mqttConnectOptions.setCleanSession(false);
          mqttConnectOptions.setAutomaticReconnect(true);
          //mqttConnectOptions.setWill(Constants.PUBLISH_TOPIC, &quot;I am going offline&quot;.getBytes(), 1, true);
          //mqttConnectOptions.setUserName(&quot;ngbllzzy&quot;);
          //mqttConnectOptions.setPassword(&quot;WtjhZKl3OPoK&quot;.toCharArray());
          return mqttConnectOptions;
      }
  /*
  
      public void publishMessage(@NonNull MqttAndroidClient client, @NonNull String msg, int qos, @NonNull String topic)
              throws MqttException, UnsupportedEncodingException {
          byte[] encodedPayload = new byte[0];
          encodedPayload = msg.getBytes(&quot;UTF-8&quot;);
          MqttMessage message = new MqttMessage(encodedPayload);
          message.setId(320);
          message.setRetained(true);
          message.setQos(qos);
          client.publish(topic, message);
      }
  
      public void subscribe(@NonNull MqttAndroidClient client, @NonNull final String topic, int qos) throws MqttException {
          IMqttToken token = client.subscribe(topic, qos);
          token.setActionCallback(new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken iMqttToken) {
                  Log.d(TAG, &quot;Subscribe Successfully &quot; + topic);
              }
  
              @Override
              public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                  Log.e(TAG, &quot;Subscribe Failed &quot; + topic);
  
              }
          });
      }
  
      public void unSubscribe(@NonNull MqttAndroidClient client, @NonNull final String topic) throws MqttException {
  
          IMqttToken token = client.unsubscribe(topic);
  
          token.setActionCallback(new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken iMqttToken) {
                  Log.d(TAG, &quot;UnSubscribe Successfully &quot; + topic);
              }
  
              @Override
              public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                  Log.e(TAG, &quot;UnSubscribe Failed &quot; + topic);
              }
          });
      }
  
   */
  
  }
  </code></pre>
  <p>And I use these permissions on <code>AndroidMainfest</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  </code></pre>
  <p>I added this <code>&lt;service android:name=&quot;org.eclipse.paho.android.service.MqttService&quot;/&gt;</code> too.</p>
  <p>Also I'm using these implementations in my <strong>build.gradle(:app)</strong></p>
  <pre><code>implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
  implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1'
  implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
  </code></pre>
  <p>And here's my error log:</p>
  <pre class="lang-none prettyprint-override"><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.im_tierd_of_this_shit, PID: 28422
      java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/LocalBroadcastManager;
          at org.eclipse.paho.android.service.MqttAndroidClient.registerReceiver(MqttAndroidClient.java:450)
          at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:428)
          at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:334)
          at com.example.im_tierd_of_this_shit.MainActivity.getMqttClient(MainActivity.java:34)
          at com.example.im_tierd_of_this_shit.MainActivity.onCreate(MainActivity.java:29)
          at android.app.Activity.performCreate(Activity.java:8198)
          at android.app.Activity.performCreate(Activity.java:8182)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:246)
          at android.app.ActivityThread.main(ActivityThread.java:8512)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
       Caused by: java.lang.ClassNotFoundException: Didn't find class &quot;android.support.v4.content.LocalBroadcastManager&quot; on path: DexPathList[[dex file &quot;/data/data/com.example.im_tierd_of_this_shit/code_cache/.overlay/base.apk/classes3.dex&quot;, zip file &quot;/data/app/~~W6OhEC-2fwAmOWAaC45Xyw==/com.example.im_tierd_of_this_shit-ghc3dmh_JMIqEeq5cr5lWg==/base.apk&quot;],nativeLibraryDirectories=[/data/app/~~W6OhEC-2fwAmOWAaC45Xyw==/com.example.im_tierd_of_this_shit-ghc3dmh_JMIqEeq5cr5lWg==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
          at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
          at org.eclipse.paho.android.service.MqttAndroidClient.registerReceiver(MqttAndroidClient.java:450) 
          at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:428) 
          at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:334) 
          at com.example.im_tierd_of_this_shit.MainActivity.getMqttClient(MainActivity.java:34) 
          at com.example.im_tierd_of_this_shit.MainActivity.onCreate(MainActivity.java:29) 
          at android.app.Activity.performCreate(Activity.java:8198) 
          at android.app.Activity.performCreate(Activity.java:8182) 
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765) 
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968) 
          at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
          at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
          at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:246) 
          at android.app.ActivityThread.main(ActivityThread.java:8512) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
  W/System: A resource failed to call close. 
      A resource failed to call close. 
  </code></pre>