既存AndroidアプリでKotlin導入を考える

 既存AndroidアプリでKotlin導入を考える

関モバ #8での発表

002c7b95e7b8f34186ffd6abaa136a99?s=128

Naoki Morioka

December 07, 2015
Tweet

Transcript

  1. طଘAndroidΞϓϦͰ KotlinಋೖΛߟ͑Δ @n_morioka ؔ੢ϞόΠϧΞϓϦݚڀձ#8 2015/11/30

  2. About me • ੝Ԭ ঘه(@n_morioka) • ϑϦϡʔגࣜձࣾͰΞϓϦ։ൃͯ͠·͢ (Android, iOS,αʔόαΠυJava etc..)

    • σʔλղੳ΋΍ͬͯΔΑ(fluentd/Redshift/ BigQuery/tableau)
  3. • ϓϦ࿈ಈSNSαʔϏε • iOS/AndroidΞϓϦ ྦྷܭ250ສDLಥഁ

  4. ࠓճ͸طଘAndroidΞϓϦΛKotlin ʹ͢΂͘ฃಆͨ݁͠ՌͰ͋Γ Kotlinຊମͷྑ͞Λ෍ڭ͢ΔͨΊͷ ΋ͷͰ͸͋Γ·ͤΜ

  5. Kotlin • https://kotlinlang.org/ • JVM্Ͱಈ͘੩తܕ෇͚ݴޠɻ࠷ۙͰ͸ AndridΛϝΠϯλʔήοτʹ͍ͯ͠Δ໛༷ • ίϯύΠϧޙͷJavaόΠτίʔυ͸JDK 6ޓ׵ •

    swift ϥΠΫ(swiftͷํ͕ઌʹͰ͖͍ͯͨʂ)
  6. roadmap • May 29, 2015 M12 release • September 16,

    2015 M13 release • October 1, 2015 M14 release • October 22, 2015 Beta Candidate relase • November 1, 2015 1.0 Beta release • November 16, 2015 1.0 Beta2 release
  7. ߴ·Δ1.0ϦϦʔεײ

  8. Step1 Java → Kotlin

  9. • ϢʔςΟϦςΟΫϥεͳͲͷଞͷϥΠϒϥϦ΁ͷ ґଘͷগͳ͍ΫϥεΛJavaԽͯ͠ΈΔ • AndroidStudio͕େମྑ͍ײ͡Ͱม׵ͯ͘͠ΕΔ • Java8 + retrolambdaͷίʔυͰ΋Ұׅม׵Մೳ

  10. None
  11. Java Code import android.content.Context;
 import android.graphics.Color;
 import android.support.annotation.ColorRes;
 import android.support.annotation.IntRange;


    
 public class ColorUtils {
 
 public static int resourceColorAlpha(Context context, @ColorRes int colorResource, @IntRange(from = 0, to = 255) int alphaDecimal) {
 int color = context.getResources().getColor(colorResource);
 int red = Color.red(color);
 int green = Color.green(color);
 int blue = Color.blue(color);
 return Color.parseColor("#" + Integer.toHexString(Color.argb(alphaDecimal, red, green, blue)));
 }
 }
  12. Kotlin Code import android.content.Context
 import android.graphics.Color
 import android.support.annotation.ColorRes
 import android.support.annotation.IntRange


    
 object ColorUtils {
 fun resourceColorAlpha(context: Context, @ColorRes colorResource: Int, @IntRange(from = 0, to = 255) alphaDecimal: Int): Int {
 val color = context.resources.getColor(colorResource)
 val red = Color.red(color)
 val green = Color.green(color)
 val blue = Color.blue(color)
 return Color.parseColor("#" + Integer.toHexString(Color.argb(alphaDecimal, red, green, blue)))
 }
 }
  13. ͋Μ·ΓKotlinײ͕ग़ͯͳ͍ Ͱ͕͢ KotlinʹͳΔ͜ͱ͕ॏཁͰ͢

  14. Step2 Dagger2

  15. • ରԠՄೳ • aptͷkotlin൛kaptΛ࢖͏

  16. kapt • JSR 269 Annotation Processing ͷkotlin൛ Kotlin Annotaion Processing

    Tool • Ξϊςʔγϣϯهड़͔ΒJavaίʔυΛੜ੒͢ Δ࢓૊Έ • Kotlin M12 ~
  17. build.gradle dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'


    compile 'com.android.support:appcompat-v7:23.1.1'
 compile 'com.android.support:design:23.1.1'
 
 // Kotlin
 compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.0- beta-2423'
 
 //Dagger2
 compile 'com.google.dagger:dagger:2.0.2'
 kapt 'com.google.dagger:dagger-compiler:2.0.2'
 provided 'org.glassfish:javax.annotation:10.0-b28'
 }
  18. sample code import dagger.Component
 
 @Component(modules = arrayOf(DebugModule::class))
 interface ApplicationComponent

    {
 fun logHandler(): LogHandler
 }

  19. None
  20. ಉ༷ʹRealm΋ҠߦՄೳͳ͸ͣ (realm-javaͷkotlinExample͸ಈ ࡞֬ೝࡁΈ)

  21. Step3 AndroidAnnotations

  22. ແཧͬΆ͍ orz..

  23. ΍ͬͨ͜ͱ • ී௨ʹGet startedυΩϡϝϯτ௨Γ࣮ࢪͭͭ͠ apt→kaptԽ • kaptઃఆ಺ͷvariantͷѻ͍͕͏·͘ߦ͔ͣΤϥʔ • variantΛར༻͠ͳ͍ͱAndroidManifest File͕ݟ͔ͭ

    Βͳͯ͘Τϥʔ • AndroidAnnotations 4.0-SNAPSHOTͰ΋ແཧ
  24. ·ͱΊ • KotlinΛར༻͢Δোน͸େ෼௿͘ͳ͍ͬͯΔ • View Binding͸ૉ௚ʹKotter KnifeΛ࢖͍͍ͨ • AndroidAnnotationsΛ৽نϓϩδΣΫτͰར ༻͢Δ৔߹ʹ͸ϦεΫΛߟ͑Α͏

  25. ࣮ࡍҠߦ͢ΔͳΒ(Ҋ) • Android Annotationsͷར༻Λ·ͣ΍ΊΔ • طଘϓϩδΣΫτͷҰ෦ΛKotlinʹͯ͠νʔϜͰ Kotlinʹ׳ΕΔɺྑ͞Λ࣮ײ͢Δ • ϨΠϠʔ͝ͱʹҠߦΛ࣮ࢪɻDagger2/RealmΛར ༻͢Δ৔߹͸৽نϓϩδΣΫτΛ࡞੒ͯ͠ߏங͠

    ͨํ͕ϕλʔ
  26. Enjoy Development ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ