Build and run your app in the official Android studio document

Source: Internet
Author: User

Build and run your app in the official Android studio document

This article is translated by MTJH and Jkyishon for revision.

Objective

By default, Android studio sets up new projects and deploys them to emulators or real-world devices with just a few clicks. With instant running, you don't need to build a new apk to apply the changed approach and existing application resources to a running application, so the code changes are instantly visible.

Click Run to build and run your app. Android Studio builds your app with Gradle, selects a deployed device (emulator or connected device), and then deploys your app. You can also customize these default settings, for example you can choose to automatically deploy to a target device by changing the run configuration.

If you want to run your app using the emulator (use the Android Emulator), you need to have an Android virtual device (AVD) ready. If you have not created an emulator yet, click Run and click Create New Emulatorin the Select Deployment Target dialog box. Follow the virtual Device Configuration Wizard to define the emulator you want to create. For more details, please see Create and edit virtual devices.

If you are using Android real computer, you need to turn on USB debug mode on your device. For more details, see Using hardware devices.

Note: You can also deploy your app via debug mode by clicking Debug. In debug mode you can set breakpoints in your code, check the values of variables and expressions at run time, and run debugging tools. For more details, see Debugging with Android Studio.

Select and build a module (select and build a different module)

If you have multiple modules in your project other than the default application module, you can follow the steps below to build a specific module:

    • Select the module in the Project panel and click Build > Make module Module-name.

Android Studio builds modules with Gradle. Once the module is built, if you build a module for a new app or new device, you can run it or debug it, or if you build a library or Google Cloud module, you can use it as a dependency.

Run a module of the app:

    • Click Run>run , and then select the module in the Run dialog box.
Changing the Run/debug configuration (change the Run/debug config)

The Run/debug configuration specifies the modules to be run, package deployment, launch page, target device, emulator settings, Logcat options, and so on. The default Run/debug configuration launches the project default activity and uses the Select Deployment target dialog box to select the deployment target. If the default settings are not appropriate for your project or module, you can customize the Run.debug configuration, or you can create a new configuration at the project level, the default level, or the module level. Edit the Run/debug configuration and select Run > Edit Configurations. For more details, refer to create and Edit run/debug configurations.

Changing the build version (change the build variant)

By default, when you click Run, Android Studio builds the debug version of the app, which is only for testing, and you need to build the release version to be released (prepare your app for public release).

Android Studio Change build, click Build > Select Build Variant, then select another option in the drop-down list. By default, a debug and build version is generated.

Depending on product flavors, you can create additional builds for your different versions of your app, each with different features or device requirements. For build variants and product flavors, see Configure your build.

Monitor the build process (monitor the build procedure)

Click Gradle Console to see the details of your build progress. The Control Panel shows each task that Gradle performs when building your app, as shown in:

If your build uses product features, it invokes tasks to build these product features. Click the Gradle of the IDE's right window to see a list of all the available build tasks.

If an error occurs during the build process, theMessages window is displayed to describe the problem. Gradle may recommend several command-line options to help your sister with this problem, such as –stacktrace or –debug. Use command-line options to help you build your process.

1. Open the Settings or Preferences dialog box:

    • On Windows or Linux, select File > Settings on the menu bar.

    • On Mac OSX, on the menu bar, choose Android Studio > Preferences .

2. Navigate to build,execution,deployment > Compiler.

3. In the text input box next to command-line options, enter your command-line options.

4. Click OK to save and exit.

Gradle will try these commands the next time you try to build your app.

Generate APKs (Generate APKs)

When you click Run , Android Studio generates a debug APK and deploys it to your target device. Before you generate a release version of the app, however, you first have to learn how to sign your app (your app). You can then build multiple signed debug or Release build versions of APKs. Locate the generated APK file and click on the link in the popup dialog box as shown in:

Click the link to find the generated apk file.

About Instant run (about Instant run)

As mentioned in the Android Studio 2.0 release, Instant Run is a behavior of the run and Debug commands, significantly reducing the time it takes to update your app. Although the first build may take more time to complete, Instant Run does not need to build a new apk to push the subsequent updates to your app, so the display is faster.

Instant Run support Only you build debug version and use Android Plugin Gradle2.0 version or higher, and set in your app module-level Build.gradle file Minsdeversion is 15 or higher. For better performance, set minsdeversion to 21 or higher.

After deploying the app, a very small, yellow lightning icon appears in the Run button (or the Debug button), indicating that Instant run is ready to push the update the next time you click the button. It only pushes those new changes instead of building a new apk, and in some cases the app doesn't need to be restarted but can instantly show the results of the code update.

Instant run pushes the updated code and resource file to a connected device or emulator by running hot swap,warm swap,or Cold swap , which automatically determines the type of swap based on the type of change you make. The video above provides an interesting detail on how all this works. However, when you push a definite code change to a target device, a quick summary of the performance of the instant run is shown in the following table:

Code Changes Instant Run Condition
Change the implementation code of an existing method Hot Swap supported: This is the fastest swap type to show code changes. Your app keeps running, and a new method is executed when the next method is called. Hot swaps does not initialize objects in the app you're running. Maybe you need to restart your current activity or restart the app (restart the app) before you see a certain update. By default, after running hot swap, Android Studio automatically restarts the current Activity. If you don't need to run this way, you can disable the automatic restart activity (disable automatic activity restarts).
Changing or removing a resource file that already exists Support Warm swap : This swap is still very fast, but instant run pushes the changed resources to your app, must restart the current activity, your app is still running, But when the activity restarts the screen will have a slight flicker, which is normal.
Code structure changes such as: Add, remove, or change: Comment, instance, static variable, static method signature, instance method signature change the parent class of the current class inheritance change inherited interface list change the static initialization method of class using dynamic resource ID re-layout Support for Cold swap (API 21 or higher): This swap is a bit slow because although a new apk is not needed, it has to restart the entire app when the code structure of the Instant Run push changes. When you run a target device API of 20 or lower, Android Studio deploys the entire apk.
Change the App's manifest file changes when it comes to manifest resource files when changing the Android Widget UI file (requires clean and rerun clean and rerun Android Studio automatically deploys a new build to apply these changes when changing the app's manifest file or a resource file that involves manifest. This is because when the APK is installed on the device, some information about the app, such as the name of the app, the app's icon file, and the intent filter, are determined in the manifest file. If your build process automatically updates any part of the app manifest, such as automatic iteration Versioncode or Versionname, you won't be able to benefit from the full presentation of instant run. When you use instant Run while you are in debug build, you should disable automatic updating of any part of the manifest file. When updating the Android Widget UI element, you need to run clean and rerun to see the changes. Also, because performing a cleanup rebuild may take longer when Instant run, you can disable Instant run (disable Instant run) When you update your layout UI.

Note: When your app crashes, you need to reboot and don't start it from your target device. When your target device restarts your app, do not apply any changes to the code since the last cold swap or incremental build (incremental build). Keep the latest changes to launch your app and click Run (or Debug ) on Android Studio.

Using rerun (use rerun)

When we push the updated code to affect the specific initialization, such as when the OnCreate () method changes, you need to restart the app to make the change take effect, click Rerun to build incrementally and restart the app.

If you need to build a clean build, click Run >clean and rerun on the menu bar, or Shift-click rerun. This will stop the running app, perform a complete, clean build, and then deploy the new apk to your target device.

Disable Activity Auto Restart (Disable automatic activity restart)

When the hot swap is performed, your app stays running but Android studio automatically restarts the current activity, which is disabled by following these steps:

1. Open the Settings or Preference dialog box:

    • On Windows or Linux, select File > Settings on the menu bar.

    • On Mac OSX, on the main menu, choose Android Studio > Preferences

2. Expand build,execution,deployment > Instant Run.

3. Uncheck Restart activity on code changes

If the activity Auto Restart is disabled, you can manually select the Run > Restart activity in the menu bar to restart the current activity.

Optimize your project for Instant run configuration (Configure and optimize your project for Instant run)

Android Studio builds a project when using Android Plugin Gradle2.0 or higher when instant Run is enabled by default.

Update the existing project plug-in to the latest version:

1. Open the Settings or Preference dialog box:

2. Expand build,execution,deployment > Instant Run and click Update Projectas shown in. If this option is not displayed, the Gradle plugin for this project has been updated to the latest version.

Update the Gradle plugin for an existing project.

At the same time you need to change the build version to the debug version (changing the build variant) to use instant Run.

Change the DEX resource to increase build speed (Improve build times by configuring DEX resources)

When you deploy a clean post build, Android Studio urges your app to allow instant run to update code and resources. Although updating the app is faster, it may take longer to build the first time. You can increase the speed of the dog by configuring some dexoptions settings:

Maxprocesscount

设置DEX允许同时运行的进程的最大数量,如果Gradle进程正在运行,你需要在初始化一个新的maximun进程数量之前停止正在运行的程序,你可以在终端窗口调用如下命令来终止Gradle进程:在Windows上输入     gradlew --stop在Linux/Mac OSC上输入       ./gradlew --stop

Javamaxheapsize

设置DEX运行时内存最大值,当改变一个值时,你可以添加“k”表示字节,“m”表示兆字节,或者“g”表示千兆字节。

The following example sets Maxprocesscount to 4 in a module-level build.gradle file and sets Javamaxheapsize to "2g":

    ...  dexOptions {    4 // this is the default value    "2g"  }}

You should try these settings, increment these values, and watch for these effects on the build time. If you assign too many resources to Dex, it can lead to longer build times.

Enable Dexing-in-process and Java Incremental compilation (Enable Dexing-in-process and incremental Java compilation)

Android Plugin for Gradle version 2.1.0 or later provides additional build process improvements, including Java Incremental compilation and dexing-in-process, with Java incremental compilation enabled by default, The compilation efficiency is improved by recompiling only the part of the resource change or the part that needs to be compiled during development.

Dexing-in-process executes the Dex build process, rather than a separate outsourced VM process. This not only makes incremental builds faster, but it also has significant acceleration for overall builds. To enable this feature, you need to set the Gradle daemon maximun size at least 2048MB. You can change the Gradle.properties file for your project by following these steps:

org.gradle.jvmargs = -Xmx2048m

If you define a value for Javamaxheapsize in the module-level Build.gradle file, you need to set the maximum daemon value to Javamaxheapsize + 1024x768. For example, how you set the javamaxheapsize size to "2g", you need to set the following in the project's Gradle.properties file:

org.gradle.jvmargs = -Xmx3072m  
Removing your project from Windows Defender (Exclude your project from Windows Defender)

In Windows systems, Windows Defender may cause slower use of instant run. If you are using Windows Defender, you should make Windows Defender no longer scanning Android Studio project folders exclude your Android Studio Projects folder from Windows Defender malware scans.

Increase build speed when using Crashlytics (Improve build times using Crashlytics)

If your fabric Gradle plug-in version is below 1.21.6,crashlytics, the build time may be longer. To improve the build performance of your app, you can update your plugin to the latest version (update the plugin to the latest version) or disable Crashlytics in the Debug version (disable Crashlytics for Your debug build variant).

Limitations of Instant run (limitations of Instant Run)

In most cases, the Instant run was developed to speed up the build and deployment. However, there are some aspects that may affect their behavior and compatibility with the program when using instant run. If you have any problems with instant run, please submit this bug (file a bug).

Deploy to multiple devices (deploy to multiple devices)

Instant run uses different technologies to perform hot, warm, and cold switching on target devices at different API levels. To do this, Android studio temporarily shuts down instant Run when deploying one application to multiple devices.

App's Multidex (Multidex your app)

When your project is configured for Legacy Multidex, in other words, When the Build.gradle configuration multidexenabled is true and minsdkversion is 20 or lower and the device is deployed to the devices that are running Android4.4 (API level 20) or lower, Android Studio disables instant Run.

If Minsdkversion is set to 21 or higher, Instant run automatically configures the Multidex of your program because Instant run only works in the debug version, When deploying release version you may need to configure your program's Multidex (Configure your app for Multidex).

Run Test and performance analysis (run instrumented tests and performance profilers)

The instrumentation test deploys the debug apk and test apk to a test device in a single process, allowing the control method to cover the normal app life cycle and perform the test. However, when running or debugging the test, Android Studio does not inject the additional methods required by instant run and turns the feature off.

When analyzing an app, you should disable instant run, which has some impact on performance when using instant run, with a slightly larger impact when using the hot Swap rewrite method. The impact of this performance may interfere with the information provided by the Profiling Tools. In addition, the stub method generated by each hot swap can complicate the stack trace.

Using third-party plug-ins (use Third-party plugins)

When using instant Run, Android studio temporarily disables the Java Code Coverage Library (Jacoco) and Proguard because instant run only runs in the debug version, which does not affect your release.

Some third-party plug-ins that perform bytecode enhancement may cause your application to run with instant running problems. If you encounter these problems, but still want to use instant Run, you should disable these plugins in the Debu version. You can also help improve compatibility with third-party plug-ins by submitting a bug (filing a bug).

Push changes to multi-process apps (push changes to multi-process apps)

Instant run only through the main process of your app in order to perform hot swap and warm swap, when the push changes to other processes in the app, such as the way the change method is implemented, or the existing resource, Instant run executes cold swap.

Disable Instant run (Disable Instant run)

Disable Instant Run:

1. Open the Settings or Preferences dialog box.
2. Expand build,execution,deployment > Instant Run
3. Uncheck the Enable Instant Run selection box

Build and run your app in the official Android studio document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.