I have a Android (Kotlin) Native app. It just has a button in the MainActivity. I want to add a React Native app as a library inside the Android Native app in such a way that from the button click in Android Native app, the React Native app should open inside the Android app seamlessly without changing anything drastically. I don’t want to install the React Native app separately and then have it open. It should all be in Android Native app.
I found this code where you can import android folder of React Native project inside the Android Native project as a library. For this I created a ReactNativeActivity in the Android Native app so that on button click I can intent to this activity and in this activity the React Native app will be shown. I tried to import react-native by adding the react-native and react-android libraries to build.gradle.kts in Android Native app but its not working. I dont get any sync error for library. But getting reference error in import com.facebook.
implementation(libs.react.native)
implementation(libs.react.android)
implementation(libs.facebook.react.android)
implementation("com.facebook.react:react-native:0.64.1")
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
class ReactNativeActivity: AppCompatActivity () {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var mReactRootView = new ReactRootView(this);
var mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(application)
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build()
mReactRootView.startReactApplication(mReactInstanceManager, "Stopwatch", null)
setContentView(mReactRootView)
}
}