Setting up Android Studio for your project will involve two tasks: (1) declaring inspections and (2) enabling Checkstyle.
The following items are similar to Eclipse's Save Actions.
- Open Studio and go to Preferences
- Go to Project Settings > Code Style > Java
- Choose the Imports tab and enable Insert imports for inner classes
- Back on the left pane, go to Project Settings > Inspections
- Search for Unused Import and enable the checkbox so they will be removed when saving files
Checkstyle for Android Studio comes in two parts (1) the IDE and (2) the gradle build. Checkstyle in the IDE only alerts you to errors and warnings, whereas the build task is the part that will actually fail the build so you know you have issues. Only the first dev to set up the project will need to add Checkstyle build support, but all devs will need to install the IDE support.
Install and setup the Checkstyle-IDEA plugin.
- Download and install the latest version of the plugin
- Download the plugin to disk
- Open Android Studio and go to preferences
- IDE Settings > Plugins > Install plugin from disk (select file you downloaded)
- Once plugin is installed, it will ask you to restart studio
- Copy checkstyle.xml file to root of your project
- Activate Checkstyle in Android Studio IDE during new project setup
- Open Studio and go back to Preferences
- Go to Project Settings > Inspections and then search for Checkstyle
- Change the real time scan to severity type Error
- Go to Project Settings > Checkstyle
- Add a new configuration file
- Name it "checkstyle.xml"
- Select the checkstyle.xml file in root of the project
- Press the Next button
- Enter the absolute path to the folder containing the
checkstyle-suppressions.xml
as the property for "proj.module.dir" - Choose OK and activate the new config
- Hit apply and then OK
-
Create a symlink to the checkstyle.xml from the project root that contains the file
mkdir -p config/checkstyle ln -s ../../checkstyle.xml config/checkstyle/checkstyle.xml
-
Activate Checkstyle in
build.gradle
by adding this at the bottom:apply from: 'https://raw.github.com/twotoasters/AndroidStyleGuidelines/master/checkstyle.gradle'
Install and setup the Checkstyle Eclipse plugins.
Update site: http://eclipse-cs.sourceforge.net/update/
- Install both plugings from the update site
- Copy checkstyle.xml file to root of your project
- Activate Checkstyle during new project setup
- Right click project > Properties then choose Checkstyle from left side
- Enable "Checkstyle active for this project"
- Enable "Use simple configuration"
- Switch to "Local Check Configuration" tab
- Add a New local configuration
- Set type: project relative configuration
- Set name: "checkstyle xml file"
- Set location: root of project
- Fix unresolved items
- Click additional properties button
- Click find unresolved properties button
- Declare "checkstyle.cache.file" as "bin/cachefile"
- Switch to "Main" tab
- Set the configuration to use to the "checkstyle xml file" local config
- Exclude files outside the source directories & from packages "gen"
Enable save actions in the global workspace preferences.
- Eclipse > Preferences
- Java > Editor > Save Actions
- Enable "Perform selected actions on save"
- Enable "Organize imports"
- Enable "Additional actions"
- Configure additional actions
- Enable the following:
- Remove trailing whitespace
- Remove unused imports
- Remove unnecessary casts
- Add missing annotations
- Add missing overrides
- Add missing deprecated
In the global workspace preferences, there are a few settings to check.
- Eclipse > Preferences
- General > Editors > Text Editors
- Set "Displayed tab width" to 4
- Enable "Insert spaces for tabs"
You can create a formatter profile based on your checkstyle settings.
- Right click your project name > Checkstyle > Create Formatter profile
- Choose workspace or project settings > Java > Code Style > Formatter
- Set the "Active Profile" to the profile for your project (e.g. eclipse-cs JazzyListView)