2020年2月26日水曜日

moshi-kotlin (1.9.2) の proguard 設定ではまった

moshi の proguard 設定は https://github.com/square/moshi#r8--proguard にあります。 この設定だけだと

java.lang.IllegalArgumentException: Cannot serialize Kotlin type XX. Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.

というエラーが出て parse に失敗しました。

ちゃんと以下のように KotlinJsonAdapterFactory をセットしているので、関係ないエラー文言であり紛らわしいです。
  1. val moshi = Moshi.Builder()  
  2.     .add(KotlinJsonAdapterFactory())  
  3.     .build()  


問題は moshi-kotlin を使うときに追加する proguard 設定にあります。
https://github.com/square/moshi/blob/master/kotlin/reflect/src/main/resources/META-INF/proguard/moshi-kotlin.pro
は 1.9.2 の時点で
  1. -keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl  
  2.   
  3. -keepclassmembers class kotlin.Metadata {  
  4.     public <methods>;  
  5. }  
になっていますが、これを
  1. -keep class kotlin.reflect.jvm.internal.impl.builtins.BuiltInsLoaderImpl  
  2.   
  3. -keep class kotlin.Metadata {  
  4.     public <methods>;  
  5. }  
にすれば正しく parse されるようになりました。


2020年2月16日日曜日

突如 Gmail が ACTION_SENDTO での EXTRA_TEXT を fill しなくなったので対応した

以前のコード
  1. val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:$address")  
  2.     .putExtra(Intent.EXTRA_SUBJECT, subject)  
  3.     .putExtra(Intent.EXTRA_TEXT, text)  
  4.   
  5. startActivity(intent)  
対応したコード
  1. val intent = Intent(Intent.ACTION_SEND) // Intent.ACTION_SENDTO でもいけた  
  2.     .putExtra(Intent.EXTRA_EMAIL, arrayOf(address))  
  3.     .putExtra(Intent.EXTRA_SUBJECT, subject)  
  4.     .putExtra(Intent.EXTRA_TEXT, text)  
  5.     .apply {  
  6.         selector = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))  
  7.     }  
  8.   
  9. startActivity(intent)  



ありがとう https://stackoverflow.com/questions/59836984/email-body-empty-when-select-to-send-email-by-gmail


2020年2月7日金曜日

moshi で List や Map の Generics Type を指定する

いつも忘れるので
  1. val type = Types.newParameterizedType(  
  2.     Map::class.java,  
  3.     Hoge::class.java,  
  4.     Fuga::class.java  
  5. )  
  6. val adapter: JsonAdapter<Map<Hoge, Fuga>> = moshi.adapter(type)