対象èªè
- Androidã¢ããªéçºè
3.0ãªãªã¼ã¹
ãAndroid Studioã®2.0ããªãªã¼ã¹ãããã®ãã2016å¹´4æ7æ¥ï¼ç¾å°æéï¼ã§ããä»å1å¹´åã¶ãã®ã¡ã¸ã£ã¼ã¢ãããã¼ãã§ãã3.0ãã2017å¹´10æ25æ¥ï¼ç¾å°æéï¼ã«ãªãªã¼ã¹ããã¾ããããã®CodeZineã§ããéå ±ã¨ãã¦ãã¡ãã®è¨äºãå ¬éããã¦ãã¾ãããã®è¨äºã«è¨è¼ããã¦ããéãã注ç®ãã¹ãæ©è½ãããã¤ãããã¾ããããã§ã¯ããã®ãã¡ä»¥ä¸ã®å 容ãããå°ã詳ããç´¹ä»ãã¦ããã¾ãã
- Kotlinãµãã¼ã
- æ°ããJavaã®æ¸ãæ¹ããµãã¼ã
- Layout Inspector
- XMLãã©ã³ãã¨ãã¦ã³ãã¼ããã«ãã©ã³ã
- Android Profiler
- Instant Appsãµãã¼ã
- Android Emulator Quick Boot
Kotlinãµãã¼ã
ãä»åã®ã¢ãããã¼ãã§æã注ç®ããã¦ããã®ããã®Kotlinè¨èªã®æ£å¼ãµãã¼ãã§ãã
ãKotlinã¯JVMè¨èªã®ã²ã¨ã¤ã§ãAndroid Studioã®ãã¼ã¹ã¨ãªã£ã¦ããIntelliJ IDEAãéçºãã¦ããJetBrains社ãéçºããè¨èªã§ããJavaã¨ã®äºææ§ãç¶æãã¤ã¤ãJavaãããç°¡æ½ã«è¨è¿°ã§ãããã¨ãç®æãã¦éçºããã¦ãã¾ãããã®Kotlinã§Androidã¢ããªãä½æãããã¨ããå ´åãä»ã¾ã§ã¯ãã©ã°ã¤ã³ãã¤ã³ã¹ãã¼ã«ãã¦ä½¿ã£ã¦ãã¾ãããä»åã®ã¢ãããã¼ãã§ããã®Kotlinãæ£å¼ãµãã¼ããããã¨ãããã¨ã¯ãJava以å¤ã§å¯¾çã®ç«å ´ã¨ãã¦ãAndroidã¢ããªéçºã®ããã®è¨èªãããã²ã¨ã¤è¿½å ããããã¨ã«ä»ãªãã¾ããã
ããã¦ãæ©éãã®Kotlinã§ããã¸ã§ã¯ããä½æããæ¹æ³ãè¦ã¦ããã¾ããããã¨ãã£ã¦ãé常ã«ç°¡åã§ããAndroid Studio 3ã§æ°è¦ããã¸ã§ã¯ãä½æã¦ã£ã¶ã¼ããèµ·åããã¨ã第1ç»é¢ã¯å³1ã®éãã«ãªã£ã¦ãã¾ãã
ãç»é¢ä¸é¨ã«ï¼»Include Kotlin supportï¼½ã®ãã§ãã¯ããã¯ã¹ãããã¾ããããã«ãã§ãã¯ãå ¥ããã ãã§ãããã¨ã¯ä»ã¾ã§åæ§ãããã¸ã§ã¯ãæ å ±ãå ¥åãã¦ããã¸ã§ã¯ãä½æãå®äºãã¦ãã ãããããã¨ãä½æãããããã¸ã§ã¯ãã®ã¨ãã£ã¿é åã¯å³2ã®ããã«ãªãã¾ãã
ãä»ã¾ã§ã¯ãMainActivity.javaãã¨ãªã£ã¦ãããã¡ã¤ã«åããMainActivity.ktãã¨Kotlinè¨èªãã¡ã¤ã«ã«ãªã£ã¦ããããããããè¨è¿°ããã¦ããã½ã¼ã¹ã³ã¼ããKotlinè¨èªã«ãªã£ã¦ãã¾ãã
ãããã«Kotlinè¨èªã§ã½ã¼ã¹ã³ã¼ããè¨è¿°ãããã¨ã§Androidã¢ããªéçºãè¡ãã¾ããä¾ãã°ããªã¹ã1ã§ã¯IDã¨ãã¦tvHelloãè¨å®ãããTextViewã«ãããã«ã¡ã¯!ãã¨è¡¨ç¤ºããã³ã¼ãã¨ãã¦å¤ªåã®2è¡ã追è¨ãã¦ãã¾ãã
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var tvHello = findViewById<TextView>(R.id.tvHello) tvHello.setText("ããã«ã¡ã¯!") } }
ããªã¹ã1ã®2è¡ç®ãè¦ã¦ãã ãããJavaã¨åãããã«onCreate()ã¡ã½ãããè¨è¿°ããã¦ãã¾ããKotlinã¯å¤æ°ã®å¾ãã«ã³ãã³ï¼:ï¼ã§åºåã£ã¦ãã¼ã¿åãè¨è¿°ããã®ã§ãå¼æ°ã®ãã¼ã¿åã¯Bundleã§ããã§ã¯ãã?ãã¯ä½ãæå³ããã®ã§ãããããããã¯ãnullã«ãªãå¯è½æ§ããããã¨ã示ãã¾ããéã«ã?ããè¨è¿°ããã¦ããªããã°ãã®å¤æ°ï¼å¼æ°ï¼ã¯nullã«ãªããªãå¤æ°ï¼å¼æ°ï¼ã表ãã¾ãããã®ããã«ãKotlinã¯nullã®å¯è½æ§ããããã®ã¨ãªããã®ãå³æ ¼ã«åºå¥ãããã¨ã§ãJavaã¨æ¯ã¹ã¦nullã«å¯¾ãã¦å®å ¨ãªè¨èªä»æ§ã¨ãªã£ã¦ãã¾ãã
ãã¾ããåãã5è¡ç®ãè¦ã¦ãã ãããå¤æ°ã宣è¨ããéãJavaãªãã°ã
TextView tvHello = â¦
ã¨ãå¤æ°ã®åãæå®ããå¿ è¦ãããã¾ããã5è¡ç®ã«ã¯ãããããã¾ãããããã¯ãKotlinã§ã¯åæ¨è«ãè¡ã£ã¦ãããããã§ãããã®ä»çµã¿ã«ãããKotlinã§ã¯ã·ã³ãã«ãªè¨è¿°ãå¯è½ã¨ãªã£ã¦ãã¾ãã