iOS122-移动混合开发研究院React Native开发Kotlin

坑中速记整理! 使用 kotlin 写第一个 ReactNati

2017-10-12  本文已影响150人  ios122
preview.png

Kotlin 和 Swift, 两大新宠! 借 ReactNative 熟悉下 kotlin 的用法,不料掉坑里面了.昨晚花了大半夜,趁这会儿思路清晰,把涉及到的一些关键信息,迅速整理下.

最佳的使用 Kotlin 快速开始写Android模块的方式

  1. react-native init AwesomeProject 生成的 android 目录,是一个标准的 Android Studio 工程,详见: http://facebook.github.io/react-native/docs/getting-started.html
  2. 直接在 Android Studio 中打开 AwesomeProject/android 目录.
  3. 参考文章 http://facebook.github.io/react-native/docs/native-modules-android.html,先用 java 实现
  4. 顶部菜单 --> code --> Convert Java File to Kotlin File ,自动转换为 kotlin .
package com.awesomeproject.AnExampleReactPackage

import android.widget.Toast

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod

import java.util.HashMap

/**
 * Created by yanfeng on 2017/10/12.
 */

class ToastModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String {
        return "ToastExample"
    }

    override fun getConstants(): Map<String, Any>? {
        val constants = HashMap<String, Any>()
        constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT)
        constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG)
        return constants
    }

    @ReactMethod
    fun show(message: String, duration: Int) {
        Toast.makeText(reactApplicationContext, message, duration).show()
    }

    companion object {

        private val DURATION_SHORT_KEY = "SHORT"
        private val DURATION_LONG_KEY = "LONG"
    }
}

一些坑中的经验

npm install -g nrm
nrm use taobao
npm install -g react-native-cli --verbose
react-native init AwesomeProject --verboses

命令是:

./gradlew -q dependencies app:dependencies --configuration compile

可能的输出:

+--- com.android.support.constraint:constraint-layout:1.0.0-beta2
|    \--- com.android.support.constraint:constraint-layout-solver:1.0.0-beta2
\--- com.facebook.react:react-native:+ -> 0.20.1
     +--- com.google.code.findbugs:jsr305:3.0.0
     +--- com.facebook.stetho:stetho-okhttp:1.2.0
     |    +--- com.google.code.findbugs:jsr305:2.0.1 -> 3.0.0
     |    +--- com.facebook.stetho:stetho:1.2.0
     |    |    +--- com.google.code.findbugs:jsr305:2.0.1 -> 3.0.0
     |    |    \--- commons-cli:commons-cli:1.2
     |    \--- com.squareup.okhttp:okhttp:2.2.0 -> 2.5.0
     |         \--- com.squareup.okio:okio:1.6.0
     +--- com.squareup.okhttp:okhttp-ws:2.5.0
     |    \--- com.squareup.okhttp:okhttp:2.5.0 (*)
     +--- com.facebook.fresco:fresco:0.8.1
     |    +--- com.facebook.fresco:imagepipeline:0.8.1
     |    |    +--- com.nineoldandroids:library:2.4.0
     |    |    +--- com.facebook.fresco:fbcore:0.8.1
     |    |    +--- com.android.support:support-v4:21.0.3 -> 23.0.1
     |    |    |    \--- com.android.support:support-annotations:23.0.1
     |    |    \--- com.parse.bolts:bolts-android:1.1.4
     |    +--- com.facebook.fresco:fbcore:0.8.1
     |    \--- com.facebook.fresco:drawee:0.8.1
     |         +--- com.facebook.fresco:fbcore:0.8.1
     |         \--- com.android.support:support-v4:21.0.3 -> 23.0.1 (*)
     +--- org.webkit:android-jsc:r174650
     +--- com.fasterxml.jackson.core:jackson-core:2.2.3
     +--- com.squareup.okhttp:okhttp:2.5.0 (*)
     +--- com.facebook.fresco:imagepipeline-okhttp:0.8.1
     |    +--- com.squareup.okhttp:okhttp:2.3.0 -> 2.5.0 (*)
     |    +--- com.facebook.fresco:imagepipeline:0.8.1 (*)
     |    \--- com.facebook.fresco:fbcore:0.8.1
     +--- com.squareup.okio:okio:1.6.0
     +--- com.android.support:recyclerview-v7:23.0.1
     |    +--- com.android.support:support-v4:23.0.1 (*)
     |    \--- com.android.support:support-annotations:23.0.1
     +--- com.facebook.stetho:stetho:1.2.0 (*)
     \--- com.android.support:appcompat-v7:23.0.1
          \--- com.android.support:support-v4:23.0.1 (*)

(*) - dependencies omitted (listed previously)
adb reverse tcp:8081 tcp:8081

源码参考:

https://github.com/ios122/kotlin-module-sample-for-reactnative

参考文章

上一篇下一篇

猜你喜欢

热点阅读