-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
94debf1
commit c1c22eb
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
template/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file in the root | ||
* directory of this source tree. | ||
*/ | ||
package com.helloworld | ||
|
||
import android.content.Context | ||
import com.facebook.flipper.android.AndroidFlipperClient | ||
import com.facebook.flipper.android.utils.FlipperUtils | ||
import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin | ||
import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin | ||
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin | ||
import com.facebook.flipper.plugins.inspector.DescriptorMapping | ||
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin | ||
import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor | ||
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin | ||
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin | ||
import com.facebook.react.ReactInstanceEventListener | ||
import com.facebook.react.ReactInstanceManager | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.modules.network.NetworkingModule | ||
|
||
/** | ||
* Class responsible of loading Flipper inside your React Native application. This is the debug | ||
* flavor of it. Here you can add your own plugins and customize the Flipper setup. | ||
*/ | ||
object ReactNativeFlipper { | ||
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) { | ||
if (FlipperUtils.shouldEnableFlipper(context)) { | ||
val client = AndroidFlipperClient.getInstance(context) | ||
client.addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())) | ||
client.addPlugin(DatabasesFlipperPlugin(context)) | ||
client.addPlugin(SharedPreferencesFlipperPlugin(context)) | ||
client.addPlugin(CrashReporterPlugin.getInstance()) | ||
val networkFlipperPlugin = NetworkFlipperPlugin() | ||
NetworkingModule.setCustomClientBuilder { builder -> | ||
builder.addNetworkInterceptor(FlipperOkhttpInterceptor(networkFlipperPlugin)) | ||
} | ||
client.addPlugin(networkFlipperPlugin) | ||
client.start() | ||
|
||
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized | ||
// Hence we run if after all native modules have been initialized | ||
val currentReactContext = reactInstanceManager.currentReactContext | ||
if (currentReactContext == null) { | ||
reactInstanceManager.addReactInstanceEventListener( | ||
object : ReactInstanceEventListener { | ||
override fun onReactContextInitialized(context: ReactContext) { | ||
reactInstanceManager.removeReactInstanceEventListener(this) | ||
context.runOnNativeModulesQueueThread { client.addPlugin(FrescoFlipperPlugin()) } | ||
} | ||
}) | ||
} else { | ||
client.addPlugin(FrescoFlipperPlugin()) | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
template/android/app/src/main/java/com/helloworld/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.helloworld | ||
|
||
import com.facebook.react.ReactActivity | ||
import com.facebook.react.ReactActivityDelegate | ||
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled | ||
import com.facebook.react.defaults.DefaultReactActivityDelegate | ||
|
||
class MainActivity : ReactActivity() { | ||
|
||
/** | ||
* Returns the name of the main component registered from JavaScript. This is used to schedule | ||
* rendering of the component. | ||
*/ | ||
override fun getMainComponentName(): String = "HelloWorld" | ||
|
||
/** | ||
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate] | ||
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled] | ||
*/ | ||
override fun createReactActivityDelegate(): ReactActivityDelegate = | ||
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) | ||
} |
38 changes: 38 additions & 0 deletions
38
template/android/app/src/main/java/com/helloworld/MainApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.helloworld | ||
|
||
import android.app.Application | ||
import com.facebook.react.PackageList | ||
import com.facebook.react.ReactApplication | ||
import com.facebook.react.ReactNativeHost | ||
import com.facebook.react.ReactPackage | ||
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load | ||
import com.facebook.react.defaults.DefaultReactNativeHost | ||
import com.facebook.soloader.SoLoader | ||
|
||
class MainApplication : Application(), ReactApplication { | ||
|
||
private val reactNativeHost: ReactNativeHost = | ||
object : DefaultReactNativeHost(this) { | ||
override fun getPackages(): List<ReactPackage> { | ||
// Packages that cannot be autolinked yet can be added manually here, for example: | ||
// packages.add(new MyReactNativePackage()); | ||
return PackageList(this).packages | ||
} | ||
override fun getJSMainModuleName(): String = "index" | ||
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG | ||
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED | ||
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED | ||
} | ||
|
||
override fun getReactNativeHost(): ReactNativeHost = reactNativeHost | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
SoLoader.init(this, false) | ||
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { | ||
// If you opted-in for the New Architecture, we load the native entry point for this app. | ||
load() | ||
} | ||
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
template/android/app/src/release/java/com/helloworld/ReactNativeFlipper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the LICENSE file in the root | ||
* directory of this source tree. | ||
*/ | ||
package com.helloworld | ||
|
||
import android.content.Context | ||
import com.facebook.react.ReactInstanceManager | ||
|
||
/** | ||
* Class responsible of loading Flipper inside your React Native application. This is the release | ||
* flavor of it so it's empty as we don't want to load Flipper. | ||
*/ | ||
object ReactNativeFlipper { | ||
@Suppress("UNUSED_PARAMETER") | ||
fun initializeFlipper(context: Context, reactInstanceManager: ReactInstanceManager) { | ||
// Do nothing as we don't want to initialize Flipper on Release. | ||
} | ||
} |