본문 바로가기

Android

[Android] ktlint 적용하기

프로젝트를 최초 생성하면서 ktlint를 적용해보려고 한다

ktlint는 kotlin 공식 codeing convention을 기준으로 code를 분석해주고, 올바르게 수정까지 도와주는 정적분석 도구이다.

여러가지 정적 분석 도구가 있는데 공식 style을 기본으로 제공하여 대세인듯 하여 ktlint를 먼저 적용해보겠다!

 

1. 공식홈페이지의 build.gradle 추가하기

https://ktlint.github.io/ 공식홈페이지에서 build.gradle 예시를 제공한다.

나의 경우 ktlint directory 를 생성하고 아래 gradle file을 생성했다. gradle file은 file을 생성하고 파일명을 명명할때 확장자를 gradle로 명기하면 생성된다.

 

2. 작성 gradle 

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
31
32
33
34
apply plugin: 'kotlin-android'
 
repositories {
    jcenter()
}
 
configurations {
    ktlint
}
 
dependencies {
    ktlint "com.pinterest:ktlint:0.41.0"
    // additional 3rd party ruleset(s) can be specified here
    // just add them to the classpath (ktlint 'groupId:artifactId:version') and
    // ktlint will pick them up
}
 
task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    main = "com.pinterest.ktlint.Main"
    classpath = configurations.ktlint
    args "src/**/*.kt"
    // to generate report in checkstyle format prepend following args:
    // "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
    // see https://github.com/pinterest/ktlint#usage for more
}
check.dependsOn ktlint
 
task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    main = "com.pinterest.ktlint.Main"
    classpath = configurations.ktlint
    args "-F""src/**/*.kt"
}
cs

 

3. 실행해보기 

잘 동작 하고, wild card 에 오류가 뜨는것을 확인 할 수 있다 프로젝트 최초 생성시 자동으로 생성되는 test file의 import문에서 발생하고 있는것이다.! 수정하고 다시 동작 시키니 잘 동작하는것을 확인할수있었다 .

  • task ktlint - code style검사
  • task ktlintFormat - 검사에서 나온 오류를 자동으로 수정

각각의 task를 따로 돌려보면 각 기능을 더 확실하게 확인 해볼 수 있다

 

3.5 terminal 에서 실행해보기

$ ./gradlew ktlint

 

./gradlew ktlint를 수행 시 오류가 발생했다...

../gradlew build 를 실행하지 않아 발생하는 오류인것 같았는데 해당 명령도 오류가 났다

 

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
> Task :app:lint FAILED
 
FAILURE: Build failed with an exception.
 
* What went wrong:
Execution failed for task ':app:lint'.
> Could not resolve all files for configuration ':app:lintClassPath'.
   > Could not resolve com.android.tools.lint:lint-gradle:27.1.2.
     Required by:
         project :app
      > Could not resolve com.android.tools.lint:lint-gradle:27.1.2.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/lint/lint-gradle/27.1.2/lint-gradle-27.1.2.pom'.
            > Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/lint/lint-gradle/27.1.2/lint-gradle-27.1.2.pom'.
               > PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      > Could not resolve com.android.tools.lint:lint-gradle:27.1.2.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/lint/lint-gradle/27.1.2/lint-gradle-27.1.2.pom'.
            > Could not GET 'https://jcenter.bintray.com/com/android/tools/lint/lint-gradle/27.1.2/lint-gradle-27.1.2.pom'.
               > PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
 
* Get more help at https://help.gradle.org
 
BUILD FAILED in 24s
52 actionable tasks: 52 executed
cs

PKIX가 인증서 문제라고 해서 인증서에 관한 문제를 해결을 해보자..!

 

SSL 인증서를 추출하는 InstallCert를 사용해보자

[출처] - https://www.lesstif.com/java/java-pkix-path-building-failed-98926844.html

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
31
32
33
34
35
36
37
38
// github에 올려가있는 소스 다운
curl -O https://gist.githubusercontent.com/lesstif/cd26f57b7cfd2cd55241b20e05b5cd93/raw/InstallCert.java
 
// 소스 컴파일
javac InstallCert.java
 
// installCert 를 연결하려는 사이트 이름과 함께 실행 
// 나의 경우 오류에서 발생하는 dl.google.com && jcenter.bintray.com
java -cp ./ InstallCert dl.google.com
 
/** 결과
Server sent 2 certificate(s):
 
 1 Subject CN=*.google.com, O=Google LLC, L=Mountain View, ST=California, C=US
   Issuer  CN=McAfee Web Gateway, OU=NSBU, O="McAfee, Inc.", L=Santa Clara, ST=CA, C=US
   sha1    8b 91 7a 40 de 9b 6e 5a 9e 7a e7 bf 49 10 b4 0b 1b 13 25 fa 
   md5     77 fa 29 90 4c 7b 29 4c 81 7c 72 1d cb e7 25 ad 
 
 2 Subject CN=McAfee Web Gateway, OU=NSBU, O="McAfee, Inc.", L=Santa Clara, ST=CA, C=US
   Issuer  CN=McAfee Web Gateway, OU=NSBU, O="McAfee, Inc.", L=Santa Clara, ST=CA, C=US
   sha1    85 01 b8 d0 08 a7 5e 72 0b fa 06 63 80 13 dc f4 1f 47 93 f9 
   md5     24 53 00 e4 7e bd 47 0b dd 1b ec e5 ae 7f 5f d1
 
Enter certificate to add to trusted keystore or 'q' to quit: [1]
*/
 
//아무거나 치고 나오면 
//Added certificate to keystore 'jssecacerts' using alias 'dl.google.com-2'
//해당 구문이 출력된다 이를 기억 
 
keytool -exportcert -keystore jssecacerts -storepass changeit -file output.cert -alias dl.google.com-2 
 
 
// jcenter에 관려애서도 같은 과정으로 실행한다 
java -cp ./ InstallCert jcenter.bintray.com
 
// Added certificate to keystore 'jssecacerts' using alias 'jcenter.bintray.com-2'
keytool -exportcert -keystore jssecacerts -storepass changeit -file output.cert -alias jcenter.bintray.com-2 
cs

 

잘 추가를 했는데 동작하지 않았다 .. 뜨든..!!

결과적으로

1
$ chmod +x gradlew
cs

해당 구문을 실행한 후 ./gradlew ktlint 를 수행하였더니 터미널에서도 정상동작을 확인 할 수 있었다.

 

끝..!