본문 바로가기

IT/안드로이드 관련

[안드로이드] 안드로이드 지문인식 변경점

안녕하세요 남갯입니다


블로그를 처음 글쓰면서 의미있는 글을 써보겠다 다짐하면서 


지문인식이란 구글샘플의 fingerprintCompat으로 타겟을 변경하고 제공하는 앱을 선보였는데요?


오랜만에 앱을 개발하면서 다시 예전에 만든 소스를 검토하면서 이용하다보니


api 28에서는 fingerprint가 deprecated되고 biomatric으로 통합되었다는 정보를 알게되어 글을 쓰게 되었습니다.



- fingerPrint의 변경점



Android 9 only includes fingerprint integration for BiometricPrompt. However, integrated support for other biometric modalities are forthcoming.

In Android 9 and higher, the FingerprintManager API is deprecated. If your bundled and system apps use this API, update them to use BiometricPrompt instead.

출처 : https://source.android.com/security/biometric



우리는 FingerPrint를

- isHardwareDetected() : 지문인식 장치가 있는지

hasEnrolledFingerprints() : 등록된 지문인식이 있는지

fingerprintManger(26이상 사용) or FingerprintManagerComapt(26이하도 사용) 를 통해 접근해야하는 방식이였습니다.

하지만 작성하던중 (targetSdk 28 기준) 실제로 위와같은 코드를 쓰기위해 작성하기위해 use_fingerprint의 권한을 쓰게되면 아래와 같이 deprecated 된것을 볼 수 있습니다.

그래서 검색을 해보니 하지만 P버전 부터는 BiometricPrompt로 통합되었다고 합니다.

Starting in Android P, developers can use the BiometricPrompt API to integrate biometric authentication into their apps in a device and biometric agnostic way.

출처 : https://android-developers.googleblog.com/2018/06/better-biometrics-in-android-p.html



api 28를 타겟으로 제공하는 앱의 경우에는 fingerprintManger or FingerprintManagerComapt을 바꿔BiometricPrompt로 이용해야 할것같습니다. 




BiometricPrompt의 문제점


여기서 저희는 또다른 문제가 발생합니다. BiometricPrompt는 29버전부터 출시되어 그 이하의 버전을 호환하려면 에러가나게됩니다. 

즉 28이하의 버전들은 이용을 할 수 없습니다. 이내 저는 버전에따라 소스를 분리하여 두번 작성하는 수고를 겪어야하나 싶었지만 검색 결과 구글에서 제공하는 androidx를 통해 해결이 가능합니다.

출처 : https://source.android.com/security/biometric



BiometricPrompt의 사용하기


먼져 androidx로 프로젝트가 생성되어 있지 않은분들은 androidx로 마이그레이션을 진행하셔야합니다.

androidx migration은 Refactor - migrate to androidx 를 통해 가능합니다.

 

※ 사용하고있는 thirdparty api가 androidx를 지원하지 않을경우 확인이 필요합니다.


그래들에 아래와 같이 추가합니다.


작성기준 버젼 '1.0.0-alpha03' 아직 정식버전이 아닌 alpha버젼 입니다.

// biometric
implementation "androidx.biometric:biometric:$bioMetricVersion"



BiometricPrompt에서 변경점은 아래와 같이 빌더를 통해 만들게되면 스마트폰에 따라 맞는 UI을 생성하여 알아서 보여주게됩니다. 


fun checkFingerprintAvailable(){
var biometricPromptInfo : BiometricPrompt.PromptInfo
biometricPromptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("finger")
.setDescription("Description")
.setSubtitle("subtitle")
.setNegativeButtonText("Cancel")
.build()
val authenticationCallback = getAuthenticationCallback()
val biometricPrompt : BiometricPrompt = BiometricPrompt(this, Executor { }, authenticationCallback)
biometricPrompt.authenticate(biometricPromptInfo)
}


위에서 작성한 BiometricPrompt에 에러나 성공에대한 callback에 대한 처리를 아래에서 하시면 됩니다.


private fun getAuthenticationCallback() = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
}
}


에러에나 처리에 대한 코드는 

https://developer.android.com/reference/android/hardware/biometrics/BiometricPrompt

여기서 보시면 됩니다






추가적으로 작성하고자 하는 activtiy에서 BiometricPrompt를 이용할때는 androidx에 있는걸 사용하셔야합니다.


되게 간단한 소스는 git에 올려두겠습니다.


https://github.com/namget/biomatric


감사합니다



--추가---


inner class MainThreadExecutor : Executor {
private val handler = Handler(Looper.getMainLooper())

override fun execute(runnable: Runnable) {
handler.post(runnable)
}
}

메인 루퍼를 이용해서 가져오게되면 callback을 타게됩니다