运行App

运行 App

如果你按照上一课的内容创建了一个安卓项目,它包含了默认的设置项

如何运行你的App取决于两项:你是使用安卓真机或者Android Studio。下面来演示如何使用真机和模拟器来运行App。

运行在真机上


设置设备

  1. 使用USB数据线,连接手机和开发机。如果在Windowsz上做开发,你可能需要安装对应的USB驱动.安装驱动教程: OEM USB Drivers .
  2. 开启 USB debugging .
    • 在大部分安卓3.2及以前版本的机器上,开启步骤是: Settings > Applications > Development.
    • 安卓4.0及更新的版本,步骤是: Settings > Developer options.

      Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go toSettings > About phone and tap Build number seven times. Return to the previous screen to findDeveloper options.

从 Android Studio 运行应用

  1. 选择一个你的项目文件,然后点击工具栏上的 Run  按钮.
  2. 在 Choose Device 窗口, 选择 Choose a running device 按钮, 然后选择你的设计,点击 OK .

Android Studio 会在你的机器上安装App,然后启动它 .

Run the app from a command line

Open a command-line and navigate to the root of your project directory. Use Gradle to build your project in debug mode, invoke the assembleDebug build task using the Gradle wrapper script (gradlew assembleRelease).

This creates your debug .apk file inside the module build/ directory, named app-debug.apk.

On Windows platforms, type this command:

> gradlew.bat assembleDebug

On Mac OS and Linux platforms, type these commands:

$ chmod +x gradlew
$ ./gradlew assembleDebug

After you build the project, the output APK for the app module is located in app/build/outputs/apk/

Note: The first command (chmod) adds the execution permission to the Gradle wrapper script and is only necessary the first time you build this project from the command line.

Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable, then execute:

$ adb install app/build/outputs/apk/app-debug.apk

On your device, locate MyFirstApp and open it.

That's how you build and run your Android app on a device! To start developing, continue to the next lesson.

Run on the Emulator


Whether you're using Android Studio or the command line, to run your app on the emulator you need to first create an Android Virtual Device (AVD). An AVD is a device configuration for the Android emulator that allows you to model a specific device.

Create an AVD

  1. Launch the Android Virtual Device Manager:
    • In Android Studio, select Tools > Android > AVD Manager, or click the AVD Manager icon  in the toolbar. The AVD Manager screen appears.
    • Or, from the command line, change directories to sdk/ and execute:
      tools/android avd

      Note: The AVD Manager that appears when launched from the command line is different from the version in Android Studio, so the following instructions may not all apply.

  2. On the AVD Manager main screen, click Create Virtual Device.
  3. In the Select Hardware window, select a device configuration, such as Nexus 6, then click Next.
  4. Select the desired system version for the AVD and click Next.
  5. Verify the configuration settings, then click Finish.

For more information about using AVDs, see Managing AVDs with AVD Manager.

Run the app from Android Studio

  1. In Android Studio, select your project and click Run  from the toolbar.
  2. In the Choose Device window, click the Launch emulator radio button.
  3. From the Android virtual device pull-down menu, select the emulator you created, and click OK.

It can take a few minutes for the emulator to load itself. You may have to unlock the screen. When you do, My First App appears on the emulator screen.

Run your app from the command line

  1. Build the project from the command line. The output APK for the app module is located inapp/build/outputs/apk/.
  2. Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable.
  3. Execute this command:

    $ adb install app/build/outputs/apk/apk-debug.apk

  4. On the emulator, locate MyFirstApp and open it.

That's how you build and run your Android app on the emulator! To start developing, continue to the next lesson.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值