Skip to content

Commit c1c22eb

Browse files
cortinicofacebook-github-bot
authored andcommitted
Convert the app template to Kotlin (#36696)
Summary: Pull Request resolved: #36696 As the title says, we're converting the new app template to Kotlin. This will reduce the template size and make it more aligned to market standards. Changelog: [Android] [Changed] - Convert the app template to Kotlin Reviewed By: mdvacca Differential Revision: D44142081 fbshipit-source-id: 6111360b6580460eba0341e47c55704cc673e444
1 parent 94debf1 commit c1c22eb

6 files changed

Lines changed: 144 additions & 0 deletions

File tree

packages/react-native/template/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: "com.android.application"
2+
apply plugin: "org.jetbrains.kotlin.android"
23
apply plugin: "com.facebook.react"
34

45
/**

packages/react-native/template/android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ buildscript {
99

1010
// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
1111
ndkVersion = "23.1.7779620"
12+
kotlinVersion = "1.7.22"
1213
}
1314
repositories {
1415
google()
@@ -17,5 +18,6 @@ buildscript {
1718
dependencies {
1819
classpath("com.android.tools.build:gradle")
1920
classpath("com.facebook.react:react-native-gradle-plugin")
21+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
2022
}
2123
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the LICENSE file in the root
5+
* directory of this source tree.
6+
*/
7+
package com.helloworld
8+
9+
import android.content.Context
10+
import com.facebook.flipper.android.AndroidFlipperClient
11+
import com.facebook.flipper.android.utils.FlipperUtils
12+
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin
13+
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin
14+
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin
15+
import com.facebook.flipper.plugins.inspector.DescriptorMapping
16+
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
17+
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor
18+
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin
19+
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin
20+
import com.facebook.react.ReactInstanceEventListener
21+
import com.facebook.react.ReactInstanceManager
22+
import com.facebook.react.bridge.ReactContext
23+
import com.facebook.react.modules.network.NetworkingModule
24+
25+
/**
26+
* Class responsible of loading Flipper inside your React Native application. This is the debug
27+
* flavor of it. Here you can add your own plugins and customize the Flipper setup.
28+
*/
29+
object ReactNativeFlipper {
30+
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) {
31+
if (FlipperUtils.shouldEnableFlipper(context)) {
32+
val client = AndroidFlipperClient.getInstance(context)
33+
client.addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()))
34+
client.addPlugin(DatabasesFlipperPlugin(context))
35+
client.addPlugin(SharedPreferencesFlipperPlugin(context))
36+
client.addPlugin(CrashReporterPlugin.getInstance())
37+
val networkFlipperPlugin = NetworkFlipperPlugin()
38+
NetworkingModule.setCustomClientBuilder { builder ->
39+
builder.addNetworkInterceptor(FlipperOkhttpInterceptor(networkFlipperPlugin))
40+
}
41+
client.addPlugin(networkFlipperPlugin)
42+
client.start()
43+
44+
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
45+
// Hence we run if after all native modules have been initialized
46+
val currentReactContext = reactInstanceManager.currentReactContext
47+
if (currentReactContext == null) {
48+
reactInstanceManager.addReactInstanceEventListener(
49+
object : ReactInstanceEventListener {
50+
override fun onReactContextInitialized(context: ReactContext) {
51+
reactInstanceManager.removeReactInstanceEventListener(this)
52+
context.runOnNativeModulesQueueThread { client.addPlugin(FrescoFlipperPlugin()) }
53+
}
54+
})
55+
} else {
56+
client.addPlugin(FrescoFlipperPlugin())
57+
}
58+
}
59+
}
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.helloworld
2+
3+
import com.facebook.react.ReactActivity
4+
import com.facebook.react.ReactActivityDelegate
5+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
6+
import com.facebook.react.defaults.DefaultReactActivityDelegate
7+
8+
class MainActivity : ReactActivity() {
9+
10+
/**
11+
* Returns the name of the main component registered from JavaScript. This is used to schedule
12+
* rendering of the component.
13+
*/
14+
override fun getMainComponentName(): String = "HelloWorld"
15+
16+
/**
17+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
18+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
19+
*/
20+
override fun createReactActivityDelegate(): ReactActivityDelegate =
21+
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.helloworld
2+
3+
import android.app.Application
4+
import com.facebook.react.PackageList
5+
import com.facebook.react.ReactApplication
6+
import com.facebook.react.ReactNativeHost
7+
import com.facebook.react.ReactPackage
8+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
9+
import com.facebook.react.defaults.DefaultReactNativeHost
10+
import com.facebook.soloader.SoLoader
11+
12+
class MainApplication : Application(), ReactApplication {
13+
14+
private val reactNativeHost: ReactNativeHost =
15+
object : DefaultReactNativeHost(this) {
16+
override fun getPackages(): List<ReactPackage> {
17+
// Packages that cannot be autolinked yet can be added manually here, for example:
18+
// packages.add(new MyReactNativePackage());
19+
return PackageList(this).packages
20+
}
21+
override fun getJSMainModuleName(): String = "index"
22+
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
23+
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
24+
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
25+
}
26+
27+
override fun getReactNativeHost(): ReactNativeHost = reactNativeHost
28+
29+
override fun onCreate() {
30+
super.onCreate()
31+
SoLoader.init(this, false)
32+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
33+
// If you opted-in for the New Architecture, we load the native entry point for this app.
34+
load()
35+
}
36+
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the LICENSE file in the root
5+
* directory of this source tree.
6+
*/
7+
package com.helloworld
8+
9+
import android.content.Context
10+
import com.facebook.react.ReactInstanceManager
11+
12+
/**
13+
* Class responsible of loading Flipper inside your React Native application. This is the release
14+
* flavor of it so it's empty as we don't want to load Flipper.
15+
*/
16+
object ReactNativeFlipper {
17+
@Suppress("UNUSED_PARAMETER")
18+
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) {
19+
// Do nothing as we don't want to initialize Flipper on Release.
20+
}
21+
}

0 commit comments

Comments
 (0)