Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PeripheralManagerService throws NoClassDefFoundError

I have the following code in my app to access PeripheralManagerService:

PeripheralManagerService service = new PeripheralManagerService();
Gpio ledGpio;
try {
    ledGpio = service.openGpio("BCM6");
    ledGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
    Log.e(TAG, "Error configuring GPIO pins", e);
}

After updating to the latest Android Things (Developer Preview 7), my app is now throwing a NoClassDefFoundError:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/things/pio/PeripheralManagerService;
...
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.things.pio.PeripheralManagerService" on path: DexPathList[...]

This code was working before, why has this started happening after the update?

like image 653
devunwired Avatar asked Mar 06 '18 23:03

devunwired


People also ask

Why does the classloader throw NoClassDefFoundError?

While loading a class, if the Classloader cannot find the class's definition, it throws the NoClassDefFoundError. There are a couple of reasons for which Java cannot find the class's definition, which are: Missing a few dependent jars which is the most common reason. All jars are added as dependencies but in the wrong path.

What is Java NoClassDefFoundError?

What Is java.lang.NoClassDefFoundError? When the Java Runtime runs a Java program, it does not load all the classes and dependencies at once. Instead, it calls upon the Java Classloader to load classes in memory as-and-when-required. While loading a class, if the Classloader cannot find the class's definition, it throws the NoClassDefFoundError.

How do I fix NoClassDefFoundError in Eclipse?

java.lang.NoClassDefFoundError in eclipse usually occurs that your project is missing some of the required jars in your build path. First, configure your project build path. It can configure by right click on the project–>properties–>java build path–> select libraries for jars or classes in folder select source.

Why does Junit4 throw NoClassDefFoundError when using Hamcrest-core jar?

However, the Junit4 has an internal dependency on the hamcrest-core jar. If we miss adding the hamcrest-core jar as a dependency in our classpath, Java throws the NoClassDefFoundError. The classpath is as follows: One other scenario is when we added both the jars, but the versions don't match.


2 Answers

Starting in Preview 7, Android Things API services are not constructed as new instances. They are instead accessed as singletons via getInstance() to be more in line with Android API paradigms. Some of the classes, such as PeripheralManagerService were also renamed.

Be sure to update your app to use the Preview 7 SDK:

dependencies {
    compileOnly 'com.google.android.things:androidthings:0.7-devpreview'
}

Then modify your code to access the PeripheralManager instead:

PeripheralManager manager = PeripheralManager.getInstance();
Gpio ledGpio;
try {
    ledGpio = manager.openGpio("BCM6");
    ledGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
    Log.e(TAG, "Error configuring GPIO pins", e);
}

Review the Android Things API reference to verify if any of the other APIs you are calling have changed.

like image 193
devunwired Avatar answered Oct 18 '22 17:10

devunwired


Rather than using PeripheralManagerService service = new PeripheralManagerService(); use

PeripheralManager peripheralManager=PeripheralManager.getInstance();
like image 41
Koustuv Ganguly Avatar answered Oct 18 '22 17:10

Koustuv Ganguly