This post was moved from one of my other platforms a little too long ago

Android Studio provides a very handy feature to help you import or export Settings. So when we install a new Android Studio we usually import one of the previous Settings. However, one damn thing happened recently – I lost my Settings file.

My advice: immediately back up your Settings files to the cloud disk.

When I’m configuring my Android Studio, here are a few things that might help you.

According to the line Numbers

When I first started my Android Studio, one of the first things I wanted to do was to see the line number in the file. I was always wondering if this basic configuration was not enabled by default. ! I have just two words to say:

No line number is displayed

According to the line Numbers

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | General | Appearance
  • Check theShow line numbers

Set up the

Ps: Right-click Show Line Numbers in the edit area to make the current open file Show line numbers, but this is a temporary setting and will be invalid once the current file is closed.

Hump choice

In Android development, we usually use the hump naming method for variables, but Android Studio does not support the word ‘hump’ by default when changing the character selection area by pressing Ctrl + Left/ Right.

Hump selection is not supported

Support hump selection

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | General | Smart Keys
  • The selectedUse “CamelHumps” words

Set up the

Note: If you still want to select the entire word after double-clicking on it, you need to set the following:

  • File | SettingsOpen the Settings
  • chooseEditor | General
  • uncheck'Honor Camel Humps Words Settings when selecting on double click'

Naming prefix

We usually follow the official Android code style guidelines for naming fields. In the Android source code we can see that member variables usually start with ‘m’. In fact, Android Studio can automatically generate field names with custom prefixes, such as:

  • Non-common, non-static member variables withmstart
  • Static member variablessstart

Naming prefix

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | Code Style | Java
  • chooseCode GenerationThe label
  • To the generalFieldAdd amThe prefix,Static filedAdd asThe prefix

Set up the

A quick guide package

In Android Studio, you can use Alt + Enter and Control + Alt + O to do guides and clear useless guides, but we’re living in 2016 and these things should be done automatically and quickly.

Imports on the fly not opened

Open imports on the fly

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | General | Auto Import
  • Check theOptimize imports on the fly
  • Check theAdd unambiguous imports on the fly

Set up the

The Log color

The default Darcula theme color scheme for Logcat is only red and white, which makes it difficult for us to distinguish between Log types.

Darcula theme color scheme

I recommend using the same vibrant color palette as the previous Android Holo theme.

Holo theme color scheme

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | Color & Fonts | Android Logcat
  • Click on theClick on the Save As...Button to create a new color schemeScheme
  • Change the color according to the table below (uncheck before changingUse inherited attributes)

Here are some personal additions:

The color code

The default code color palette in Android Studio already feels pretty good to me, but it’s a matter of opinion. For example, some friends feel that the default white of local variables in Java code is not easy to distinguish from other code, this time you need to customize the Java code color, local variables as an example.

The default color

Custom color matching

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | Color & Fonts | Java
  • Click on theClick on the Save As...Button to create a new color schemeScheme
  • UnfurledVariableschooseLocal variable
  • Set the rightForegroundcolor

Set up the

Project template

Android Studio does not generate all the files and directories commonly used in Android development when creating modules. For example, only a drawable folder is generated by default, and common drawable -HDPI folders need to be created by ourselves. As the author of the previous article pointed out, it’s 2016 and these things should be done automatically because we’re all ‘lazy’ after all!

The default structure

Custom structure

Configuration Method 1

  • Go to the Android Studio installation directory
  • In turn intoplugins | android | lib | templates | gradle-projects | NewAndroidModule | root | res
  • Create it in the res folderdrawable-hdpiEtc folder (Optional: from the correspondingmipmapCopy in foldersiclauncher.pngTo create adrawableFolder)
  • Go back toNewAndroidModuleDirectory, open with an editorrecipe.xml.ftlfile
  • Add the following configuration

Configuration Method 2

  • Go to the Android Studio installation directory
  • In turn intoplugins | android | lib | templates | gradle-projects | NewAndroidModule
  • Open with an editorrecipe.xml.ftlFile and add the following configuration

The difference between the two methods is that the first method can add the corresponding image to the folder, but the configuration is a little tedious, the second method is simple to configure, but can only create a directory, cannot contain the default image.

Of course, there are many other things we can do when creating a Module in a similar way, such as:

  • incolors.xmlFile to generate common colors
  • inbuild.gradleThe custom configuration is generated in the
  • in.gitignoreFile to generate custom ignore configuration
  • And so on…

Activity template

Android Studio provides a lot of handy Live Templates by default. For example, if you type sout and press Enter, Android Studio automatically writes system.out.println () for you.

In fact, Sout is an activity template that comes with AS.

As you can see, active template is an acronym for code we often use. A lot of code is repeated in development, so customizing appropriate activity templates can save us a lot of repetitive manual labor. So the question is, how do you customize it? Let’s use Handler as an example. Here’s what a qualified Handler in an Activity looks like:

<pre class="brush: java; gutter: true; first-line: 1 hljs" style="margin: 15px auto; padding: 10px 15px; display: block; overflow-x: auto; color: rgb(51, 51, 51); background: rgb(251, 251, 251); word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; font: 400 12px/20px "courier new"; border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" >private static class MyHandler extends Handler { private WeakReference<MainActivity> activityWeakReference; public MyHandler(MainActivity activity) { activityWeakReference = new WeakReference<MainActivity>(activity); } @Override public void handleMessage(Message msg) { MainActivity activity = activityWeakReference.get(); if (activity ! = null) { } } }</pre>Copy the code

I won’t repeat the reasons for this. There are so many articles on the Internet. Now, if I only wanted to type ‘PSH’ and the above code would automatically appear, I would do this:

Configuration method

  • File | SettingsOpen the Settings
  • chooseEditor | Code Style | Live Templates
  • Click the plus sign on the far right and selectTemplate Group
  • Enter the name of an active template group in the dialog box that pops up, such ascustom
  • On the left select the one created in the previous stepcustomGroup, click on the rightA plus sign
  • chooseLive TemplateIn theAbbreviationIn the inputpsh
  • inDescriptionEnter a description of the active template in
  • inTemplate textEnter the following code in

  • Click on the bottomDefineButton, selectjavaIndicates that this template is used for Java code
  • Click on the rightEdit variables
  • chooseExpressionIn the drop-down listclassNameAnd check theSkip the if...

AS will automatically replace the className we wrapped in the ‘$’ sign in the previous step with the name of the current class that does not contain the package name

  • Click on theApplyandOkLet the Settings take effect.

At this point, a PSH active template in our custom template group is defined. Let’s see, it’s time for a miracle:

If gradle is slow to compile or depends on downloading, you can consider domestic images

When AS imports a new project, it will download some dependency packages, which is often a long process. However, we can solve this problem by configuring the domestic image. Here is a record of the process of configuring the Maven image of Ali Cloud.

Ali cloud maven mirror address is maven.aliyun.com/mvn/guide we need in the project root gradle file find buildscript and allprojects node, In these two nodes, replace the original Maven address with ali Cloud’s Maven address

buildscript {
    repositories {
        // Ali Cloud cloud efficiency warehouse
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        // JitPack remote repository
        maven { url 'https://jitpack.io' }
        mavenCentral()
        google()
        jcenter()
    }
}
 
allprojects {
    repositories {
        // Ali Cloud cloud efficiency warehouse
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        // JitPack remote repository
        maven { url 'https://jitpack.io' }
        mavenCentral()
        google()
        jcenter()
    }
}

Copy the code