Last Thursday, Google finally released Android Studio 3.0 after more than half a year of refinement, providing a satisfactory answer for Android developers. As usual, with every new version of the development tools, many cautious friends still worry about stability, whether there are pitfalls, etc., choosing to wait and see before updating.
After reviewing the official introduction of the new features in the development tools, I decided to upgrade that day. In fact, I did spend quite a while testing it, but so far I have not encountered any pitfalls. I just needed to make some configuration adjustments for old projects. Overall, the responsiveness and usability of the new features make the upgrade worthwhile. This article will introduce the practical changes brought by the new development tools and share my practical experience on how to make compatibility modifications for old projects.Note: The welfare activity I did last week is still open for participation, please click the link to send books and join the small secret circle for free. Thank you for accompanying me for two years.
Changes Brought by 3.0
Kotlin Support
Before Android Studio 3.0, developing Android applications using Kotlin required additional configuration of the Kotlin plugin. The new version has built-in default support for Kotlin, reducing our configuration workload.
By selecting the toolbar Code -> Convert Java File To Kotlin File, or using a shortcut key, you can convert a Java file to a Kotlin file with one click.
Note: Regarding the use of shortcut keys, you can use the Command + Shift + A shortcut to open the Find Action window, input keywords to find corresponding functions and their shortcut keys, as shown in the above GIF.
Java 8 Support
3.0’s default support for Java 8 language features means you no longer need to add jackOptions to the build.gradle configuration file. If you have used it, remember to remove it:
android {
...
defaultConfig {
...
// Remove this block.
jackOptions {
enabled true
...
}
}
}
Select the toolbar File -> Project Structure, and change the Source Compatibility and Target Compatibility options to 1.8 to enable Java 8 language features in our project.
Android Profiler
The Android Monitor tool we used in the past has been replaced by Android Profiler starting from version 3.0. As shown in the picture, the run button now has an additional Android Profiler button:
Android Profiler provides three major debugging analysis tools: CPU, Memory, and Network, allowing real-time tracking of Apk’s running status, which can help us visualize performance tuning work.
These three tools are very useful during the development phase. For example, the CPU Profiler can analyze thread usage in the application, the Memory Profiler can detect memory leaks, and the Network Profiler can intercept network requests for packet capturing. Here are some images to preview the features of these three tools:
For detailed usage of these tools, you can refer to the official guides:
-
CPU Profiler guide
-
Memory Profiler guide
-
Network Profiler guide
Device File Explorer
This feature is impressive. The new development tools provide a resource manager that allows us to access the file system of connected devices, enabling convenient file transfer from PC to phone or emulator. With this tool, we can bid farewell to previously used third-party tools like “File Transfer Assistant”.
Adaptive Icons wizard
Select File -> New -> Image Asset, and the tool provided by Android Studio to create image resources has always been one of the development tools. Now, this tool starts supporting vector drawables format and can create desktop icons of various shapes for Android 8.0 systems.
URL intent-filter validator
This feature is quite thoughtful. The Manifest file now supports a special tag for testing whether a given URL string conforms to the rules defined by the intent-filter tag content. For example:
As shown in the picture, the data tag is used to define a URL rule, and the tools:validation tag is used to validate an incorrect test URL address, and the detector can automatically recognize and provide error prompts. This example is simple, and it’s easy to spot the error at a glance. But if it’s a more complex rule, it may not be so easy to see. At that time, this tool can come in handy.
APK Analyzer
Version 3.0 has further enhanced the functionality of the APK Analyzer. The main improvements include: for APK files that have been obfuscated using ProGuard, we can upload the mapping.txt file for code recovery; the Dex file provides a display of the size of each package directory; etc.
Other Features
In addition to the changes mentioned above, there are also other various changes. For example, the Layout Inspector and Layout Editor tools have UI adjustments, and the App Links Assistant and Firebase App Indexing Assistant have been enhanced for use. These are left for everyone to explore and discover. You can also visit the Android Studio Release Notes official website to learn more about the details of version 3.0.
Compatibility Adjustments for Old Projects
With the release of Android Studio 3.0, Android Plugin for Gradle 3.0.0 was also released simultaneously. The overall compilation speed has been greatly improved. After upgrading Android Studio, old projects need to make some changes to compile successfully with Gradle. Here, based on one of my actual projects, I will introduce the necessary changes. Of course, your project may not need these, and there may be more than these.
distributionUrl
Plugin 3.0.0+ requires the minimum version of Gradle to be 4.1. We can set the Android Plugin version through File -> Project Structure -> Project, or modify the contents of the gradle.properties file to add the distributionUrl property:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Google’s Maven repository
The new Android Studio tools default to using Google’s Maven Repository for downloading dependencies for the Android Support Library, replacing the local dependency method of the Android SDK Manager. Therefore, you need to add the line google() to the build.gradle file in the root directory of the project:
allprojects {
repositories {
google()
}
}
buildToolsVersion
Android Plugin For Gradle 3.0.0 automatically adds the minimum version of the build tools required by the plugin. Therefore, we no longer need to manually add a line of buildToolsVersion in build.gradle. For old projects, this line of configuration can be removed, for example:
android {
compileSdkVersion 26
// remote buildToolsVersion
buildToolsVersion "25.0.2"
...
}
each() and outputFile()
Plugin 3.0.0 has removed some APIs used for compilation configuration, among which the most common are each() and outputFile(), two methods commonly used to modify the output APK file name and path.
In a previous article Summary of Common Usage Scenarios of Android Gradle , it was mentioned that the custom output APK file name can be done like this:
android {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, rootProject.getName()
+ "-" + buildType.name
+ "-" + releaseTime()
+ "-v" + defaultConfig.versionName
+ "-" + defaultConfig.versionCode
+ ".apk");
}
}
}
However, using Plugin 3.0.0 will result in a compilation error. We need to modify each() and outputFile() methods to all() and outputFileName, for example:
android {
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = rootProject.getName()
+ "-" + buildType.name
+ "-" + releaseTime()
+ "-v" + defaultConfig.versionName
+ "-" + defaultConfig.versionCode
+ ".apk";
}
}
}
For old projects, these configuration adjustments are basically all that is needed. If you encounter any other issues, don’t panic; follow the compilation error prompts step by step, and you can also successfully compile. Overall, the release of Android Studio 3.0 and Plugin For Gradle 3.0.0 is definitely worth trying to upgrade, and there aren’t as many pitfalls as rumored online. After all, as programmers who write code, aren’t we here to solve problems?
Leave a Comment
Your email address will not be published. Required fields are marked *