Android Studio introduction and usage summary, androidstudio

Source: Internet
Author: User

Android Studio introduction and usage summary, androidstudio

With the release of android 5.0, android development has entered a brand new era. It is a bit out of date for eclipse to develop android. android studio is used for android studio development in 80% of android projects on github, in addition, there are more and more Chinese tutorials on android studio. Why not stick to eclipse.

The following article is helpful for understanding android studio. The original Article is as follows:


This development tool was introduced by Google at the 2013 I/O developer conference. It has been updated to version 0.5.8.

Currently, many github projects use Android Studio for development. If you still use Eclipse, You need to split the entire project,
It is very troublesome to include dependency packages in the project. Sometimes it may not be able to be imported successfully after half a day. In addition, the future trend of Android Developer Tools must also shift to Android Studio.
For various reasons, you have to learn how to use Android Studio. The Project Management of Android Studio uses Gradle, so you need to have a simple understanding of Gradle. You can go to the official website to check it out.


Gradle Official Website



Because Android is developed based on IntelliJ IDEA, you can refer
IntelliJ IDEA official help address

Some concepts are briefly introduced: (some are translated from the official translation of individuals, and some are copied)
Android Studio
Create a system toolkit that you use to generate, test, and run your applications and software packages. The building system is independent from the Android studio, so you can call its Android studio or from the command line. When you write your application, you can use the features of the compilation system:
Customization, configuration, and expansion.

Create multiple applications to use different features of the same project for your applications.
Reuse code and resources.


The AndroidStudio build system is composed of Gradle. Gradle is an advanced build tool for managing dependencies, allowing you to define custom build logic.
The Android plug-in tool does not depend on Android Studio, although Android Studio is fully integrated with it.


Gradle configurations include the following:


Build variable

The build system can generate multiple APK for the same project according to different configurations. This is useful when you want to build apps of different versions without having to create separate projects for everyone.


Build System Management Project dependencies and support Dependencies from local file systems and remote repositories. This prevents you from searching, downloading, and copying Binary packages as your dependencies to your project directory.


List entries
The build system allows you to specify some elements in the configuration list file. These new values will overwrite the existing values in the manifest file. If you want to generate multiple
APK is useful for your project. Each of them has a different package name, the minimum SDK version, or the target SDK version.



The building system allows you to specify the signature settings for generating configurations. It can sign your APK during the generation process.



The build system allows you to specify each version variable of a different ProGuard rule file. The build system can run the ProGuard generation process to confuse your class.


Build the test source test APK in the project generated by the system, so you do not have to create a separate test project. The build system can run the test during the generation process.


The Gradle Build File uses Groovy syntax. Groovy is a dynamic language. You can use it to define custom generation logic and interact with the Android plug-in Gradle to provide Android-specific elements.


Established by Convention

The Android Studio generation system assumes a reasonable default value for the project structure and other compilation options.
If your project meets these conventions, your Gradle build file is very simple. When some of these conventions do not apply to your project, you can configure almost every aspect of the generation process.
For example, if the source of your project is located in a directory different from the default value, you can specify this location in the build file.


Projects and modules
A project represents a complete Android Application. The Android Studio project contains one or more modules. A module is an independent component of your application,
You can build, test, or debug it. Module contains the source code and resources of your application. The Android Studio project contains three modules:

Java library module
Contains reusable code. Build a Java library module that generates a JAR package.
Android library module

Contains unique Android code and resources. The building system generates the AAR (Android archive) package library module.


Android Application Module
Contains application code and may depend on library modules, although many Android applications only contain one application module. Build the system to generate an APK package for the application module.


Dependencies dependency

Android Studio generates System Management Project dependencies and supporting module dependencies, Local Binary dependencies, and remote binary dependencies.

Module dependency
The list of other modules that a project module can include in the Build File depends on. When you build this module, build system assembly, including the required modules.


Local dependency
If you have a binary archive dependent on a module in your local file system, such as a JAR file, you can declare these dependencies in this module in the build file.


Remote dependency
When some of your dependencies can be stored in a remote repository, you do not need to download them and copy them to your project. Android studio's system supports remote Maven dependencies. Maven is a popular software project management tool that helps you organize project dependencies using libraries.

Many popular software libraries and tools can be stored in the public Maven repository. For these dependencies, you only need to specify your Maven coordinates, which uniquely identifies each element in a remote repository. The format of Maven Coordinates Used in the build system is group: Name: version. For example, the coordinates of Maven are: com. google. guava: 16.0.1.
Maven's central repository is widely used to distribute many libraries and tools.

Build Tasks


The Android Studio generation system defines a series of hierarchical building tasks:
Top-level tasks: tasks used to generate necessary results.
Build system provides project tasks: Build tasks for your applications and modules to build independent modules.


Gradle Wrapper


The Gradle Wrapper included in the Android Studio project is composed of the following:
A jar file
A properties file
A shell script for Windows platforms
A shell script for Mac and Linux platforms


Note:You should submit all the above files to the resource control system.
Use Gradle wrapper (instead of local Gradle installation) to ensure that you always run the version defined by Gradle In the attribute file. To configure your project to use a newer version of Gradle,
Edit the attribute file and specify a new version.

Android Studio will read your configuration file from the Gradle Wrapper directory and then run and modify wrapper. Therefore, you can seamlessly process multiple projects according to different Gradle versions.
Android Studio does not support Shell scripts, so you should define custom logic in the Gradle file to replace it.


The following is a brief summary of the problems encountered during project construction.


Question 1. How to introduce a third-party lib?


A copy lib project to project, setting. gradle configuration include ': yourlib', otherwise the system will prompt that the lib project cannot be found under your project.
B to the entire project build. gradle configuration include ': actionbarsherlock'
C In Your Moudle, instead of adding
Dependencies {
Compile project (': yourlib ')


Right-click the information you checked on the Internet and choose add as library. My files are 0.5.8, but no information is found. If it is not introduced as lib, when synchronizing project with gradle files.
Default config is not supported. This is because your lib project does not have the build. gradle file. You can copy a simple build. gradle file to the lib project directory you added.
This will allow normal compilation. Of course, if your lib project also references some third-party jar packages, You need to configure them in dependencies.
The file is as follows:
apply plugin: 'android-library'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')}android {    compileSdkVersion 17    buildToolsVersion "19.0.3"    sourceSets {    main {        manifest.srcFile 'AndroidManifest.xml'        java.srcDirs = ['src']        resources.srcDirs = ['src']        aidl.srcDirs = ['src']        renderscript.srcDirs = ['src']        res.srcDirs = ['res']        assets.srcDirs = ['assets']    }    // Move the tests to tests/java, tests/res, etc...    instrumentTest.setRoot('tests')    // Move the build types to build-types/<type>    // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...    // This moves them out of them default location under src/<type>/... which would    // conflict with src/ being used by the main source set.    // Adding new build types or product flavors should be accompanied    // by a similar customization.    debug.setRoot('build-types/debug')    release.setRoot('build-types/release')    }}
Question 2 gradle Configuration
During the first download, you can download the latest version from gradle official website and decompress it to the gradle path of android studio. This is not fixed. It is the same as the SDK configuration,
You only need to specify the path in gradle setting. Then configure gradle to path and gradle-v to verify whether the configuration is successful.

Note: The configuration of gradle is in the build. gradle file under the entire Project.

Compiler error: Only 0.9 + gradle versions are supported.
Solution: You can configure 0.9 + in build. gradle, or click the prompt to select one of the following options in gradle setting.


Use default gradle wrapper(Officially recommended, with few issues)
Gradle is automatically downloaded to ensure that your project uses the exact gradle version.
In addition, if this option is optional and gray, copy a gradle folder from other projects to your project.
Use customizable gradle warpper(Supported in version 1.7 or later)
This option always checks and updates the specified gradle version. You only need to change the version number of gradle.
Use local gradle distribution(This is not recommended. You may not be familiar with any problems)
Use the local gradle to build the project, but make sure that you have installed and correctly configured the gradle in the path. In the command line, gradle-v can verify.


Problem 3: The sync project remains in the resolve dependencies ': classpath' state for a long time or Error :( 1, 0) Cause: org/gradle/api/artifacts/result/ResolvedComponentResult

Cause: The gradle configuration may be incorrect or the agent is not connected and cannot be accessed to the server.

Make sure that you also have build. gradle in other lib projects.
Please check the gradle/wrapper/
Gradle wrapper distributionUrl = http \: // response
We recommend that you clone the project from github or download the latest one. Prevent problems with old configurations.

As follows:

12 distributionUrl=http\://
Suggestions: Clone the project from github or download the latest one. Prevent problems with previous engineering configurations.
Generally, you want to use the offline status instead of connecting to the Internet. Therefore, you can use the local gradle configuration, but there are often various problems, such:
Error:No cached version of available for offline mode.Disable Gradle 'offline mode' and sync project

Especially when gradle is vague at the beginning, you do not know when it depends on lib or when it needs to be downloaded from the website.


Select the use default gradle wrapper configuration, and there are few problems (please ensure that the Proxy is connected successfully, you can check the connection in the Http Proxy). In the future, I will gradually accumulate more,

You are familiar with it. You can try again.

Question 4 work offline mode

If you use this mode, but the dependency lib project is not local, you still need to connect to the network. Otherwise, an error is reported.


Each time a file is started, the network checks whether the dependent files are the latest version, resulting in a very slow speed. If you confirm that the jar package or dependent project in your project is complete and you do not want to update the latest one, you can select offline in gradle setting so that it will not be updated. But don't be surprised if something goes wrong in this case. Connect to the proxy. Re-sync the project, because in many cases, you may not know whether some plug-ins on which the build project depends already exist.

In addition, if you use the configuration method 0.9 + (with a + number), no matter whether you select offline or not, it will be detected, if you do not have a network connection, an error will be reported during compilation.

0.9 +. This configuration method must be networked.

Therefore, if you select offline, check your gradle version configuration and write it as your gradle version. For example, you can download and configure gradle1.2.
We recommend that you configure a network connection that can be connected to the server.
Dependencies {
Classpath 'com. android. tools. build: gradle: 0.9. +'
Problem 5. com. android. dex. DexException: Multiple dex files define Landroid/support/v4/accessibilityservice

Cause:The support v4 package is imported multiple times, or the dependencies in Different build. gradle files have no intention of compile twice. For example, you have used

Dependencies {

Compile fileTree (dir: 'libs', include: '*. jar ')}


It indicates that all the jar packages under the libs Directory have been compiled with the support v4.jar package, and the build. gradle under the project has been compiled again:
Dependencies {
Compile 'com. android. support: support-v4: 18.0. +'


So the compile 'com. android. support: support-v4: 18.0. + 'under the project should be killed
In addition, not only support v4 packages, but also various lib and even layout attributes, style and color, cannot be repeatedly defined in unnecessary files, an error will be reported during sync project. You only need to keep one copy.


Question 6. AndroidMainfest. xml

Prerequisites:At that time, the label of my activity used Chinese characters, and the label of the activity used Chinese characters,

Scenario 1Fatal error when parsing: AndroidManifest. xml. failed to parse XML file: org. xml. sax. SAXParseException: Element type "activity" must be followed by either attribute specifications, ">" or "/> ".

Scenario 2Unexpected end of block data

Solutions for the above two scenarios:

Set Chinese label Extr to String.
However, some activity labels are Chinese during compilation, and the troubleshooting of one activity is quite disgusting. We recommend that all labels be extracted to the String. xml file.
You can try this.

There is another case: unexpected end of block data

When Sync project with gradle files
If you are using buildToolsVersion "19.0.0", change it to buildToolsVersion "19.0.3" and Re-Sync the project.
Question 7: The targetSDK versions in the AndroidMainfest. xml files of different lib projects are inconsistent
This is simple. You only need to change all to the same one.
Problem 8. plugin with id 'android' not found


Add the following code to build. gradle of the Project:

buildscript {   repositories {mavenCentral()   }   dependencies {classpath ''   }}
Problem 9 when NDK is used, the NDK not configured is added to build. gradle under the NDK lib Project
productFlavors {       arm {           ndk {           abiFilters "armeabi", "armeabi-v7a"           }       }       x86 {           ndk {           abiFilter "x86"           }       }       }


buildTypes {     debug {     ndk {         abiFilters "armeabi", "armeabi-v7a"     }     } }

I am using the first one. Problems occurred when building the photup project.

It is best to have a proxy connection when using it. Many times you need to download some plug-ins or dependency projects, unless you have all the dependency projects locally. However, we recommend that you have a proxy at the beginning, because
Before using Android Studio, you need to download some things, such as maven repository,
The gradle configuration must be accurate and kept up-to-date. clone and update the project or download the latest project whenever possible. Because I used to download a lot of zip files and run the demo, later, I directly imported some
The plug-in has been updated, but the configuration file has not been updated. Therefore, the resolve dependency classpath may stay for a long time.
Offline work is untrusted. Please try to maintain the agent connection of gradle. You may need to download some dependent projects or other configurations. It may be that the individual's processing is not good enough.

Please use the Gradle configuration option officially recommended: use default gradle wrapper


Shortcut Keys for Android Studio 


Ctrl + P method parameter prompt
Ctrl + space code prompt
Ctrl + Shift + Space provides Smart prompts in many cases
Ctrl + Alt + Space class name or interface name prompt


Alt + 1 quickly open or hide the project panel
Ctrl + H view the class structure
Ctrl + F12 view the structure of the current file
Ctrl + Q view comments
Ctrl + P view parameter information
Alt + Q view the declaration of the current method
Ctrl + Q view JavaDoc
Ctrl + W select the word and then the statement and then the Function


Alt + F1 locate code
Ctrl + F7 find the reference of the current element in the current file, and then press F3 to select
Alt + F3 quick search
Ctrl + Shift + F7 can highlight the use of the current element in the current file
Ctrl + Shift + N search for files
Ctrl + Shift + Alt + N find methods or variables in the class
Ctrl + B find the class or method to open the cursor
Ctrl + N Quick Search Class
Ctrl + F search for text
Alt + F1 can locate the elements being edited in each panel
Ctrl + Shift + Alt + N you can use wildcards or abbreviations to quickly open a class/file/symbol.
Ctrl + Alt + Up/Ctrl + Alt + Down to quickly jump to the search results


Shift + F6 reconstruction-Rename
Ctrl + X Delete rows
Ctrl + D copy rows
Ctrl +/or Ctrl + Shift +/comment (// or /*...*/)
Alt + Insert can generate Constructors/Getter/Setter, etc.
Ctrl + Alt + L format the code
Ctrl + R replace text
Alt + Enter import package, automatically corrected
Ctrl + Alt + O optimized imported classes and packages
Ctrl + J Automatic Code
Ctrl + Shift + Space auto-completion code
Ctrl + Alt + Space class name automatically completed
Ctrl + Shift + Insert: select the clipboard content and Insert it
Ctrl + Shift + J can be integrated into two rows
Ctrl + Alt + T can enclose the code in a block, for example, try/catch
Ctrl + Alt + V can introduce variables. For example, assign the SQL statement in parentheses to a variable.
Alt + F8 calculate the variable value
Ctrl + O you can override the method of the parent class

Recent problems

Ctrl + E recently opened file ----------
Ctrl + Shift + Backspace can jump to the Last edited place
Ctrl + Alt + left/right return the previous browsing position
Ctrl + E or Alt + Shift + C recently changed code
Alt + Shift + C compare the recently modified code


Ctrl + Shift + Up/Down code move Up/Down. ---------------
F2 or Shift + F2 highlight error or warning quick locating ------------
Ctrl + Up/Down Jump cursor to the first line or the last line
Ctrl + [or] can jump to the beginning and end of braces
Ctrl + Shift + up/down move Method

Ctrl + P method parameter prompt
Ctrl + space code prompt
Ctrl + Shift + Space provides Smart prompts in many cases
Ctrl + Alt + Space class name or interface name prompt

Related Article

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: 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.