一、编译&安装Wear Apk
1.1、源码下载
首先可以到Google官网Demo下载源码,本地编译几个WearApk,这里我选用AlwaysOn这个项目,就是展示时间等信息,比较简单。界面样式大概就这样:
1.2、项目结构
可以看到,跟普通的Android项目并没有什么不同,区别就是引入的包:
1.3、编译Apk
跟普通项目一样,通过Gradle编译即可。
1.4、安装
安装方式也是一样,直接通过adb install -r即可。
二、运行效果
经测试,在手机上,应用打开就会直接崩溃。Log如下:
04-27 14:45:35.942 E/AndroidRuntime(10278): Process: com.example.android.wearable.wear.alwayson, PID: 10278
04-27 14:45:35.942 E/AndroidRuntime(10278): 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
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.wear.ambient.SharedLibraryVersion.verifySharedLibraryPresent(SharedLibraryVersion.java:57)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.wear.ambient.WearableControllerProvider.getWearableController(WearableControllerProvider.java:48)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.wear.ambient.AmbientDelegate.onCreate(AmbientDelegate.java:103)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.wear.ambient.AmbientModeSupport.onCreate(AmbientModeSupport.java:198)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
04-27 14:45:35.942 E/AndroidRuntime(10278): at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.Activity.performStart(Activity.java:7864)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3366)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2077)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.os.Handler.dispatchMessage(Handler.java:107)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.os.Looper.loop(Looper.java:244)
04-27 14:45:35.942 E/AndroidRuntime(10278): at android.app.ActivityThread.main(ActivityThread.java:7539)
04-27 14:45:35.942 E/AndroidRuntime(10278): at java.lang.reflect.Method.invoke(Native Method)
04-27 14:45:35.942 E/AndroidRuntime(10278): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
04-27 14:45:35.942 E/AndroidRuntime(10278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1020)
三、原因分析
可以看到报错点主要是在SharedLibraryVersion.verifySharedLibraryPresent函数中,我们通过反编译看一下其中究竟做了什么:
public WearableActivityController getWearableController(Activity activity, final AmbientDelegate.AmbientCallback callback) {
SharedLibraryVersion.verifySharedLibraryPresent();
WearableActivityController.AmbientCallback callbackBridge = new WearableActivityController.AmbientCallback() {
public void onEnterAmbient(Bundle ambientDetails) {
callback.onEnterAmbient(ambientDetails);
}
public void onUpdateAmbient() {
callback.onUpdateAmbient();
}
public void onExitAmbient() {
callback.onExitAmbient();
}
public void onInvalidateAmbientOffload() {
callback.onAmbientOffloadInvalidated();
}
};
verifyAmbientCallbacksPresent();
return new WearableActivityController(TAG, activity, callbackBridge);
}
从函数名上,我们可以猜测,该函数是校验共享库是否存在并且可运行状态的。接下来我们来到:
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");
}
}
可以看到代码中抛出的IllegalStateException正是log中打印的
"Could not find wearable shared library classes. Please add <uses-library android:name="com.google.android.wearable" android:required="false" /> to the application manifest"
那是不是像Log中提示的一样,将这段 uses-library添加到AndroidManifest.xml中就可以运行了呢。我们来看下项目的配置文件:
原来这段代码已经添加,是为了让App跑在android-22之前而声明的,跟现在的报错并没有关系。
我们接着往下看吧,看看PresenceHolder.PRESENT是什么情况下会返回false。
看到这里,真相大白了!这是通过类加载的方式,检查系统预加载库中是否有包括类WearableActivityController,如果没有的话,证明系统中并没有内置wear相关的运行环境库,直接退出就可以了。
四、结论
手机跟手表必须内置Google Wear运行框架,Wear App才可以调用相关接口正常运行。此处可以参考小米手表,其内置了Google Play服务框架,以此来支持Android Wear的开发。这个也是小米手表跟OPPO Watch最大的区别。
而如何开发一个可以在国内正常使用,并且与手机正常通讯的Wear App,可以参考打造中国版 Wear OS 应用