-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add kotlin examples in new-architecture-app-modules-android.md #3241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7a3d6aa
8b2f0e9
8472488
ee3a105
1884102
db64080
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,8 @@ title: Enabling TurboModule on Android | |||||||||||||
| --- | ||||||||||||||
|
|
||||||||||||||
| import NewArchitectureWarning from './\_markdown-new-architecture-warning.mdx'; | ||||||||||||||
| import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; | ||||||||||||||
| import constants from '@site/core/TabsConstants'; | ||||||||||||||
|
|
||||||||||||||
| <NewArchitectureWarning/> | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -135,11 +137,14 @@ You can now verify that everything works correctly by running your android app: | |||||||||||||
| yarn react-native run-android | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ## 2. Java - Provide a `ReactPackageTurboModuleManagerDelegate` | ||||||||||||||
| ## 2. Java/Kotlin - Provide a `ReactPackageTurboModuleManagerDelegate` | ||||||||||||||
|
|
||||||||||||||
| Now is time to actually use the TurboModule. | ||||||||||||||
| First, we will need to create a `ReactPackageTurboModuleManagerDelegate` subclass, like the following: | ||||||||||||||
|
|
||||||||||||||
| <Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}> | ||||||||||||||
| <TabItem value="java"> | ||||||||||||||
|
|
||||||||||||||
| ```java | ||||||||||||||
| package com.awesomeproject; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -179,6 +184,52 @@ public class MyApplicationTurboModuleManagerDelegate extends ReactPackageTurboMo | |||||||||||||
| } | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| </TabItem> | ||||||||||||||
|
|
||||||||||||||
| <TabItem value="kotlin"> | ||||||||||||||
|
|
||||||||||||||
| ```kotlin | ||||||||||||||
| package com.awesomeproject | ||||||||||||||
|
|
||||||||||||||
| import com.facebook.jni.HybridData | ||||||||||||||
| import com.facebook.react.ReactPackage | ||||||||||||||
| import com.facebook.react.ReactPackageTurboModuleManagerDelegate | ||||||||||||||
| import com.facebook.react.bridge.ReactApplicationContext | ||||||||||||||
| import com.facebook.soloader.SoLoader | ||||||||||||||
|
|
||||||||||||||
| class MyApplicationTurboModuleManagerDelegate | ||||||||||||||
| protected constructor( | ||||||||||||||
| reactApplicationContext: ReactApplicationContext, | ||||||||||||||
| packages: List<ReactPackage> | ||||||||||||||
| ) : ReactPackageTurboModuleManagerDelegate(reactApplicationContext, packages) { | ||||||||||||||
| override protected external fun initHybrid(): HybridData? | ||||||||||||||
| class Builder : ReactPackageTurboModuleManagerDelegate.Builder() { | ||||||||||||||
| override protected fun build( | ||||||||||||||
| context: ReactApplicationContext, | ||||||||||||||
| packages: List<ReactPackage> | ||||||||||||||
| ): MyApplicationTurboModuleManagerDelegate { | ||||||||||||||
| return MyApplicationTurboModuleManagerDelegate(context, packages) | ||||||||||||||
| } | ||||||||||||||
|
||||||||||||||
| ): MyApplicationTurboModuleManagerDelegate { | |
| return MyApplicationTurboModuleManagerDelegate(context, packages) | |
| } | |
| ) = MyApplicationTurboModuleManagerDelegate(context, packages) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @Volatile private var sIsSoLibraryLoaded = false | |
| @Volatile private var isSoLibraryLoaded = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I was not precise before. Let's use mReactNativeHost for Java and reactNativeHost for Kotlin.
That convention was in place for Java only:
https://source.android.com/setup/contribute/code-style#follow-field-naming-conventions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| override fun getReactPackageTurboModuleManagerDelegateBuilder(): | |
| ReactPackageTurboModuleManagerDelegate.Builder? { | |
| return Builder() | |
| } | |
| override fun getReactPackageTurboModuleManagerDelegateBuilder() = | |
| ReactPackageTurboModuleManagerDelegate.Builder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we add new lines around this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done