Android 部署到设备时出现 Wear 库问题(在模拟器中正常)

Android Wear library problems with deploying to device (OK in emulator)

我正在尝试 运行 在(物理)可穿戴 OS 设备上的独立可穿戴应用程序,运行ning Oreo 8.1。该应用程序成功构建并且 运行 在 Android Wear 方形模拟器 (API 26) 上运行正常,但是当我尝试 运行 物理设备上的应用程序时,我收到了以下错误:

Caused by: java.lang.IllegalStateException: Could not find wearable shared library classes. 
Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to 
the application manifest

如错误代码所示,行

<meta-data. android:name="com.google.android.wearable". android:value="false" />

已添加到应用程序标签内的应用程序清单文件 (AndroidManifest.xml) 中。下面显示的是清单文件中应用程序标签的内容。

<application
android:allowBackup="false"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault">

<meta-data
    android:name="com.google.android.wearable.standalone"
    android:value="true" />

<uses-library android:name="com.google.android.wearable" android:required="false" />

<activity android:name="com.example.android.wearable.wear.alwayson.MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleInstance"
    android:configChanges="orientation|keyboardHidden"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

即使将建议的行添加到 android 清单文件,我仍然收到完全相同的错误代码。

我怀疑设备上的库版本与我们的开发环境使用的版本不同,但是我不知道如何查找和检查设备库版本。

任何人都可以提出解决此问题的方法吗?

构建信息:

compileSdkVersion 28

buildToolsVersion "28.0.3"

defaultConfig {
  versionCode 1
  versionName "1.0"

  minSdkVersion 24

  targetSdkVersion 27

  multiDexEnabled true
}

依赖关系:

dependencies {

  implementation 'androidx.wear:wear:1.0.0'
  implementation 'androidx.legacy:legacy-support-v13:1.0.0'

  implementation 'com.google.android.gms:play-services-wearable:17.0.0'

  compileOnly 'com.google.android.wearable:wearable:2.1.0'

  implementation 'com.google.android.support:wearable:2.1.0'
 }

评论里有人建议我把compileOnly改成implementation。这是收到的错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.wearable.wear.alwayson, PID: 2593
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.wearable.wear.alwayson/com.example.android.wearable.wear.alwayson.MainActivity}: java.lang.RuntimeException: Stub!
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6501)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.RuntimeException: Stub!
        at com.google.android.wearable.compat.WearableActivityController$AmbientCallback.<init>(WearableActivityController.java:6)
        at androidx.wear.ambient.WearableControllerProvider.<init>(WearableControllerProvider.java:52)
        at androidx.wear.ambient.WearableControllerProvider.getWearableController(WearableControllerProvider.java:51)
        at androidx.wear.ambient.AmbientDelegate.onCreate(AmbientDelegate.java:103)
        at androidx.wear.ambient.AmbientModeSupport.onCreate(AmbientModeSupport.java:198)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
        at android.app.Activity.performStart(Activity.java:7019)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6501) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

关于 Android Wear 的设置和开发的在线信息似乎不多,因此,如果能提供有用资源的任何指示,我们将不胜感激。

-------- 根据新测试于 4 月 6 日更新

原来post我只显示了清单文件的应用标签。在应用程序标签之外确实包含以下行:

<uses-feature android:name="android.hardware.type.watch" />

使用过时的库仅仅是因为使用了设备源代码包中提供的(过时的)样本。最初我遵循了 google 提供的教程(与您提供的 link 使用 wearable:2.5.0 相同)但没有成功 – returns 与原始 post。

在我 posted on SO 之前,我确实尝试了包含在您提供的位置的多个示例应用程序,以及开发工具包源包中提供的示例——所有结果都导致相同的错误。

这是最初 posted 的完整错误代码。当设备尝试启动应用程序时会发生这种情况。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.wearable.speaker, PID: 3230
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.wearable.speaker/com.example.android.wearable.speaker.MainActivity}: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6501)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest
        at androidx.wear.ambient.SharedLibraryVersion.verifySharedLibraryPresent(SharedLibraryVersion.java:57)
        at androidx.wear.ambient.WearableControllerProvider.getWearableController(WearableControllerProvider.java:48)
        at androidx.wear.ambient.AmbientDelegate.onCreate(AmbientDelegate.java:103)
        at androidx.wear.ambient.AmbientModeSupport.onCreate(AmbientModeSupport.java:198)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        at com.example.android.wearable.speaker.MainActivity.onStart(MainActivity.java:254)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1335)
        at android.app.Activity.performStart(Activity.java:7019)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6501) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

我检查了抛出异常的代码,显然这是由于缺少它试图验证其存在的共享库的前缀持有者。下面是文件“SharedLibraryVersion.java”中的函数。

/**
 * Throws {@link IllegalStateException} if the Wearable Shared Library is not present and API
 * level is at least LMP MR1.
 * <p>
 * <p>This validates that the developer hasn't forgotten to include a {@code <uses-library>} tag
 * in their manifest. The method should be used in combination with API level checks for
 * features added before {@link #version() version} 1.
 */
public static void verifySharedLibraryPresent() {
    if (!PresenceHolder.PRESENT) {
        throw new IllegalStateException("Could not find wearable shared library classes. "
                + "Please add <uses-library android:name=\"com.google.android.wearable\" "
                + "android:required=\"false\" /> to the application manifest");


由此看来,可穿戴库似乎不在设备上,或者是但不能find/link?

根据 Martin 的评论,建议如果来自 Playstore 的可穿戴应用程序可以 运行 那么该库应该存在于设备上,我尝试安装一些可穿戴应用程序。开发工具包上的 sdk verison 没有 play 商店,所以我尝试通过 adb 侧载一些我在网上找到的东西。

当 运行nig “adb install appname.apk” none 应用程序实际出现时,adb 返回成功,但 APKPure 除外,它本身就是一个应用程序侧载程序 运行s 在设备上。尝试通过 APKPure 在开发套件上安装可穿戴应用程序也没有成功,因为我收到“install/uninstall Wear 不支持的操作”。这似乎是由于 PackageManger 阻止了任意 APK 的安装——看起来像死胡同。

Create a Wear OS app 提示,您错过了一个不属于 application 节点的节点:

<uses-feature android:name="android.hardware.type.watch" />

此外,您使用的是过时的库:

dependencies {
    implementation 'androidx.wear:wear:1.0.0'
    implementation 'com.google.android.gms:play-services-wearable:17.0.0'
    implementation 'com.google.android.support:wearable:2.5.0'
    compileOnly 'com.google.android.wearable:wearable:2.5.0'
}

根据其他值,应该是:

targetSdkVersion 28

如果它不起作用,请尝试 运行 任何 wear-os-samples 进行比较。

如果 Play 商店中的穿戴式应用能够找到它,则可以假设它在设备上可用。


如果您使用 Open-Q™ 2500 Development Kit, you might have to flash another firmware. The issue tracker is for the component itself, which most likely does not apply here; better contact Intrinsyc Technologies Corporation support 部门。