ParticleCloudSDK.init(this); crashes app

I am a hobbyist. I am trying to learn how to build an android app to replace the current local HTML page for controlling my projects. I have been using https://docs.particle.io/reference/SDKs/android/#android-cloud-sdk and Using the Particle Android SDK by ToglefritzJun 23, 2017 as my guide. Just for learning, I have built a single button Android layout that calls a method which I will use to practice with the various particle functions. However, I can’t seem to get past the ~~~ ParticleCloudSDK.init(this); ~~~. If I comment out this line it loads my layout fine but obviously the called functions tied to the button click don’t work. Here is my MainActivity.java code:

package com.example.particlepractice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.cloud.exceptions.ParticleCloudException;
import io.particle.android.sdk.utils.Async;
import io.particle.android.sdk.utils.Toaster;

public class MainActivity extends AppCompatActivity {


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       ParticleCloudSDK.init(this);
       setContentView(R.layout.activity_main);



       Button btnLogin = (Button) findViewById(R.id.btnLogin);


       //When clicking on this button, Will login in to particle
       btnLogin.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               requestData();
           }
       });


   }
       private void requestData() {
           try {
               ParticleCloudSDK.getCloud().logIn("mylogin", "mypassword");
           } catch (ParticleCloudException e) {
               e.printStackTrace();
           }
           Context context = getApplicationContext();
          Toast toast = Toast.makeText(context, "Logged In", Toast.LENGTH_LONG);
           toast.show();
       }
   }

Here is the LogCat that I get:

2020-12-01 15:01:47.653 27839-27839/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-12-01 15:01:47.654 27839-27839/? E/Zygote: accessInfo : 1
2020-12-01 15:01:47.660 27839-27839/? I/articlepractic: Late-enabling -Xcheck:jni
2020-12-01 15:01:47.730 27839-27839/com.example.particlepractice I/articlepractic: report jit thread pid = 27862
2020-12-01 15:01:48.388 27839-27839/com.example.particlepractice W/articlepractic: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-12-01 15:01:48.388 27839-27839/com.example.particlepractice W/articlepractic: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-12-01 15:01:48.388 27839-27839/com.example.particlepractice W/articlepractic: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-12-01 15:01:48.388 27839-27839/com.example.particlepractice W/articlepractic: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-12-01 15:01:48.388 27839-27839/com.example.particlepractice W/articlepractic: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-12-01 15:01:48.495 27839-27839/com.example.particlepractice D/AndroidRuntime: Shutting down VM
2020-12-01 15:01:48.499 27839-27839/com.example.particlepractice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.particlepractice, PID: 27839
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/localbroadcastmanager/content/LocalBroadcastManager;
        at io.particle.android.sdk.cloud.SDKProvider.buildCloud(SDKProvider.java:74)
        at io.particle.android.sdk.cloud.SDKProvider.<init>(SDKProvider.java:51)
        at io.particle.android.sdk.cloud.ParticleCloudSDK.initWithParams(ParticleCloudSDK.java:47)
        at io.particle.android.sdk.cloud.ParticleCloudSDK.init(ParticleCloudSDK.java:24)
        at com.example.particlepractice.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:7335)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.localbroadcastmanager.content.LocalBroadcastManager" on path: DexPathList[[zip file "/data/app/com.example.particlepractice-hR8IaBvB50-B9uN0TZG77w==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.particlepractice-hR8IaBvB50-B9uN0TZG77w==/lib/arm, /system/lib, /vendor/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 io.particle.android.sdk.cloud.SDKProvider.buildCloud(SDKProvider.java:74) 
        at io.particle.android.sdk.cloud.SDKProvider.<init>(SDKProvider.java:51) 
        at io.particle.android.sdk.cloud.ParticleCloudSDK.initWithParams(ParticleCloudSDK.java:47) 
        at io.particle.android.sdk.cloud.ParticleCloudSDK.init(ParticleCloudSDK.java:24) 
        at com.example.particlepractice.MainActivity.onCreate(MainActivity.java:23) 
        at android.app.Activity.performCreate(Activity.java:7335) 
        at android.app.Activity.performCreate(Activity.java:7326) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7156) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

I added the Internet permissions to the manifest as directed and implemented the dependency to the build.gradle file.

Any guidance would be appreciated.

Well, I didn’t get any love with my post but I was able to figure it out. Add:

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

to the build.gradle file

2 Likes