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?
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.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.
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.
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.
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.
Rather than using PeripheralManagerService service = new PeripheralManagerService();
use
PeripheralManager peripheralManager=PeripheralManager.getInstance();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With