-
Notifications
You must be signed in to change notification settings - Fork 521
Ktlint Guide
Once you have completed all the installation steps, you will have a ktlint
file in your opensource/oppia-android-tools
folder.
Note: Keep your terminal or command line to opensource/oppia-android
path while running below commands.
-
Check the version of your ktlint. As of now on GitHub Actions, we are using
0.37.1
.
../oppia-android-tools/ktlint --version
-
Android Kotlin Style Guide using
--android
../oppia-android-tools/ktlint --android "path/of/your/kotlin/file"
- Examples
- Specific File
../oppia-android-tools/ktlint --android app/src/sharedTest/java/org/oppia/android/app/walkthrough/WalkthroughWelcomeFragmentTest.kt
- Specific Directory
../oppia-android-tools/ktlint --android "utility/src/**/*.kt"
- Specific File
- Examples
-
Android Kotlin Style Guide using
--android
with Ktlint Auto Formatter-F
. This will fix some of the issues automatically.
../oppia-android-tools/ktlint -F --android "path/of/your/kotlin/file"
- Examples
- Specific File
../oppia-android-tools/ktlint -F --android app/src/sharedTest/java/org/oppia/android/app/walkthrough/WalkthroughWelcomeFragmentTest.kt
- Specific Directory
../oppia-android-tools/ktlint -F --android "utility/src/**/*.kt"
- Specific File
- Examples
In Android Studio we have a feature called Macros which helps you convert multiple tasks in one shortcut.
There are two major tasks when we talk about style formatting, One is the Reformat Code
and another one is Optimize imports
.
-
Reformat Code
- this will reformat your code with proper indentation and newlines wherever needed. -
Optimize imports
- this will remove all the unused import from your file. Also, this will rearrange your imports in lexicographical order and all those imports which are starting withjava
,javax
orKotlin
will get shifted at the end of the import list which is a correct order.
- Double shift
- Search for "macros"
- Click on "Macros"
- Click on "start macro reading"
- Menu Toolbar -> Code -> Reformat Code -> Menu Toolbar -> Code -> Optimize Import -> click on stop macro reading at bottom
- Now you can give whatever shortcut you want and these above two steps will get performed
-
Wildcard Imports
- If you had imported anything which directs to just the packageimport java.util.*
, it will give you an error saying there should not be any wildcard imports. So, you had to use the path completely for what you need in your file.- Example -
import java.util.Date
orimport java.util.Locale
- Example -
-
Exceeding 100 char limit
- This means that there is a line of code which is more than 100 char in one line. You must have noticed a grey line in the editor area, the code should not cross that line.There are some cases like the name of the tests where the code crosses that line and we cannot rearrange it as it is the name of the function. This does not apply to comments. There you should put a ktlint comment using which ktlint disable the check for 100 char limit.
// ktlint-disable max-line-length
- Example -
fun testWalkthroughWelcomeFragment_recyclerViewIndex1_topicSelected_clickNoButton_worksCorrectly() { // ktlint-disable max-line-length
- Example -
Note: We don't recommend the use of // ktlint-disable max-line-length
to disable the ktlint check unless the line of code cannot be broken into multiple lines to prevent disabling ktlint check.
Have an idea for how to improve the wiki? Please help make our documentation better by following our instructions for contributing to the wiki.
Core documentation
Developing Oppia
- Contributing to Oppia Android
- Bazel
- Key Workflows
- Testing
- Developing Skills
- Frequent Errors and Solutions
- RTL Guidelines
- Working on UI
- Writing Design Docs
Developer Reference
- Code style
- Background Processing
- Dark mode
- Buf Guide
- Firebase Console Guide
- Platform Parameters & Feature Flags
- Work Manager
- Dependency Injection with Dagger
- Revert & regression policy
- Upgrading target SDK version
- Spotlight Guide
- Triaging Process
- Bazel
- Internationalization
- Terminology in Oppia
- Past Events