0

So, I am trying to build an app using Android Studio which connects to a HiveMQ Broker on a button click.

This is the project code blocs:

app-build.gradle
In this section I added the "repositories" and the four last lines in the "dependencies" section

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.MyProject"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
repositories {
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-releases/"
    }
}
dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
        exclude module: 'support-v4'
    }
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
    implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'

}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.MyProject">
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Myproject">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name="org.eclipse.paho.android.service.MqttService" >
        </service>
    </application>

</manifest>

MainActivity.java

package com.example.MyProject;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;

public class MainActivity extends AppCompatActivity {

    private Button btn1;
    MqttAndroidClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1= (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View view){
            String clientId = MqttClient.generateClientId();
            client = new MqttAndroidClient(getApplicationContext(), "tcp://www.domain.in/ip:11883",clientId);
            try {
                client.connect().setActionCallback(new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        // We are connected
                    }
                    @Override
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    }

            });
            } catch (MqttException e) {
                e.printStackTrace();
            }


        }});
    }}

But when I click the button , I get this error on the "Logcat" console. I've tried different solutions which I found on the net but the error doesn't seem to go. Any help?

2021-06-14 22:46:59.922 9907-9907/com.example.MyProject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.MyProject, PID: 9907
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/LocalBroadcastManager;
        at org.eclipse.paho.android.service.MqttAndroidClient.registerReceiver(MqttAndroidClient.java:445)
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:423)
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:354)
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:309)
        at com.example.MyProject.MainActivity$1.onClick(MainActivity.java:32)
        at android.view.View.performClick(View.java:6597)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.LocalBroadcastManager" on path: DexPathList[[zip file "/data/app/com.example.MyProject-bfP2jouZbWKT4cZm7bwerA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyProject-bfP2jouZbWKT4cZm7bwerA==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        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:445) 
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:423) 
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:354) 
        at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:309) 
        at com.example.MyProject.MainActivity$1.onClick(MainActivity.java:32) 
        at android.view.View.performClick(View.java:6597) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:6574) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25885) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
user29
  • 9
  • 4
  • `org.eclipse.paho:org.eclipse.paho.android.service:1.0.2` still uses the old `support-v4` library for a copy of the deprecated `LocalBroadcastManager`. You could see if version `1.1.1` fixes that, but since it was released in 2017, I am not hopeful. – CommonsWare Jun 14 '21 at 22:38
  • I have tried the `1.1.1` version, still same error. – user29 Jun 14 '21 at 23:51
  • Then the best thing would be to find an MQTT client library that is being actively maintained. – CommonsWare Jun 15 '21 at 10:46
  • do you have any refrence? – user29 Jun 15 '21 at 14:52

0 Answers0