Slide 1

Slide 1 text

,PUMJOΒ͍͠ίʔυΛॻ͜͏ $POWFSU+BWB'JMFUP,PUMJO'JMFͷ ͋ͱʹ΍Δ͜ͱ :VLJ"O[BJ !ZBO[N ,PUJO'FTU

Slide 2

Slide 2 text

+BWBˠ,PUMJOͷ ϨϏϡʔ͠΍͍͢HJUMPH

Slide 3

Slide 3 text

DPNNJUͰLPUMJOԽͨ͠13

Slide 4

Slide 4 text

LPUMJOԽલʹSFOBNFͷDPNNJUΛೖΕΔ % mv DetailHeaderView.java DetailHeaderView.kt % git add -A % git commit -m 'rename DetailHeaderView.java to DetailHeaderView.kt' … % mv DetailHeaderView.kt DetailHeaderView.java ͜ͷޙʹ Convert Java File to Kotlin File ͢Δ

Slide 5

Slide 5 text

LUʹSFOBNF͔ͯ͠ΒLPUMJOԽͨ͠13

Slide 6

Slide 6 text

LUʹSFOBNF͔ͯ͠ΒLPUMJOԽͨ͠13

Slide 7

Slide 7 text

LUʹSFOBNF͔ͯ͠ΒLPUMJOԽͨ͠13

Slide 8

Slide 8 text

LUʹSFOBNF͔ͯ͠ΒLPUMJOԽͨ͠13

Slide 9

Slide 9 text

$POWFSU+BWB'JMFUP,PUMJO 'JMFͷ͋ͱʹ΍Δ͜ͱ

Slide 10

Slide 10 text

Λͳ͘͢ public void addAll(List newItems) { items.addAll(newItems); } Java fun addAll(newItems: List?) { items.addAll(newItems!!) } Kotlin (Auto Convert) fun addAll(newItems: List) { items.addAll(newItems) } Kotlin

Slide 11

Slide 11 text

ෆཁͳ Λͳ͘͢ public static DetailFragment createInstance(String title) { DetailFragment fragment = new DetailFragment(); Bundle args = new Bundle(); args.putString(DetailFragment.ARGS_TITLE, title); fragment.setArguments(args); return fragment; } Java public void putString(@Nullable String key, @Nullable String value) { unparcel(); mMap.put(key, value); }

Slide 12

Slide 12 text

Kotlin (Auto Convert) ෆཁͳ Λͳ͘͢ fun createInstance(title: String?): DetailFragment { val fragment = DetailFragment() val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args return fragment }

Slide 13

Slide 13 text

ෆཁͳ Λͳ͘͢ fun createInstance(title: String): DetailFragment { val fragment = DetailFragment() val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args return fragment } Kotlin

Slide 14

Slide 14 text

ෆཁͳ ͕ೖΓ΍͍͢ͱ͜Ζ w Ҿ਺ͷܕ w ໭Γ஋ͷܕ w ܕύϥϝʔλ w ྫʣ-JTU4USJOH

Slide 15

Slide 15 text

MBUFJOJUWBS Kotlin (Auto Convert) class ViewHolder { private var titleView: TextView? = null fun init(view: View) { titleView = view.findViewById(R.id.title_view) } fun setTitle(title: String?) { titleView!!.text = title } }

Slide 16

Slide 16 text

MBUFJOJUWBS class ViewHolder { private lateinit var titleView: TextView fun init(view: View) { titleView = view.findViewById(R.id.title_view) } fun setTitle(title: String) { titleView.text = title } } Kotlin

Slide 17

Slide 17 text

WBSΛͰ͖Δ͔͗Γ΍ΊΔ int sum = 0; for (Item item : items) { sum += item.getPrice(); } Java var sum = 0 for (item in items) { sum += item.price } Kotlin (Auto Convert) val sum = items.sumOf { it.price } Kotlin

Slide 18

Slide 18 text

LPUMJOTUEMJCΛ࢖͏ w ͍ΖΜͳศརؔ਺͕͋ΔͷͰࣗ෼Ͱ࣮૷͢Δલʹௐ΂Α͏ w fi MUFS fi MUFS/PU/VMM NBQ NBQ/PU/VMM w fl BU.BQ JG&NQUZ w HFU0S/VMM w EJTUJODU EJTUJODU#Z w DPFSDF"U.PTU DPFSDF"U-FBTU DPFSDF*O w BT4FRVFODF

Slide 19

Slide 19 text

MFU Λ࢖͏ LogParams log; if (title == null) { log = null; } else { log = new LogParams(title); } Java val log = if (title == null) { null } else { LogParams(title) } Kotlin (Auto Convert) val log = title?.let { LogParams(it) } Kotlin

Slide 20

Slide 20 text

BQQMZ BMTP TextView titleView = view.findViewById(R.id.title_view); titleView.setTextSize(16); titleView.setTextColor(getResources().getColor(R.color.title)); titleView.setText(title); Java val titleView = view.findViewById(R.id.title_view) titleView.textSize = 16f titleView.setTextColor(resources.getColor(R.color.title)) titleView.text = title Kotlin (Auto Convert)

Slide 21

Slide 21 text

BQQMZ BMTP val titleView = view.findViewById(R.id.title_view) titleView.textSize = 16f titleView.setTextColor(resources.getColor(R.color.title)) titleView.text = title Kotlin (Auto Convert) view.findViewById(R.id.title_view).apply { textSize = 16f setTextColor(resources.getColor(R.color.title)) text = title } Kotlin

Slide 22

Slide 22 text

@Λ࢖͏ okButton.setListener((v, log) -> onClickOk(log)); Java Kotlin (Auto Convert) okButton.setListener { v: View?, log: String -> onClickOk(log) } okButton.setListener { _, log -> onClickOk(log) } Kotlin

Slide 23

Slide 23 text

֦ுؔ਺Λ࢖͏ fun convert(value: A): B { … } Kotlin ͷ৔߹ val b = a?.let { convert(it) } fun A.toB(): B { … } ͷ৔߹ val b = a?.toB()

Slide 24

Slide 24 text

4NBSUDBTU if (parentFragment instanceof Listener) { ((Listener) parentFragment).onUpdate(title); } Java if (parentFragment is Listener) { (parentFragment as Listener).onUpdate(title) } Kotlin (Auto Convert) if (parentFragment is Listener) { parentFragment.onUpdate(title) } Kotlin

Slide 25

Slide 25 text

BT if (parentFragment instanceof Listener) { ((Listener) parentFragment).onUpdate(title); } Java if (parentFragment is Listener) { (parentFragment as Listener).onUpdate(title) } Kotlin (Auto Convert) (parentFragment as? Listener)?.onUpdate(title) Kotlin

Slide 26

Slide 26 text

XIFO public static ImageUrl create( String light, String dark ) { if (light == null) { return null; } else if (dark == null) { return null; } else { return new ImageUrl(light, dark); } } Java Kotlin (Auto Convert) companion object { fun create( light: String?, dark: String? ): ImageUrl? { return if (light == null) { null } else if (dark == null) { null } else { ImageUrl(light, dark) } } }

Slide 27

Slide 27 text

Kotlin (Auto Convert) XIFO companion object { fun create( light: String?, dark: String? ): ImageUrl? { return if (light == null) { null } else if (dark == null) { null } else { ImageUrl(light, dark) } } } companion object { fun create( light: String?, dark: String? ): ImageUrl? { return when { light == null -> null dark == null -> null else -> ImageUrl(light, dark) } } } Kotlin

Slide 28

Slide 28 text

ؔ਺ࢀর cancelButton.setListener(() -> { onClickCancel(); return Unit.INSTANCE; }); Java cancelButton.setListener { onClickCancel() Unit } Kotlin (Auto Convert) cancelButton.setListener(::onClickCancel) Kotlin fun setListener(listener: () -> Unit) { this.listener = listener }

Slide 29

Slide 29 text

$PMMFDUJPOܥ

Slide 30

Slide 30 text

Ͱ͖Δ͚ͩ*NNVUBCMF$PMMFDUJPOʹ͢Δ List log = new ArrayList<>(); for(int i = 0; i < items.size(); i++){ Item item = items.get(i); log.add(i + ": " +item.getTitle()); } Java val log: MutableList = ArrayList() for (i in items.indices) { val item = items[i] log.add(i.toString() + ": " + item.title) } Kotlin (Auto Convert)

Slide 31

Slide 31 text

Ͱ͖Δ͚ͩ*NNVUBCMF$PMMFDUJPOʹ͢Δ val log: MutableList = ArrayList() for (i in items.indices) { val item = items[i] log.add(i.toString() + ": " + item.title) } Kotlin (Auto Convert) val log = items.mapIndexed { i, item -> "$i: ${item.title}" } Kotlin

Slide 32

Slide 32 text

.VUBCMF$PMMFDUJPOΛެ։͠ͳ͍ public class DataHolder { public final List items = new ArrayList<>(); public void addAll(List newItems) { items.addAll(newItems); } Java class DataHolder { val items: MutableList = ArrayList() fun addAll(newItems: List?) { items.addAll(newItems!!) } Kotlin (Auto Convert)

Slide 33

Slide 33 text

.VUBCMF$PMMFDUJPOΛެ։͠ͳ͍ class DataHolder { private val _items: MutableList = ArrayList() val items: List get() = _items fun addAll(newItems: List) { _items.addAll(newItems) } } Kotlin

Slide 34

Slide 34 text

"SSBZ-JTU ˠNVUBCMF-JTU0G public class DetailFragment extends Fragment { private List items = new ArrayList<>(); void addItem(String item) { items.add(item); } Java class DetailFragment : Fragment() { private val items: MutableList = ArrayList() fun addItem(item: String) { items.add(item) } Kotlin (Auto Convert)

Slide 35

Slide 35 text

"SSBZ-JTU ˠNVUBCMF-JTU0G class DetailFragment : Fragment() { private val items: MutableList = ArrayList() fun addItem(item: String) { items.add(item) } Kotlin (Auto Convert) class DetailFragment : Fragment() { private val items = mutableListOf() fun addItem(item: String) { items.add(item) } Kotlin HashMap, LinkedHashMap → mutableMapOf() HashSet, LinkedHashSet → mutableSetOf()

Slide 36

Slide 36 text

CVJME-JTU\^ CVJME.BQ\^ ArrayList params = new ArrayList<>(); if (!hasParamA) { params.add(PARAM_A); } else { if (hasParamB) { params.add(PARAM_B); } else { params.add(PARAM_C); } } Java val params = ArrayList() if (!hasParamA) { params.add(PARAM_A) } else { if (hasParamB) { params.add(PARAM_B) } else { params.add(PARAM_C) } } Kotlin (Auto Convert)

Slide 37

Slide 37 text

CVJME-JTU\^ CVJME.BQ\^ val params = ArrayList() if (!hasParamA) { params.add(PARAM_A) } else { if (hasParamB) { params.add(PARAM_B) } else { params.add(PARAM_C) } } Kotlin (Auto Convert) val params = buildList { if (!hasParamA) { add(PARAM_A) } else { if (hasParamB) { add(PARAM_B) } else { add(PARAM_C) } } } Kotlin Immutable

Slide 38

Slide 38 text

"OESPJEؔ࿈

Slide 39

Slide 39 text

CVOEMF0G Bundle args = new Bundle(); args.putString(DetailFragment.ARGS_TITLE, title); fragment.setArguments(args); Java val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args Kotlin (Auto Convert) fragment.arguments = bundleOf( ARGS_TITLE to title ) Kotlin

Slide 40

Slide 40 text

5FYU6UJMTJT&NQUZ ˠJT/VMM0S&NQUZ val title = args.getString(ARGS_TITLE) if (!TextUtils.isEmpty(title)) { … } Kotlin (Auto Convert) val title = args.getString(ARGS_TITLE) if (!title.isNullOrEmpty()) { … } Kotlin String title = args.getString(ARGS_TITLE); if (!TextUtils.isEmpty(title)) { … } Java

Slide 41

Slide 41 text

5FYU6UJMTFRVBMT ˠ val title = args.getString(ARGS_TITLE) if (TextUtils.equals(title, current)) { … } Kotlin (Auto Convert) val title = args.getString(ARGS_TITLE) if (title == current) { … } Kotlin String title = args.getString(ARGS_TITLE); if (TextUtils.equals(title, current)) { … } Java

Slide 42

Slide 42 text

CZWJFX.PEFMT private DetailViewModel viewModel; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); viewModel = new ViewModelProvider(this).get(DetailViewModel.class); Java private var viewModel: DetailViewModel? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = ViewModelProvider(this).get(DetailViewModel::class.java) Kotlin (Auto Convert)

Slide 43

Slide 43 text

CZWJFX.PEFMT private var viewModel: DetailViewModel? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = ViewModelProvider(this).get(DetailViewModel::class.java) Kotlin (Auto Convert) private val viewModel: DetailViewModel by viewModels() Kotlin

Slide 44

Slide 44 text

MBUFJOJUWBSGPSˏ*OKFDU @AndroidEntryPoint public class DetailFragment extends Fragment { @Inject DetailLogger logger; Java @AndroidEntryPoint class DetailFragment : Fragment() { @Inject var logger: DetailLogger? = null Kotlin (Auto Convert) @AndroidEntryPoint class DetailFragment : Fragment() { @Inject lateinit var logger: DetailLogger Kotlin

Slide 45

Slide 45 text

νΣοΫϦετ w ͸࢒͍ͬͯͳ͍͔ w ෆཁͳ ͸࢒͍ͬͯͳ͍͔ w MBUFJOJUWBSʹͰ͖Δͱ͜Ζ͸ͳ͍͔ w WBMʹͰ͖ΔWBS͸࢒͍ͬͯͳ͍͔ w ࢖͑ΔTUEMJC͸ͳ͍͔ w MFU Ͱ؆ܿʹͰ͖Δͱ͜Ζ͸ͳ͍͔ w BQQMZ BMTPͰ·ͱΊΒΕΔॳظԽॲཧ͸ͳ͍͔

Slide 46

Slide 46 text

νΣοΫϦετ w @ʹϦωʔϜͰ͖Δͱ͜Ζ͸ͳ͍͔ w 4NBSUDBTUʹΑͬͯDBTU͕ෆཁʹͳ͍ͬͯΔͱ͜Ζ͸ͳ͍͔ w XIFOΛ࢖ͬͨ΄͏͕؆ܿʹͳΔͱ͜Ζ͸ͳ͍͔ w ؔ਺ࢀর͕͔ͭ͑Δͱ͜Ζ͸ͳ͍͔ w *NNVUBCMFʹͰ͖Δ.VUBCMFDPMMFDUJPO͕࢒ͬͯͳ͍͔ w .VUBCMFDPMMFDUJPOΛެ։͍ͯ͠ͳ͍͔

Slide 47

Slide 47 text

νΣοΫϦετGPS"OESPJE w CVOEMF0G͕࢖͑Δͱ͜Ζ͸ͳ͍͔ w 5FYU6UJMTJT&NQUZ ͕࢒ͬͯͳ͍͔ w 5FYU6UJMTFRVBMT ͕࢒ͬͯͳ͍͔ w CZWJFX.PEFMT Λ࢖͍ͬͯΔ͔ w !*OKFDU͕͍ͭͨ fi FME͸MBUFJOJUWBSʹͳ͍ͬͯΔ͔ɹ