开发

Android Studio 基本信息

本指南介绍了 Android Studio IDE,并回顾了一些基本功能,包括如何构建和运行示例项目。
在使用本指南之前,请遵循设备设置Android 开发软件设置中的所有说明。移动 SDK\ovr_sdk_mobile\VrSamples 中包含本主题中引用的原生示例。

开始使用原生示例:导入 Gradle 项目

  1. 如果是第一次启动 Android Studio,请选择打开现有的 Android Studio 项目。如果之前启动过 Android Studio,请点击文件 > 打开
    Android Studio welcome screen with Open an existing Android Studio project option.
  2. 打开移动 SDK VrSamples 文件夹中的任意 build.gradle 项目文件。例如,VrSamples/VrCubeworld_Framework/Projects/Android/build.gradle
    File browser navigating to the VrCubeworld_Framework build.gradle file in VrSamples.
  3. 当询问您是否希望项目使用 Gradle 包装器时,点击确定
    Android Studio dialog prompting to use the Gradle wrapper with an OK button.

Gradle 同步错误疑难解答

如果 Android Studio 报告 Gradle 同步或配置错误,以下是一些可能的解决方案:
  • 造成这种错误的最常见原因是 Android SDK 或 NDK 位置错误。验证在文件 > 项目结构中正确指定了 SDK 和 NDK 位置。如果指定有误或缺失,必须填入正确的路径才能继续。
  • 在 MacOS 上,即使正确的路径已列出于项目结构对话框中,有时 Android Studio 也会报告缺失 SDK 位置的错误。要纠正这个问题,请从项目文件夹复制 local.properties 文件至 Oculus 移动 SDK 根文件夹。
Android Studio Project Structure dialog showing SDK and NDK location fields.

项目概览

Android Studio 默认在 Android 视图中显示项目文件。我们建议将它改为项目视图,它可以很好地显示整个目录结构,并用粗体突显导入的项目目录。
Android Studio project panel switched to the Project view showing directory structure.

选择目标配置、构建和运行

请确保您已遵循设备设置Android 开发软件设置的配置步骤,以确保您的设备和开发环境已正确配置。
在构建之前,通过在 Android Studio 工具栏的项目菜单中选择编辑配置来选择您想要构建的目标配置。
Android Studio toolbar with the Edit Configurations option in the configuration dropdown.
要构建并运行应用:
  1. 点击工具栏中的运行
    Android Studio toolbar with the Run button highlighted.
  2. 将显示选择部署目标对话框。它有时会默认设置为模拟器。
  3. 选择连接设备下面列出的设备。
    Select Deployment Target dialog showing a connected device under Connected Devices.
  4. 如果您的设备要求您允许 USB 调试,请点击确定
疑难解答:如果 USB 调试无法工作:
  1. 在您的手机上打开开发者选项
  2. 关闭 USB 调试然后重新开启。
疑难解答:如果调试时进入某些函数导致非法指令异常 (SIGILL)/崩溃:
  1. 在 Android Studio,前往运行 > 调试 > 编辑配置
  2. 选择调试器窗格。
  3. 选择 LLDB Post 附加命令窗格。
  4. 点击 + 添加一个新命令。
  5. 添加以下命令:process handle --pass true --stop false --notify true SIGILL

同步项目

如果您编辑一个 *.gradle 文件或安装更新到 Oculus 移动 SDK,其中包括更新后的 Gradle 项目,点击与 Gradle 文件同步项目来更新您的 Android Studio 项目文件。
Android Studio toolbar with the Sync Project with Gradle Files button highlighted.