Android Studio Simple Introduction and usage issues summary

Source: Internet
Author: User
Tags google guava

Android Studio Simple Introduction and Usage issues Summary 

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

Many of GitHub's projects are now developed using Android Studio, so if you still use eclipse, you need to split the entire project,
And then in the project include dependent packages, is very troublesome, and sometimes get a half a day is not necessarily able to successfully import. And the future trend of Android developer tools must also be to move to Android Studio.
For a variety of reasons, you have to learn to use Android studio,android Studio project management using Gradle, so you need to have a simple understanding of gradle, you can go to the official website to see.

Gradle official website

http://www.gradle.org

Because Android was developed on the basis of IntelliJ idea, you can refer to
IntelliJ's official help address
Http://www.jetbrains.com/idea/webhelp/getting-help.html

Simple introduction of some concepts: (some are official translations from individuals, some are copy-over)
Android Studio
Build the System toolkit you use to build, test, run your applications and packages. The build system is independent of the Android studio, so you can call it from the Android studio or from the command line. After you write your application, you can use the features of the compile system:
Customize, configure, and expand the build process.

Create multiple applications that use different characteristics of the same project for your application.
Reuse code and resources.

the Androidstudio construction system is composed of Gradle. Gradle is an advanced build tool for managing dependencies, allowing you to define custom build logic.
Android Plugin tool does not depend on Android studio, although Android Studio is fully integrated by it.

The Gradle configuration includes the following aspects

Building variables

the build system can generate multiple apk for the same project based on different configurations. This is useful when you want to build different versions of the application without having to separate the project for everyone.

Dependent Relationships
Build systems to manage dependencies on projects and support them from local file systems and remote repositories. This prevents you from having to search, download and copy binary packages for your dependencies into your project directory.

List Entry
The build system enables you to specify some elements of the value in the build configuration manifest file. These new values will overwrite the existing values in the manifest file. If you want to generate multiple
APK for your project, each of them has a different package name, Minimum SDK version, or target SDK version, which is useful.

Signature

The build system allows you to specify the build configuration signature settings, which can be used to sign your APK during the build process.

Proguard

The build system allows you to specify a different proguard for each version of the rule file. The build system can run the Proguard build process to confuse your class.

Test
Build a test source test in a system-generated project?? APK, so you don't have to create a separate test project. The build system can run tests during the build process.

Gradle build files using Groovy's syntax. Groovy is a dynamic language, you can use it to?? Define custom build logic and interact with Android plug-in Gradle, which provides Android-specific elements.

Established by convention

The Android Studio build system assumes a reasonable default value for the project structure and other compilation options.
If your project conforms to these conventions, your Gradle build file is simple. When some of these conventions do not apply to your project, you can configure almost every aspect of the build process.
For example, if your project's source is in a different directory than 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 module. A module is a standalone component of your application,
You can build, test or Debug. The module contains the source code and resources for your application. The Android Studio project consists of three modules:

Java Library Module
Contains code that can be reused. The build system generates a Java library module for a jar package.
Android Library Module

Contains reusable Android-specific code and resources. Build the system to generate the AAR (Android Archive) package Library module.

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


Dependencies Dependency Relationship
Android Studio generates system-managed project dependencies and supports module dependencies, local binary dependencies, and remote binary dependencies.

Module Dependency relationship
a project module can include a list of other modules in the build file that it relies on. When you build this module, build the system assembly, including the required modules.

Local Dependencies
If you have a binary archive in your local file system that relies on a module, such as a jar file, you can declare these dependencies in the build file for that module.

remote Dependencies
When some of your dependencies are available in a remote repository, you do not have to download them and copy them to your project. The Android studio-built system supports remote MAVEN dependencies. Maven is a popular software project management tool that helps you use libraries to organize your project dependencies.

Many popular repositories and tools are available in public maven repositories. For these dependencies, you only need to specify your own MAVEN coordinates, which uniquely identifies each element in a remote repository. The format of the MAVEN coordinates used in the build system is the group: Name: Version. For example, MAVEN coordinates for Google Guava Library 16.0.1 version is Com.google.guava: Guava: 16.0.1.
In Maven's central repository, it is widely used to distribute many libraries and tools.


Build Tasks
The Android studio build system defines a series of layered build tasks:
Top-level task: The task used to produce the necessary results.
Build systems provide project tasks: Build the tasks of your applications and modules to create separate modules.

Gradle Wrapper
The Android Studio project contains Gradle wrapper consisting 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 of the above files to the resource control system.
Use Gradle wrapper (instead of a local gradle installation) to ensure that you always run a version of Gradle defined in the properties file. To configure your project to use a newer version of Gradle,
Edit the properties file and specify a new version.

Android Studio will read your profile from the Gradle wrapper directory and run the change wrapper, so you can work with multiple projects seamlessly based on different versions of Gradle
Android Studio does not support shell scripting, so you should define custom logic in the Gradle file instead.

The following is a brief summary of the problems encountered in building the project
1. How to introduce a third party lib?
Http://stackoverflow.com/questions/20744692/android-studio-0-4-0-absherlock-gradle-without-import-module
A copy Lib project to the project, Setting.gradle configuration include ': Yourlib ', otherwise you will be prompted to find the Lib project under your project.
b to the Build.gradle configuration under the entire project include ': Actionbarsherlock '
C is added under your moudle instead of the whole project Build.gradle
dependencies {
Compile project (': Yourlib ')
}

in the online search data, right-click, select the Add as library, my is 0.5.8, did not find. If not introduced as LIB, at Sync project with Gradle files.
You will find that default Config is not supported. This is because your LIB project does not have a Build.gradle file, you can copy a simple build.gradle to the LIB project directory that you added.
This will allow you to compile correctly. Of course if your Lib project also references some of the three-party jar packages, you need to configure them in dependencies.
File as follows

[Java]View Plaincopy
  1. Apply plugin: ' android-library '
  2. dependencies {
  3. Compile Filetree (dir: ' Libs ', include: ' *.jar ')
  4. }
  5. Android {
  6. Compilesdkversion
  7. Buildtoolsversion "19.0.3"
  8. sourcesets {
  9. Main {
  10. Manifest.srcfile ' Androidmanifest.xml '
  11. Java.srcdirs = [' src ']
  12. Resources.srcdirs = [' src ']
  13. Aidl.srcdirs = [' src ']
  14. Renderscript.srcdirs = [' src ']
  15. Res.srcdirs = [' res ']
  16. Assets.srcdirs = [' Assets ']
  17. }
  18. //Move The tests to Tests/java, tests/res, etc ...
  19. Instrumenttest.setroot (' tests ')
  20. //Move The build types to build-types/<type>
  21. //For instance, Build-types/debug/java, Build-types/debug/androidmanifest.xml, ...
  22. //This moves them out of them the default location under src/<type>/... which would
  23. //conflict with src/being used by the main source set.
  24. //Adding New build types or product flavors should be accompanied
  25. //by a similar customization.
  26. Debug.setroot (' build-types/debug ')
  27. Release.setroot (' build-types/release ')
  28. }
  29. }


Issue 2 configuration of Gradle
in the first time, due to the slow download, you can go to gradle official download the latest version, extracted to the gradle of Android Studio under the path. This is not fixed, as with the SDK configuration,
Just specify a good path in the Gradle setting. Then configure Gradle to Path,gradle-v to verify the configuration is successful

Note: The Gradle configuration is in the Build.gradle file under the entire project project.

Compiler Error: only the Gradle version of 0.9+ is supported.
Solution: You can configure 0.9+ in the Build.gradle, or select one of the following options in the Gradle setting click the prompt

Use default Gradle wrapper(official recommendation, few problems)
Gradle is automatically downloaded to ensure that your project uses an accurate gradle version
Also, if the option is not selectable, gray, copy a Gradle folder from another project to your project
Use
customizable gradle warpper(support above 1.7 version)
This option always checks to update the Gradle version you specified, you only need to change the Gradle version number.
use local gradle distribution(not recommended, often out of various problems, may not be personally familiar)
Use the local gradle to build the project, but make sure that you have installed and configured the Gradle correctly in path. On the command line, gradle-v can be verified.

Issue 3:sync Project long stay in Resolve dependencies ': Classpath ' status or Error: (1, 0) cause:org/gradle/api/artifacts/result/ Resolvedcomponentresult
cause: The server may not be reachable because the Gradle configuration is incorrect or there is no connection agent.

Make sure that your other Lib project also has Build.gradle
Please check the gradle/wrapper/gradle-wrapper.properties:
Gradle wrapper distributionurl=http\://services.gradle.org/distributions/ Gradle-1.11-bin.zip or higher, when I downloaded the project earlier, so pointed to the gradle-0.9
So it's recommended to clone the project from GitHub, or download the latest. Prevent problems with old configurations.
as follows: [Java] view plaincopy
  1. Distributionurl=http\://services.gradle.org/distributions/gradle-1.11-bin.zip
  2. http://stackoverflow.com/questions/22989638/android-studio-gradle-could-not-create-plugin-of-type-libraryplugin

recommendation : Clone a project from GitHub, or download the latest. Prevent problems with previous engineering configurations.
usually want to use the state of offline, not to connect to the extranet, so use the local gradle configuration, but often there are various problems, such as:
Error:no cached version of com.android.tools.build:gradle:0.9.+ available for offline mode. Disable Gradle ' offline mode ' and Sync project

Especially at the beginning of the gradle is more vague, you do not know when it needs some dependent lib, when need to go to the site download.

Suggestions:

Choose the Use default Gradle wrapper this configuration, but also very few problems (please ensure that the agent successfully connected, you can check the HTTP proxy connection), and gradually accumulated a lot later,

The use is relatively ripe, you can try again.

Issue 4 Work Offline mode

If you use this mode, but its dependency Lib project is not on-premises, then it needs to be networked, otherwise it will get an error.

Http://stackoverflow.com/questions/20746071/failed-to-build-android-hello-world-application-in-offline-mode

Each boot will have a network detection depends on the various files are the latest version, resulting in a fairly slow each time. If you have determined that the jar package in your project or the dependency of the project is complete, do not want to update the latest, you can check the Gradle setting offline, so it will not be updated. But don't be surprised if something goes wrong in this situation, please connect the agent. Re-sync project, because there are a lot of things you might not know about whether the build project depends on some of the plugins and so on.

Also mention, if you use 0.9+ such configuration (with + number), then whether you choose Offline, will go to detect, and, if you do not have Internet connection, compile will error.

0.9+, this configuration method must be networked.

So if you choose Offline, then please check your gradle version configuration, can be written directly to your gradle version, such as I downloaded and configured gradle1.2.
In the case of a network connection that can be connected to the server, the following configuration is still recommended.
dependencies {
Classpath ' com.android.tools.build:gradle:0.9.+ '
}

Issue 5. com.android.dex.DexException:Multiple dex files define Landroid/support/v4/accessibilityservice

Cause:The Support V4 package is imported multiple times, or is inadvertently compile two times in different build.gradle files, for example, in your actionbarsherlock dependencies

dependencies {

Compile Filetree (dir: ' Libs ', include: ' *.jar ')}


indicates that all the jar packages in the Libs directory have been compiled, the support V4.jar package has been compiled, and the Build.gradle under the project has been compiled:
dependencies {
Compile ' com.android.support:support-v4:18.0.+ '
}

so we should get rid of the compile ' com.android.support:support-v4:18.0.+ ' under the project.
In addition, not only the support V4 package, all kinds of lib and even various layout properties, Style,color, are not allowed to repeat the definition in the unused files, when the Sync project will be error, you only need to keep a copy

question 6. Androidmainfest.xml problems

The premise: the label of my activity was used in Chinese, and the activity's label was in Chinese,

Scenario 1 Fatal error when parsing:AndroidManifest.xml. Failed to parse XML file:org.xml.sax.SAXParseException:Element type "Activity" must is followed by either attribute spec Ifications, ">" or "/>".

Scenario 2 unexpected end of block data

Solutions for both scenarios:

Extr the Chinese label to a string
However, when I compile some of the activity of the label is Chinese, an activity of the wrong row is more disgusting. It is also recommended that all labels be extracted to the String.xml file.
I have modified it so that we can try it.

Another scenario arises: unexpected end of block data
at Sync project with Gradle files
Solution:
Http://stackoverflow.com/questions/23045022/unexpected-end-of-block-data-in-gradle-sync
If you are using Buildtoolsversion "19.0.0", then change to Buildtoolsversion "19.0.3" and re-sync project to

Issue 7 inconsistent TARGETSDK versions in Androidmainfest.xml files in different Lib projects
this simple, just need to change all to the same.

Issue 8. Plugin with Id ' android ' not found

Http://stackoverflow.com/questions/18153739/android-studio-plugin-with-id-android-library-not-found

In Project Build.gradle, add the following code
[Java] view plaincopy

  1. Buildscript {
  2. repositories {
  3. Mavencentral ()
  4. }
  5. dependencies {
  6. Classpath ' com.android.tools.build:gradle:0.5.+ '
  7. }
  8. }
Issue 9 NDK Not configured when using the NDK
Http://stackoverflow.com/questions/20674650/how-to-configure-ndk-with-android-gradle-plugin-0-7
Added in Build.gradle under the Ndk Lib project
[Java]View Plaincopy
  1. productflavors {
  2. Arm {
  3. NDK {
  4. Abifilters "Armeabi", "armeabi-v7a"
  5. }
  6. }
  7. x86 {
  8. NDK {
  9. Abifilter "x86"
  10. }
  11. }
  12. }
Or
[Java]View Plaincopy
    1. Buildtypes {
    2. Debug {
    3. NDK {
    4. Abifilters "Armeabi", "armeabi-v7a"
    5. }
    6. }
    7. }

I'm using the first problem that occurs when the build Photup project

briefly summarize below:
It is best to have an agent connection when using. There are a lot of times when you need to download plugins or dependencies, unless you have all the dependencies on your local project. However, at the beginning of the use, it is recommended that there are agents, because
In just using Android Studio, you need to download something, like maven repository,
Gradle configuration to be accurate, keep is up to date, try to clone and in use when updating the project, or download the latest project, because the individual previously download a lot of Zip, run demo with, and then directly imported, some
The plugin has been updated, but the configuration file has not been updated. So may lead to resolve dependency classpath for a long time stay.
Offline work is not credible, please try to keep the Gradle proxy connection, it is likely to need to download some dependencies, or other configuration. Perhaps the individual is not well disposed of.

Please use the official recommended gradle configuration option: Using default Gradle Wrapper

Android Studio shortcut keys  
Tips

Ctrl+p Method Parameter Hints
CTRL + Space Code hints
Ctrl+shift+space can give smart hints in many cases
Ctrl+alt+space class name or interface name hint

View

Alt+1 quickly open or hide the engineering panel
Ctrl+h view class structure diagram
CTRL+F12 view the structure of the current file
Ctrl+q Viewing a comment document
Ctrl+p Viewing parameter information
ALT+Q View the declaration of the current method
Ctrl+q View Javadoc
Ctrl+w Select the word and then the statement goes to the function.

Find

Alt + F1 Find where the code is located
Ctrl + F7 finds references to the current element in the current file, and then presses F3 to select
Alt + F3 Quick Find
Ctrl + Shift + F7 to highlight the current element's use in the current file
Ctrl + shift+n Find files
Ctrl + Shift+alt+n Find a method or variable in a class
Ctrl + B finds the class or method at the opening cursor
Ctrl + N Quick Find class
Ctrl + F Find text
ALT+F1 you can position the element you are editing in each panel
Ctrl+shift+alt+n can quickly open a symbol quickly when opening classes/files/symbols, you can use wildcards, or you can use abbreviations
Ctrl+alt+up/ctrl+alt+down can quickly jump to search results

Fix

Shift+f6 Refactoring-Renaming
Ctrl+x Deleting rows
Ctrl+d Copying rows
ctrl+/or ctrl+shift+/Comments (//or/*...*/)
Alt+insert can generate constructors/getter/setter, etc.
Ctrl+alt+l Formatting Code
Ctrl+r replacing text
Alt+enter Import package, auto fix
Ctrl+alt+o Optimizing imported classes and Packages
CTRL+J Automatic Code
Ctrl+shift+space Auto-Complete code
Ctrl+alt+space class name Auto-complete
Ctrl+shift+insert can select Clipboard contents and insert
Ctrl+shift+j can integrate two lines
Ctrl+alt+t can wrap code inside a piece, such as Try/catch
Ctrl+alt+v can introduce variables. For example, to assign a variable to a SQL in parentheses
ALT+F8 Calculating variable values
Ctrl+o can choose a method of the parent class to override

Recent related

Ctrl+e files that were recently opened----------
Ctrl+shift+backspace can jump to the last edited place
ctrl+alt+ Left/right back to the last viewed location
Ctrl+e or Alt+shift+c recently changed code
Alt+shift+c vs. recently modified code

Moving

The Ctrl+shift+up/down code moves up/down. ---------------
F2 or SHIFT+F2 highlighting error or warning quick location------------
Ctrl+up/down cursor jumps to the first or last line
ctrl+[or] can jump to the beginning of the curly brace
Ctrl+shift+up/down Move Method

Ctrl+p Method Parameter Hints
CTRL + Space Code hints
Ctrl+shift+space can give smart hints in many cases
Ctrl+alt+space class name or interface name hint

Android Studio Simple Introduction and usage issues summary

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