Android Studio get started to master

Source: Internet
Author: User
Tags version control system jcenter

As introduction

After 2 years of research and development, Google has finally officially released an integrated development environment for Android developers, Android Studio 1.2 (stable version). Android Studio is a Google-developed IDE for Android developers, supporting Windows, Macs, Linux and other operating systems, based on the popular Java language integrated development environment IntelliJ built. The IDE first appeared at the May 2013 Google I/O developer conference, when the beta version was a variety of puzzling bugs, but the release of December 8, 14 was a stable version. With the launch of Android Studio 1.0, Google will gradually abandon its support for the original main eclipse ADT and provide an engineering migration solution for Eclipse users. But I believe that as a developer you get started as 1.0 after you do not want to use the original bitter Eclipse+adt, you will be as the various powerful attracted.

Download installation

To download as before the first, as the installation package is included in the SDK version and does not include the SDK version of the download, if you have the SDK, you can download the SDK version is not included, but download the SDK version is also OK, the installation of the custom SDK can also be installed after the installation of the SDK path can also be specified, Anyway, look at your hobbies. First spit trough The mighty of the celestial, have to admire celestial wall. If you have a ladder please go to Android developer to download the latest version of the as installation package, if you do not have a ladder, there is a way to go to the Android Studio Chinese Community official website Download your platform needs to install the package.

Download down the installation of the process can be ignored, can be installed is the program Ape Bar, so install this does not say, note that the installation is correctly configured JDK.

Installed after the first run as may have been parked in the fetching Android SDK component information. The following interface:

This is because the wall of celestial is really too high and too thick to put the first run Update SDK to the wall. The workaround is to close the Setup wizard, and if you cannot close the process can be manually shut down in Task Manager (Ctrl+alt+del start Task Manager), Then open the Idea.properties file in the bin directory under the As installation directory and add a configuration item that disables the start of running the wizard:

disable.android.first.run=true

And then start the program will open the Project Wizard interface, this time if you click on Start a new Android Studio project is unresponsive, and the SDK manager under Configure is gray, this is because Android is not installed SDK's sake. This is usually done in two ways:

    1. I do not have the SDK, need to download from the network; Open the configure-settings of the wizard, enter proxy in the lookup box, locate the following HTTP proxy, set the proxy server, and force https://... sources to be fetched using HTTP//selected, and then quit commenting out the configuration item added above in the Idea.properties configuration file, reopen Android studio, and just start the wizard to download and install the Android SDK.

    2. You have the SDK, re-specify the SDK path, open the wizard's Configure->project Defaults->project Structure, here to fill in your existing SDK path.

Restart as now to create a new Android project in the wizard. This completes the installation process.

Introduction to basic use

First create a new project, enter the project name and the main package name and storage path; Click Next to step:

In the first place, you can choose whether the device you want to fit is wear or mobile or TV. When you create a new app to choose the minimum adaptation version, powerful as will give you some useful statistical hints, describes the current version of the user situation, click on the Help me choose after the following more image of the distribution chart description:

The highlight is that this step by step is more powerful than eclipse, which is just a little bit less worth mentioning, powerful features are still behind. Continue to click Next to select the friendly GUI template, click Finish to enter the project initialization process.

The first installation project was initialized due to the need for networking download Gradle will be slow, but sometimes not the first is also slow, engineering dependent version of Gradle will automatically re-download; My initialization is fast, The reason is that my local gradle-2.2-all.zip has been downloaded OK before. As for when Gradle will be explained. This is just to tell you that if you see the card in a moment, it's normal.

Next go to the engineering interface:

This creation process can be much longer than eclipse. Mainly because it is downloaded from Gradle. Gradle can also be downloaded manually offline and placed in the corresponding directory. The structure of the project differs from Eclipse, and SRC is divided into Java and res. As is based on idea, and there are great differences between idea and eclipse, there are good and bad places, and for some time, it is thought to be the most powerful IDE tool for developing Java, so as new is developed with new application and new module. Idea doesn't have a working space. This is where the eclipse user switches to the first one that doesn't fit in.

Specifically, that is:

    1. Android Studio is a single-project development model
    2. Application in Android Studio is equivalent to the workspace concept in eclipse
    3. Module in Android Studio is equivalent to project concept in eclipse

With the above three concepts of self-created by hand, believe that smart you naturally understand what happened.

Next look at some of the basic high frequency practical settings for 工欲善其事 its prerequisite:

  1. Chinese garbled-in the window, locate the IDE settings->appearance, tick "Override default fonts by" on the right, then select the font "SimSun" in the first drop-down box, then apply, restart the IDE, Just fine.

  2. Set shortcut keys-in the Settings window, locate the IDE Settings->keymap, and the shortcut key is open on the right. Right-click the shortcut you want to modify, a menu will pop up, and the shortcut can be modified by selecting "ADD keyboard shortcut". If you delete, select Remove XXX from the menu that pops up. In particular, you can set the use of Eclipse shortcuts or other IDE shortcuts directly in the shortcut key settings of AS. If you're passionate about eclipse, you can also set up Eclipse shortcut keys.

  3. Modify the theme-in the IDE settings->appearance, on the right side of the theme choose your favorite theme. Individuals prefer the Darcula theme, which is like the above style.

  4. How to import an Eclipse project into as use-select File->import Project, select the project you want to import in the popup menu, select OK, then direct next, and in the second window choose the default first option. It is important to note that in as, there are two kinds of projects, one project and one module, which has been described above.

  5. Import jar Package--Select File->projcet Structure, find the libraries on the left side of the popup window and select it, then click "+" and select Java to import the jar package. or copy the jar file directly into the project's Libs folder, and then run: Sync project with Gradle Files. Then clean project recompile.

  6. Delete Project-–as a protection mechanism for project deletion, by default you find no delete option in Project right-click. You will find a small mobile phone on your module, which is a protection mechanism. Removal of the first step is to remove the protection mechanism, that is, to let the phone is not visible, the specific method is to put the mouse on the project right-->open module setting, or F4 into the interface, select the module you want to delete, and then click the minus sign, so that the protection mechanism is removed, Then go back to Project engineering right-click to find the delete option. Note: Deleting will delete the source files.

    1. Modify a Project Catalog-when you create a project, after you select the project directory in project location, you enter a folder name to save the project, and then you can use your own project catalog.
Introductory summary

So far, as the basics, I'm sure you've got a rough idea. Specific advantages over eclipse are reflected in the following points:

    1. As is Google's purpose-built tool for Android based on IntelliJ idea. Biological is always the best, but it is still growing up.

    2. As in any aspect of speed is faster than eclipse.

    3. The Darcula theme UI is simply geek, and it's awesome.

    4. The powerful smart Tip completion feature is simply more efficient than eclipse when writing code.

    5. Smart save, do not need CTRL + S. Efficiency will be greatly improved.

    6. Integrating the Gradle build tool, Gradle combines the benefits of Ant and Maven, whether it's configuration, compilation, or packaging.

    7. The UI editor is simply more efficient than eclipse, and comes with a real-time preview of multiple devices, which is an artifact. Multi-language adaptation Click on the earth Direct input, no longer compare that string without translation.

    8. The built-in terminal directly replaces the cmd command line, and an IDE is all done.

    9. Perfect plug-in system, such as Git, Markdown, gradle, etc., direct search for download.

    10. Version control system, when installed with GitHub, Git, SVN and other popular version control system, you can directly check out your project, side write code right-side can directly have Bcompare function and other versions to compare and modify.

In a word, believe me, if you and I are the same eclipse users switch over, then you will never switch back, you'll love as.

Android Studio Directory Structure

After the new project, the product directory structure for as is as follows:

    .idea://AS生成的工程配置文件,类似Eclipse的project.properties。    app://AS创建工程中的一个Module。    gradle://构建工具系统的jar和wrapper等,jar告诉了AS如何与系统安装的gradle构建联系。    External Libraries://不是一个文件夹,只是依赖lib文件,如SDK等。

After the new project, the module directory structure of As is as follows:

BuildBuild the directory, equivalent to the default Java project in Eclipse Bin directory, the mouse on the right-click Show in Exploer to open the folder, compile the generated apk is also in the directory of the Outs subdirectory, but in the project as the default does not show the Out directory,    Even if there is a compilation result is not displayed, right-click Open through the folder directly can see. LibsA dependency package that contains packages such as Jar packages and JNI. Src:The source code, equivalent to the Eclipse project. MainHome folder Java:Java code, including engineering and new is the default generated test project source code. Res:Resource file, similar to eclipse. LayoutApp layout and interface element configuration, similar to eclipse. MenuApp menu configuration, similar to eclipse. ValuesSimilar to eclipse. Dimens.xml://define the configuration file for the CSS. Strings. Xml://the configuration file that defines the string. Styles. Xml://defines the configuration file for the style.  ...  ... : //arrays and other files.  ...  ... : //assets and other directories androidmanifest. Xml://app basic information (Android managed file) Ic_launcher-web.png://app icon build.gradle://module gradle build script            
Android Studio Development Debug Use

Android Studio debugging is also very convenient, general problems directly through as the DDMS Logcat can be done. As supports all features of DDMS like Eclipse. Here to say is the debugging of difficult problems, that is, breakpoint debugging.

First compile the program you want to debug.

Click Set breakpoint at line number as shown in 2. Then click to open the debugging session as shown in 1.

As shown, the Debug view appears below the IDE, and 1 points to the line of code where the debugger is currently stuck, and the 2 area is the program's method call stack area. In this area shows the method used by the program to execute to the breakpoint, the more the following method is called earlier. In this order presumably some Android in-depth understanding of a little Android system startup process know how these methods, how to the activity of the oncreate. Haha, speaking of the system. It's not a rip. 3 is a number of debugging buttons, shortcut keys placed directly on the above will be displayed. 4 and 5 are some variable observation areas.

In

Click the 1 point button, the program executes a line down, if the current line has a method call, this method will be executed to return, and then to the next line.

Click the 2 point button and the program executes one line down. If the row has a custom method, it runs into a custom method (a method that does not enter the official class library).

Click on the 3 button to enter any method when debugging.

Click 4 the role is if you enter a method (such as Debugfunc) when debugging, and feel that the method is not a problem, you can use 4 to jump out of the method, return to the method is used to the next line of the statement. It is worth noting that the method has been executed.

When you click the 5-point button, you will return to the current method's call to execute again, and the value of all context variables will return to that time. You can jump to any of these methods as long as there is a parent method in the call chain.

If you set multiple breakpoints, turn on debugging. To move across a breakpoint to the next breakpoint, click a 1 arrow, and the program will run a breakpoint between the next breakpoint and the code that needs to be executed. If the following code does not have a breakpoint, tapping the button again will run the program. Click on the arrow 2 to point to the button, you can see the breakpoint you have set and can set some properties of the breakpoint, as shown in. After debugging begins, in the variables area you can assign a value to the specified variable (the left mouse button selects the variable, the right-click popup menu selects the SetValue ... )。 This feature allows you to detect your conditional statements and loop statements more quickly. You can view the value of the trace variable by tapping the arrow 3 plus or by right-clicking on the debug code variable to add watcher.

Arrow 1 points to the breakpoint you have set, and arrow 2 can set a conditional breakpoint (when a condition is met, pause the execution of the program, such as Index==5). After you finish debugging, you should delete the breakpoint you set at arrow 1 (after selecting the breakpoint you want to delete, click the red minus sign above).

These are some of the debugging techniques used by AS.

Android Studio Building System Fundamentals Basics

When the project is successfully created, it will download gradle automatically, which is very slow and FQ recommended. The downloaded gradle on the Windows platform will default to the C:\Documents and settings\< user name >.gradle\wrapper\dists directory, which has a Gradle-x.xx-all folder under this directory. You can also manually download the corresponding version to the Gradle website, and then copy the downloaded. zip file (or unzip it) to the Gradle-x.xx-all folder above.

Each module needs to have a Gradle configuration file, the syntax is the same, the only difference is that the beginning of the declaration is the Apply plugin. Be careful to distinguish between build.gradle files in different locations.

Build.gradle file under Engineering root of as:

Buildscript {//set the run environment of the script repositories {//support Java Dependent Library Management (Maven/ivy, etc.) for project Lai //mavencentral ()//Just a different network warehouse jcenter () //recommended use of this warehouse} //the definition of a dependency package. Support Maven/ivy, remote, local library, single file, the previous definition of the Repositories{}jcenter library, the use of Jcenter rely only on // Similar to Com.android.tools.build:gradle:1.0.0-rc2,gradle, the corresponding dependencies are automatically downloaded to the remote library. dependencies {classpath  ' COM.ANDROID.TOOLS.BUILD:GRADLE:1.0.0-RC2 ' //note:do not place your application dependencies here; they belong //in the individual module Build.gradle files}} //the centralized configuration of multiple projects, most build tools, and the configuration of sub-projects are based on inheritance. Gradle also provides this configuration allprojects {repositories {jcenter ()}}      , in addition to providing an inheritance mode setting subproject 

Settings.gradle file under Engineering root of as:

    include ‘:app‘      //module    include ‘:my_lib‘ //module(build as lib)

The Build.gradle file for module in the engineering root directory of AS (here is an example of a simple Lib module Gradle):

    The value of plugin in as is generally ' com.android.library ' or ' com.android.application ' Apply plugin:' com.android.library ' //build for Lib Android {compilesdkversion  //compile requires SDK version BUILDTOOLSV Ersion "19.1.0" //SDK Manager determines that the version can be installed locally to Defaultconfig {minsdkversion 8 //Minimum version targetsdkversion 17 //target version} buildtypes { //compiler Item release {minifyenabled false proguardfiles getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.txt '}}} dependencies { //dependent support compile ' Com.android.support: support-v4:18.+ '}               
Gradle Packaging App Signature

By default, debug is configured to use one of the debug keystory. The debug keystory uses the default password and default key and the default key password. The Debug build type is automatically configured with the debug signature. Add in your module's Build.gradle file:

 Android { ... " Yanbober.keystore ") Storepassword " Gradle "Keyalias  "Gradle" Keypassword  "Gradle"}} buildtypes{release {Runproguard true zipalignenabled true // Remove useless resource files shrinkresources true proguardfiles getdefaultproguardfile ( Proguard-android.txt '),  ' Proguard-rules.pro ' Signingconfig signingConfigs .myconfig}} }

Although the relative path of the project root is often used as the path to the KeyStore, an absolute path can also be used, although this is not recommended (except for the automatically created debug KeyStore). Run the Gradle clean Gradle build to generate a signature confusion-aligned app.

Gradle build Android apps Multi-Channel pack (bulk package)

The release of Android apps needs to face a variety of markets, which we call channels. Usually as a developer we need to know which channel the app was downloaded from. This statistical information is commonly used in Baidu statistics or friends League statistics. Here, for example, use the Friends League statistics as an example to illustrate the problem. The principle is Gradle manifest merger.

Configure the so-called placeholder in Androidmanifest.xml.

    <meta-data        android:name="CHANNEL"        android:value="${CHANNEL_VALUE}" />

In the defaultconfig of the module Build.gradle file plus placeholder, the function is to declare channel_value to be the placeholder of the replaceable value and set the Yanbober default value for it.

    android {        ......        defaultConfig {            ......            manifestPlaceholders = [ CHANNEL_VALUE:"yanbober" ] } }

Add the Productflavors configuration to the module's Build.gradle file. Productflavors is actually a definable product attribute, and with manifest merger it is possible to produce multiple versions with their own feature configurations during a single compilation. The purpose of this configuration is to produce different channel_value values for each channel package.

    android {        ......        defaultConfig {            ......            manifestPlaceholders = [ CHANNEL_VALUE:"yanbober" ] } productFlavors { yanbober{} wandoujia{} xiaomi{} baidu{} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [ CHANNEL_VALUE:name ] } }

Batch build multi-Channel package: Enter the project directory to run Gradlew assemblerelease. You can see that the compilation produced a total of 4 apk, respectively, corresponding to the 4 channels defined in the Productflavors segment. Anti-compilation opens androidmanifest.xml you will find that the channel configuration has been modified.

Generate a single channel package: Open as the Gradle Tasks panel module has many tasks, directly double-click the corresponding delay channel task to generate the corresponding apk. It would be nice to use Gradlew assemblexiaomirelease to generate Xiaomi channels separately using the command line.

Well, gradle the basic situation of this, specifically can read the official website or access to other materials, the use of gradle need experience accumulation.

Android Studio plug-in installation and use of Genymotion simulator

Android Studio comes with an emulator faster than the Eclipse plugin, but it's not violent enough, it's not cool enough. Now, the most violent genymotion simulator can be used in conjunction with AS. First on the Genymotion official website to download the installation of genymotion, and you need to register an account on the Genymotion official website, so that you can use the Genymotion normal.

To the left of the settings interface of as, locate plugins and click on the arrow button (i.e., install online):

By the way, the interface is the same as the general method of installing plug-ins, you can see the current installed plug-ins, choose to install online or from the hard disk installation, that is, for you have already downloaded the plug-in, you can use this option to your next good plug-in, to install.

As shown in the 1 zone input plug-in name, 2-Zone selection, 3-zone download installation, and then return to the as toolbar can see the Genymotion small icon, that is, the arrow 4 refers to the thing, indicating the installation OK.

Next is to set the next genymotion, create a new virtual machine device, this is genymotion things, as to how to get step by step on the line, no difficulty. Click on the As on the emulator icon can be started using, run as program selection Simulator can see their program in the simulator, is the genymotion start up interface.

This fast simulator genymotion has been taken care of to improve your speed. Other plug-in installation will be comprehend by analogy.

Other

The other is the shortcut key what thing. This thing will have to accumulate slowly the whole, pure accumulation of proficiency. Strange problem on Google and the degree of Niang. In fact, the version of as still have some bugs, but to meet the basic needs, meet the bug check to modify bypass or use the big strokes-restart as the general can solve, other strange problems welcome the discussion of common growth.

PS: Other issues on the line on the ad, this article is basically the translation of the ad and its own experience of the egg pain of the summary of the problem.

You're going to love him anyway.

Android Studio Summary

This is where you can use Android Studio for application development. Other questions believe that smart you use Google can handle, good luck! In short, as the strong need you to explore slowly, you will find that you will fall in love with him.

Android Studio get started to master

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.