Getting Started with Android Studio

Source: Internet
Author: User
Tags android sdk manager

本文适用于从Eclipse转AndroidStudio的开发者

Recently intends to write a series of Android primary development tutorials, estimated more than 40 articles, combined with my actual work experience, write some work often used in the technology, so that beginners can less detours, this is the first article of this series, 345 great God wrote.

Original address

One. Download and install
    1. JDK Install and configure Java_home environment variables

      这个就略过了,纯小白请自行百度
    2. Android Studio Installation

      截止到2016年4月9日Android studio的最新稳定版本为2.0,推荐使用该稳定版本。

    • 1 website download page Portal

      如果下载速度很慢,请复制下载连接到迅雷,万能的迅雷会帮你提速的。如果根本打不开请移步地址2
    • 2 Androiddevtools

      You need the basic will have here, strongly recommend to download here.
      If the Local has already downloaded the SDK, it is recommended to download the zip-free version, the first time you start the configuration of the SDK directory is good.

Two. First time start
    1. After the installation is not anxious to start, the first launch will pop up check the SDK update components and other settings, click Finish may go to download, if not set up the agent, this interface will be stuck for a long time.

Workaround

    • Close Android Sutdio, open the Bin folder under the Android Studio installation directory, locate idea.properties and open it.
    • Add the following code to the tail of the file:
# 禁用首次检查sdk功能disable.android.first.run=true
    • 1
    • 2
    • Restarting Android Studio will no longer show the SDK Setup Check screen.

      1. You will see the following interface when you start to complete

Click Configure Select SDK Manger to configure properties such as SDK directory

Click on the lower left corner to connect to launch SDK Manger Configuration SDK Update image
I want to say this step in the celestial kingdom is necessary, reason I think you understand

Here's how to use the SDK update Mirror site:
Please refer to Androiddevtools for details.
1. Launch the Android SDK Manager, open the main interface, select "Tools", then "Options ..." and pop up the "Android SDK manager-settings" window;

    1. In the "Android SDK manager-settings" window, fill in the "HTTP proxy server" and "HTTP Proxy port" input boxes with the above mirror server addresses (which do not contain HTTP.//, such as) and ports, and select "Force https://. Sources to be fetched using http://... check box. Click the "Close" button to close the "Android SDK manager-settings" window and return to the main screen after Setup is complete.

    2. Select "Packages", "Reload" in turn.

      选择镜像网站的时候要注意的是,并不是所有的适合别人的就适合你还有可能根本无法更新,由于服务器远近带宽的问题,你应该选择适合自己的镜像,怎么选择。。。。我想说设置成功后选择更新个没下载的文件能成功下载更新并且速度不慢的就是合适的
    3. Theme Settings

This is nothing to say, but the pursuit of the black theme of the small partners to care about this many, here is explained.
Start Page Click configure-> Select Prefrences->appearance->theme->darcula that's the black Visual Studio style theme->apply finished

If you want more font keyword color What personalized configuration, recommend to the following URL download
Http://color-themes.com/?view=index
Choose the theme you like to download, download to a jar file, using the method:

    在AS中选择File->import settings ->选择下载的jar文件->勾选editor color 确认
Three. Project Engineering
    1. New Project

Follow the guidance step by step, pay attention to the minimum compatible version of the selection, the new project does not provide a compile version of the option, the default currently installed the highest version of the SDK, as to provide a few activity templates, template implementation are more classic, recommend a new look.

    1. Project directory Structure

Create a new project click on the upper left structure view toggle, switch to the project directory structure (the development is commonly used in Android view, relatively concise) where app2 and mylibrary is to facilitate the next explanation of the newly completed manually added module default new project is not.

  • MyApplication folder status quite eclipse of the workspace

    不同的是AS工程目录下面多了用于全局项目的配置文件,比较重要的是local.properties、setting.gradle、gradle.properties 、build.gradle和gradle文件夹
    • Local.properties
      Where you can set up your SDK, NDK configuration, generally do not need to change
    • Settings.gradle
      The file contains only a sentence, the module contained in the Declaration
      For example, the above project contains three module

      include ‘:app‘, ‘:app2‘, ‘:mylibrary‘ 
    • Gradle.properties
      Gradle some parameter settings used in the compilation normally do not need to change
    • Build.gradle
      Define the dependencies of the version warehouse and some gradle commands generally do not need to change
  • Module directory

    This directory is very similar to the Eclipse directory, it is easy to understand the corresponding relationship between the various directories, the more special is more than eclipse a series of mipmap folders, mipmap and drawable folders are used to store pictures, according to official documents is The Mipmap folder is only placed in the app icon. Other drawable resources that need to be used are placed in the corresponding Drawable folder as before.

    New Moudle when the Drawable folder only default without resolution parameters, for different resolutions of the folder needs to be built manually, as provides a convenient way to create

    On Moudle, right-click on Android Resource Directory, select the type drawable to the left list select density Select the DPI
    Such as



    Other res files are created in the same vein.

    1. Open the Internet download as engineering Gradle error problem

Sometimes we download a demo project from the Internet after open Gradle error can not run, this situation most due to the Gradle version inconsistency, as download corresponding version of the problem caused by the failure, generally there are two ways to solve
* Manually download the corresponding Gradle version to the local directory

    这种方式不推荐有兴趣的自行搜索

* Modify the Gradle configuration to open the project

Refer to the same file for the local new project
Gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip//修改版本号与本地工程文件相同
    • 1
    • 2

Build.gradle

dependencies {        ‘com.android.tools.build:gradle:2.0.0‘        //修改版本号与本地工程文件相同}
    • 1
    • 2
    • 3
    • 4
Four. Add dependencies

Understand the project structure after simple compilation run debugging generally there should be no problem, after all, the menu bar, the bottom function window function hints are very clear, the following is a comparison of the place to encounter problems: add dependent projects or run a third-party project error problem.

There are generally three ways to add dependencies
1. Adding a dependency on the Code warehouse

This is the simplest way to add a word directly to the dependencies inside the Build.gradle of the project module, and then rebuild it, such as:

dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    testCompile ‘junit:junit:4.12‘    compile ‘com.android.support:appcompat-v7:23.3.0‘ compile ‘com.android.support:design:23.3.0‘ //添加了OKHttp的依赖 compile ‘com.squareup.okhttp3:okhttp:3.2.0‘}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Can be added manually or you can right-click the item in open Module settings search Add (recommended this way)

AS会自动联网下载添加的依赖 这个一般不需要FQ
    1. Dependency of the JAR package

This is the simplest and does not have to be networked, like Eclipse to copy the jar package into the Libs folder, and then need to rebuild the project click on the menu bar of the Build button

Build complete the left side of the jar file in the directory bar should have a pointed tip to expand view jar Package Contents

Some jars also need to be associated. So library just need to build a folder named Jnilibs under the main folder of the SRC directory to copy the so library in (same as res directory)

3. Reliance on the Libray project

Most of the time we need to use a project downloaded from GitHub and add the library provided to our project by first importing module and then adding dependent
Open project structure, add new Moudle, select the module for the library you want to import in the downloaded project file
note Instead of importing the entire project folder that you downloaded, select the module folder



Add the imported library to the dependency of the destination project

当导入的library里的项目build.gradle文件里包含一些上传到版本仓库的信息的话导入过后可能会报错,这时候只需要把不必要的信息注释掉即可,需要保留的是编译配置,依赖关系,目录配置。如下
apply plugin: //Project compile information Android {compilesdkversion 23 Buildtoolsversion  "23.0.2" Defaultconfig {minsdkversion 11 Targetsdkversion 23 versioncode 1 versionName " 1.0 "}}//dependency information dependencies {compile filetree ( ' *.jar ']) testcompile  ' junit:junit:4.12 ' compile  ' com.android.support:appcompat-v7:23.2.1 ' Compile  ' com.android.support:design:23.2.1 '}      
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

Of course, you can also manually copy the module folder to the project directory and then modify the Settings.gradle file, but this is not recommended.

Five. Shortcut keys

As the automatic prompt function is very powerful, has the input after the automatic popup prompt does not need to like eclipse presses alt+/to prompt, as for other as's shortcut key many does not explain, last picture takes to self-discipline

Six. Plug-in installation

Open Setting page Find plugins search installation

Recommended a few as plug-ins, the specific use of your own Baidu
1. Gsonformat
2. Android-butterknife-zelezny
3. Jsononlineviewer

Seven. Multi-channel Package

Domestic on-line app generally to be released from the multi-channel, in order to statistical channel installation generally need each channel to produce an installation package, as comes with multi-channel packaging function, multi-channel packaging is definitely as one of the most practical functions, here is the key to talk about.

    1. Generate Signature File

Signature files can be generated in Build->generate signed APK---Select project Next->create new, in the same way as Eclipse

    1. Set up a signed file for packaging

Open module setting create a new signing key select signature file fill in the signature file password alias, etc.

    1. New Flavors Configuration

As an example, I created three new configurations and selected the signature to be packaged to configure a offline_debug, a release_360, a Release_baidu

    1. Modify module's build.gradle and mainfest files

After the configuration of the previous step, Build.gradle will be more than productflavors and singconfig configuration, we want to modify the productflavors part.
How to modify us to illustrate

This is the part of the Build.gradle file that was modified in

Defaultconfig {ApplicationID"Com.example.mark.myapplication" minsdkversionTargetsdkversionVersioncode1 versionname"1.0"//buildconfig. Is_sign = False Buildconfigfield"Boolean","Is_sign","False"} productflavors {offline_debug {signingconfig signingconfigs .signconfig//Here is a variant of the notation manifestplaceholders = [app_name:  "Debug Version"]} release_360 {signingconfig signingconfigs.signconfig//BuildConfig.is_sign = True Buildconfigfield " boolean ",  "Is_sign",  "true"//here is a demonstration of the notation of multiple variables manifestplaceholders = [App_name:  "360 version", EXAMPLE: .signconfig buildconfigfield  "boolean", Span class= "hljs-string" > "Is_sign",  "true" manifestplaceholders = [app_name: " Baidu version, EXAMPLE:  "Baidu"]}}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

where defaultconfig within the Buildconfigfield "Boolean", "Is_sign", "false" is the build project generated in the Buildconfig class to add a bool variable named Is_sign, and assigns a value of false.
This variable can be used in the code, such as the following code can be used to switch the line and offline request address

public static String BASE_URL;    static {        if(BuildConfig.IS_SIGN){            BASE_URL = "线上地址"; }else{ BASE_URL = "线下地址"; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Manifestplaceholders = [app_name: "Debug Version"] in productflavors is the meaning of replacing the app_name in the Androidmanifest.xml file with the string after the colon. This sentence is only one of the variables to be substituted, the following example of the way you have multiple variables. This app_name is defined in the manifest file as follows ${variable name} Gradle will automatically replace the placeholder variable when it is packaged.

<activity android:name=". Loginactivity "android:label=" ${app_name} "> <intent-filter> < action android:name= " Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </ACTIVITY>             
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

This variable definition can appear in mainfest any need string position, I above is just an example, please ingenious do not copy copy.

    1. Packaged

At the same time, select the configuration item you want to package click Finish to generate multiple apk at the same time.

Recommended to create their own Android QQ group: 202928390 welcome everyone to join.

If you want the first time to read our late article, sweep code attention to the public, every weekend will push the Android development of a practical tutorial, the rest of the time we will launch some Internet industry news, you are waiting for what, quickly pay attention to it, can learn the technology, but also long-cornered, as CEO, win white rich beauty ....

Getting Started with Android Studio

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.