
Recently the company upgraded the computers to the latest Mac Pro. Although it’s a good thing, it means I have to reinstall various software and tools, which is quite troublesome. First, I need to install the development tool Android Studio, and I have to reconfigure the features I set up before. Therefore, I decided to summarize a few important tips into an article for those who haven’t used them as a reference.
1. Modify Variable Prefix
According to Android’s development specifications, member variables are prefixed with m
, and static variables are prefixed with s
, following camel case naming conventions. When we write code in Android Studio, it will give us prompts, as shown below:
As shown in the image, the variables we write are not standardized. To change them to standardized variable names, we have to manually add m
in front of member variables and s
in front of static variables. However, doing this manually every time is quite cumbersome. Is it possible to unify the prefix for member variables? Of course, it is possible. Android Studio can be configured:
-
1. Open File -> Settings (on Mac: Android Studio -> Preferences) to access the settings panel.
-
2. Select Editor -> Code Style -> Java -> Code Generation.
-
3. Set Field prefix to: m, Static Field prefix to: s.
-
4. Click
Apply
to apply the configuration.
After the configuration is complete, we won’t need to add prefixes manually when adding member variables. Let’s take a look at the effect:
See? The prompts now include the prefixes m
and s
, which makes things much easier.
2. Use Plugins to Serialize Objects with One Click
In daily development, when switching between interfaces, we often need to carry some data. Basic types of data are fine as Intent can carry them directly, but most of the time we need to carry an object, which Intent cannot do directly. At this point, serialization is needed, and there are two ways to serialize (Serializable and Parcelable), with Parcelable being the most commonly used. Writing it by hand is cumbersome (we need to write write, and read, and keep the order of variables consistent) and prone to errors. This is where a serialization plugin comes in handy, allowing us to serialize with one click.
Plugin Installation:
-
1. Open File -> Settings to access the settings panel.
-
2. Select Plugins -> Browse Repositories.
-
3. Search for
Parcelable
plugin, then clickInstall
. -
4. After installation, restart Android Studio.
Usage:
-
1. Right-click in the entity class to be serialized -> Generate.
-
2. Click Parcelable -> Click
OK
to confirm.
Note: The Parcelable option will only appear in the menu if the installation is successful
Here’s a GIF demonstration:
3. Convert JSON to Java Bean with One Click
One of the most annoying things in development is writing entity classes. To avoid errors, we have to copy each field from the documentation to ensure each field matches the server. Here I recommend the GsonFormat plugin, which allows us to convert JSON into entity classes.
Installation Method:
-
1. Open File -> Settings to access the settings panel.
-
2. Select Plugins -> Browse Repositories.
-
3. Search for
GsonFormat
plugin, then clickInstall
. -
4. After installation, restart Android Studio.
Usage:
-
1. Create a new empty entity class, then right-click to bring up the menu.
-
2. In the pop-up, click GsonFormat.
-
3. Paste the JSON in the pop-up box.
-
4. Click
OK
to confirm.
Effect Demonstration:
Created a class
Entry
, then copied a segment of JSON, generating member variables. If the JSON contains another entity, a corresponding static inner class will be generated.
Isn’t it much more convenient? No need to copy each field one by one anymore.
4. Generate Fixed Format Header Information for Java Classes
When we create a Java class, Android Studio already generates header information for us, as shown below:
If you want to modify or add more information, how can you do it? We can configure it as well.
Configuration Method:
-
1. Open File -> Settings to access the settings panel.
-
2. Select Editor -> Code Style -> File And Code Templates -> Include -> File Header.
-
3. Fill in your own template, then click Apply -> OK to apply.
Now when adding new Java files, there will be new header information. For example, creating a new Test class:
See? It has been applied, and you can modify and add freely.
Conclusion
This article summarizes a few tips for Android Studio. For those who haven’t used them yet, hurry up and apply them, and you’ll definitely see a significant increase in your development efficiency. Android Studio has many interesting, useful, and convenient features, so those who love tinkering can explore more. Additionally, I previously wrote an article about changing themes in Android Studio, titled “Tools: It’s Time to Create a Cool Personalized Theme for Your Android Studio,” so everyone can take a look.
Previous Articles
1. 2018 Mobile Application UI Design Trend Predictions
2. Android Imitation of the New WeChat Mini Program Pull-down Bar
3. Android Arc ViewPager and Arc Header View (Upgraded Version)
If you found this article helpful, please share it with more people.
Follow 【Android Technology Store】, and there will be Android articles shared every day!



Long press to identify the QR code to follow