AndroidAnnotationsã¯Kotlinã§ã¯ä½¿ããªã
ã¢ããã¼ã·ã§ã³ãä»ãããã¨ã§ãã¤ã©ã¼ãã¬ã¼ããªã³ã¼ããåæ¸ãããã¼ã«ã§ããAndroidAnnotationsã¯åã«ã¨ã£ã¦ãã¯ãAndroidã¢ããªéçºã§æ¬ ãããªããã¼ã«ã¨ãªã£ã¦ãã¾ããAndroidAnnotationsã使ã£ããµã³ãã«ã³ã¼ãã¨ãã¦ã¯ä¸è¨ã®ããã«èªåã§Viewãã¤ã³ã¸ã§ã¯ãããä¾ã代表çã ã¨æãã¾ãã
@EActivity(R.layout.activity_main) public class MainActivity extends Activity { @ViewById Button button; }
AndroidAnnotationsã®ãããªãã¼ã«ï¼ã©ã¤ãã©ãªã使ããªãå ´åã¯button = (Button) findViewById(R.id.button);
ãæ¸ãå¿
è¦ããã£ã¦ããã£ã¹ãããªããå«ã ãããªã½ã¼ã¹åã¨ãã£ã¼ã«ãåãåãå ´åã¯éè¤ãã¦æå®ãã¦ãæãããã£ã¦ä½ãã ãºã ãºãã¾ããã
ãã®ããã«é常ã«ä¾¿å©ãªAndroidAnnotationsã§ãããæ®å¿µãªãããã¿ããªå¤§å¥½ãKotlinã§ã¯ä½¿ããªããããªã®ã§ãã
RoboGuiceã¯Kotlinã§ä½¿ãã
Androidç¨DIã³ã³ããã§ããRoboGuiceã«ãViewãã¤ã³ã¸ã§ã¯ãããæ©è½ãããã¾ããä¸è¨ã®ã³ã¼ããRoboGuiceã使ã£ã¦æ¸ãã¨æ¬¡ã®ããã«ãªãã¾ãã
public class MainActivity extends RoboActivity { @InjectView(R.id.button) Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
AndroidAnnotationsã¨ã®éãã¯RoboActivity
ãç¶æ¿ããç¹ã¨ãªã½ã¼ã¹IDã®æå®ãå¿
é ãªç¹ã§ãããã¨ç´°ããé¨åã§ã¯button
ãprivate
æå®ã§ãããã¨ï¼ãã£ã¨è¨ãã¨final
ãä»ããããï¼ãããã¯AndroidAnnotationsãAPTã§éçã«ãã£ã¦ããã¨ã¨ãRoboGuiceããªãã¬ã¯ã·ã§ã³ã§åçã«ãã£ã¦ãããã¨ã®éãã§ãããããããä¸é·ä¸çããã¨æãã¾ãã
ã§ããã®RoboGuiceã¯Kotlinã§ã使ãã¾ã*1ã
Kotlinã§RoboGuiceã使ã£ã¦ã¿ã
åãä¾ãKotlin + RoboGuiceã§æ¸ãã¦ã¿ã¾ãã
class MainActivity: RoboActivity() { [InjectView(R.id.button)] val button: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
ã¾ãã ãããããæãã§ããã3ã¤æ°ã«ãªãã¨ãããã
- AndroidAnnotaionsã¿ããã«ãªã½ã¼ã¹IDãçç¥ããã
- ãªã½ã¼ã¹ãè¦ã¤ãããªãå ´åã¯NPEãæããããã«ãé¢ããããããããã£ã¯Nullableã«ããããå¾ãªã
- ã§ããã°
RoboActivity
ã¯ç¶æ¿ããããªã
Kotlinã§Viewãã¤ã³ã¸ã§ã¯ãããAPIã¤ãã£ã
ã¨ãããã¨ã§æ¬é¡ã§ããä¸è¨ã®ä¸æºã解æ¶ãã¹ããViewãã¤ã³ã¸ã§ã¯ãããAPIãä½ã£ã¦ã¿ã¾ããããã®ä½ã£ããã¤ã使ãã¨ä»ã¾ã§ç¤ºããä¾ã¨åãå 容ã次ã®ããã«æ¸ãã¾ãã
class MainActivity: Activity() { val button: Button by viewInjector() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
ããã«ããä¸æºã解æ¶ããã¾ããã
- ãªã½ã¼ã¹IDãçç¥ã§ããããã«ãªã£ã
- NotNullã«ã§ãã
- ã©ã¤ãã©ãªç¬èªã®Activityãç¶æ¿ããã«æ¸ãã
解説
ä¸è¨ã§èª¬æããã¨ãæ¡å¼µé¢æ°ã¨Delegated Proptertyã¨ãªãã¬ã¯ã·ã§ã³ã使ã£ã¦å®ç¾ãããã§ãã
ã¾ãæ¡å¼µé¢æ°ã«ã¤ãã¦ã§ãããviewInjector()
ãActivity
ã®æ¡å¼µé¢æ°ã¨ãã¦å®ç¾©ãã¾ãããviewInjector()
ã¯Delegated Propertyæ©è½ãæä¾ãããªãã¸ã§ã¯ããè¿ãã¾ãã
Delegated Propertyã¨ã¯å¹³ããè¨ãã°ãããããã£ã¸ã®ã¢ã¯ã»ã¹ãä»ã®ãªãã¸ã§ã¯ãã¸å§è²ããä»çµã¿ã®ãã¨ã§ãããã®ä»çµã¿ã使ããã¨ã§button
ãæåã«åç
§ãããã¨ãã«ããªãã¬ã¯ã·ã§ã³ã使ã£ã¦ãã¿ã³ãåå¾ãbutton
ã¸ã»ãããã¾ãã
ã³ã¼ã
public trait Injector<T> { fun get(thisRef: Any, prop: PropertyMetadata): T } // ãªã½ã¼ã¹IDãæ示çã«æå®ã§ããããã«ããã public fun <T: View> Activity.viewInjector(resId: Int? = null): Injector<T> = object: Injector<T> { private var view: T? = null override fun get(thisRef: Any, prop: PropertyMetadata): T { // ååã®ã¢ã¯ã»ã¹ã®ã¿åæåãã if (view == null) { view = getView(resId, prop.name) } return view!! } private fun <T: View> Activity.getView(resId: Int?, name: String): T = (if (resId != null) findViewById(resId) else findViewByName(name)) as T // ããããã£ã®ååããViewãåå¾ãã private fun Activity.findViewByName(name: String): View? = try { val resIds = Class.forName(getPackageName() + ".R\$id") val resId = resIds.getField(name).get(resIds) as Int findViewById(resId) } catch(e: Exception) { null } }
Injectorã®ç¨®é¡ãå¢ããããä¾å¤å¯¾å¿ãããGithubã«ããã¾ãã
ã¾ã¨ã
- AndroidAnnotationsã¯Kotlinã§ã¯ä½¿ããªã
- RoboGuiceã¯Kotlinã§ã使ãã
- Kotlinã®æ©è½ã使ã£ã¦Viewã®ã¤ã³ã¸ã§ã¯ã·ã§ã³ãæ¯è¼çç°¡åã«å®è£ ã§ãã