【问题标题】:PeripheralManagerService throws NoClassDefFoundErrorPeripheralManagerService 抛出 NoClassDefFoundError
【发布时间】:2023-04-08 10:44:01
【问题描述】:

我的应用中有以下代码可以访问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);
}

更新到最新的 Android Things(开发者预览版 7)后,我的应用现在正在抛出 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[...]

这段代码之前可以运行,为什么更新后开始出现这种情况?

【问题讨论】:

  • 这很有趣.... 使用 SO 作为您的发行说明... 或无论如何作为补充。有道理,这是人们首先看到的地方。

标签: android-things


【解决方案1】:

从 Preview 7 开始,Android Things API 服务不再是新构建的 实例。相反,它们通过getInstance() 作为单例访问 更符合 Android API 范式。一些类,如 PeripheralManagerService 也被重命名了。

确保更新您的应用以使用 Preview 7 SDK:

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

然后修改您的代码以访问PeripheralManager

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);
}

查看 Android Things API reference 验证您调用的任何其他 API 是否已更改。

【讨论】:

【解决方案2】:

而不是使用PeripheralManagerService service = new PeripheralManagerService();使用

PeripheralManager peripheralManager=PeripheralManager.getInstance();

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,但上述解决方案对我不起作用。我试图通过选择 new->project 并选择 Android Things 来创建一个字母数字显示项目。生成的 gradle 构建文件如下所示。请注意,ht16k33 的驱动程序版本为 0.3。这导致了问题。一旦我将其更改为 1.0,ClassNotFound 问题就消失了。

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.google.android.things.contrib:driver-ht16k33:0.3'
        implementation 'com.android.support:support-v4:28.0.0-beta01'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        compileOnly 'com.google.android.things:androidthings:+'
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-31
      • 2010-12-19
      • 1970-01-01
      • 2016-01-08
      • 2012-10-06
      • 2011-08-27
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多