포스트

Ktlint (정적 프로그램 분석)


Summary

  • lint : 코드를 분석하여, 프로그램 오류, 버그, 스타일 오류, 구조적 문제점을 확인하는 도구
    • 코딩 컨벤션에 따라 코드를 작성했는지 확인해주는 도구
    • ktlint: kotlin 개발 환경에서 사용되는 lint로, 공식 코틀린 코딩 컨벤션과 안드로이드 코틀린 스타일 가이드에 따라 만들어짐
    • android lint : 폴더 선택 > 마우스 오른쪽 버튼 > Analyze > Inspect

적용 방법

1.ktlint 공식 페이지에서 Installation 탭 클릭

1
2
3
4
링크 : [ktlint](https://pinterest.github.io/ktlint/install/overview/) 


![0](/assets/img/GBD40/0.png)

2.Integrations 클릭하면 gradle을 이용한 설치법이 나옴

1
![1](/assets/img/GBD40/1.png)

3.Android Studio에서 build.gradle (:app)에 아래 내용 추가 후 Sync now…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	configurations {
	    ktlint
	}
	
	dependencies {
	    ktlint("com.pinterest:ktlint:0.48.2") {
	        attributes {
	            attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
	        }
	    }
	    ...
	}
	
	
	task ktlint(type: JavaExec, group: "verification") {
	    description = "Check Kotlin code style."
	    classpath = configurations.ktlint
	    mainClass.set("com.pinterest.ktlint.Main")
	    args "src/**/*.kt"
	    // see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
	}
	check.dependsOn ktlint
	
	task ktlintFormat(type: JavaExec, group: "formatting") {
	    description = "Fix Kotlin code style deviations."
	    classpath = configurations.ktlint
	    mainClass.set("com.pinterest.ktlint.Main")
	    args "-F", "src/**/*.kt"
	    // see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information
	}

실행하기

build.gradle(:app)에 넣은 코드 중 task ktlinttask ktlintFormat 의 왼쪽에 재생버튼을 클릭해서 Run ‘앱 이름’을 클릭하면 검사를 해준다.

task ktlint는 검사만 해주는 것이고, task ktlintFormat은 검사 후 수정까지 해준다. 대신 task ktlintFormat후에도 남아있는 에러는 직접 수정해주어야한다.

2

검사 결과

성공

3

에러

4

에러가 난 경우에는 위에 사진처럼 발생이 난 코드 위치와 발생 이유를 알려준다.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.