[Android] kotlinとPermissionDispatcherを使うときの注意点
![](https://blog.mogmet.com/wp-content/uploads/2017/06/kotlin.png)
PermissionDispatcherをkotlinで使おうとしていたのですが、なぜかコードが自動生成されなかったので、その対策メモ
調べたところ、2017-09-14時点で、kotlin対応はまだ正式リリースされていないようで、ベータ版ではあるが、3.0.0を使うようにしないと行けない模様。
cf:
上記資料より、build.gradle(app)に下記のように記載が必要の模様
1 2 3 4 5 6 7 8 |
repositories { jcenter() maven { url "http://oss.jfrog.org/artifactory/oss-snapshot-local/" } } dependencies { compile("com.github.hotchemi:permissionsdispatcher:3.0.0-SNAPSHOT") kapt "com.github.hotchemi:permissionsdispatcher-processor:3.0.0-SNAPSHOT" } } |
さらに、コードにはjavaと違い以下を記載する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@RuntimePermissions(kotlin = true) // kotlin=trueをいれる class MainActivity: AppCompatActivity() { ... override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) onRequestPermissionsResult(requestCode, grantResults) // kotlinの拡張関数で定義されているため、javaのように頭にMainActivity.onRequestPermissionsResultとする必要はない } @NeedsPermission(Manifest.permission.CAMERA) inline fun showCamera() { // inlineもしくはinternalで定義する必要あり。internalのほうがthisが参照できるので使いやすい // カメラ起動てきな処理 } // 例えばボタンをおされたらカメラ起動などの起動の仕方 fun onClickButton() { showCameraWithCheck() // @NeedsPermissionに定義したメソッド名+WithCheck()で呼び出し。 } } |
他の実装部分
細かい実装は以下が参考になった
GitHub
![](https://blog.mogmet.com/wp-content/uploads/pz-linkcard/cache/0b7df611b9a9627ea823fdc435b8f3d89fd2833433fc84c5b7837d39af71650d.jpeg)
Build software better, together
GitHub is where people build software. More than 73 million people use GitHub to discover, fork, and contribute to over 200 million projects.
追記
近々正式リリースされるので、kotlinにてPermissionsDispatcherをprodで使うのは少し待ちましょう
ご利用ありがとうございます!3.0.0はSNAPSHOTでまだ品質を保証できないのでprodで使わない事をおすすめします(今週末に正式版をリリース予定です)!2系でもkaptと合わせて使う事は可能です!
— hotchemi (@hotchemi) 2017年9月14日
追記その2
正式版リリースされたようなので、以下の記載でイケルと思います。
1 2 3 4 |
dependencies { compile("com.github.hotchemi:permissionsdispatcher:3.0.1") annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.0.1" } |