How to add the config in react-native-fs? - react-native

I installed react-native-fs and I followed through the document. I did all the configuration but at the last, they mentioned I should make the changes in MainApplication.java . But configuration is not the same as their document.
react-native-fs ==> MainApplication.java
import com.rnfs.RNFSPackage; // <------- add package
public class MainApplication extends Application implements ReactApplication {
// ...
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(), // <---- add comma
new RNFSPackage() // <---------- add package
);
}
My app ===> MainApplication.java
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return packages;
}

react-native-fs installation instructions were written long time ago when autolinking was not there(prior to React Native v0.60.0 release).
Firstly, if you are using React Native version 0.60.0+, then you do not have to perform any additional android and iOS(except the pod install for iOS) steps as described in the Readme.md of the package since autolinking will automatically handle the native configuration part.
But if you still want to manually link the library, then in your given android code in MainApplication.java file, you can add the new RNFSPackage() as shown in the code snippet below.
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new RNFSPackage()); //<== here
return packages;
}

Related

How to access Android assets folder in React-native projects?

I'm trying to build a Android module for React-native,
But it seems that ReactApplicationContext cannot access Android assets folder.
Could anybody tell me ho to do ?
Or, is there any workaround?
Thank you.
public class Predictor extends ReactContextBaseJavaModule {
private ReactApplicationContext mContext;
public Predictor(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
// how to access android assets folder?
String ss = mContext.getCacheDir().toString();
}

Force Android App Link to open in browser

We have a React Native app which shows our mobile website and adds some extra features.
Since Android 12 App links (like domain.com) always open our app: https://developer.android.com/training/app-links
This behaviour is not always desirable, for example in this scenario:
Customer is logged in and starts an order via their browser
Customer needs to pay via an app from their bank
After payment, the customer is redirected back to our website (domain.com/returnUrl)
Now the app is opened, instead of the browser, so the customer isn't logged-in and isn't allowed to view the page.
In this case, after payment started from the browser, we would like to redirect the customer back to the browser instead of the app.
Is there a way to open a link in the browser (ie. via domain.com/returnUrl?force-browser) instead of the app?
Related: Android App link - Open a url from app in browser without triggering App Link
Based on this answer, I've created a RN Native Module and instead of using await Linking.openURL(url) you can just use the Native Module's exposed method to open Android App links.
I've followed the official RN tutorial to make an Android Native Module.
So in summary, first you will have to create a Java class file inside android/app/src/main/java/com/your-app-name/folder. I've named the module DefaultBrowserModule so the path is src/main/java/com/your-app-name/DefaultBrowserModule.java. Here's how it looks like:
package com.your-app-name;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class DefaultBrowserModule extends ReactContextBaseJavaModule {
private ReactApplicationContext _context;
DefaultBrowserModule(ReactApplicationContext context) {
super(context);
this._context = context;
}
#NonNull
#Override
public String getName() {
return "DefaultBrowserModule";
}
// This is the method that we're exposing
#ReactMethod
public void openUrl(String url) {
Intent defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER);
defaultBrowser.setData(Uri.parse(url));
// Through ReactApplicationContext's current activty, start a new activity
this._context.getCurrentActivity().startActivity(defaultBrowser);
}
}
After that we'll have to register the module with React Native. That can be done by adding a new Java class file to the android/app/src/main/java/com/your-app-name/ folder. I've named mine DefaultBrowserPackage: src/main/java/com/your-app-name/DefaultBrowserPackage.java:
package com.your-app-name;
import androidx.annotation.NonNull;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DefaultBrowserPackage implements ReactPackage {
#NonNull
#Override
public List<NativeModule> createNativeModules(#NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new DefaultBrowserModule(reactContext));
return modules;
}
#NonNull
#Override
public List<ViewManager> createViewManagers(#NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
The last step is to register the DefaultBrowserPackage inside of MainApplication.java (android/app/src/main/java/com/your-app-name/MainApplication.java). Locate ReactNativeHost’s getPackages() method and add your package to the packages list
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// below DefaultBrowserPackage is added to the list of packages returned
packages.add(new DefaultBrowserPackage());
return packages;
}
Now we are ready to use it inside of JS. So wherever you want to use it, you can do it like this:
import { Linking, NativeModules, Platform } from 'react-native';
// DefaultBrowserModule should be equal to the return value of the getName() method
// inside of the src/main/java/com/your-app-name/DefaultBrowserModule.java class
const { DefaultBrowserModule } = NativeModules;
export const openUrl = async (url) => {
if (Platform.OS === 'android') {
DefaultBrowserModule.openUrl(url);
} else {
await Linking.openURL(url);
}
};
// And then use it like this
await openUrl('https://my-app-link-domain.com');
Deep and universal linking happens on the operating level and it's hard to control the behavior of other app linking I think it should security breach as some apps try to override the deep link behaviors of another app.
Try to create your simple page with your custom URL https://my-domain.com which redirect to tour target URL without opening associated app.
The best possible solution for that can be using android:pathPattern in android manifest. Basically you have to provide path pattern (a sort regex) to match the valid links.
Documentation for that can be found here.
https://developer.android.com/guide/topics/manifest/data-element

Creating a custom run configuration using IntelliJ SDK and adding Build task in Before launch section

I'm following the tutorial in Run Configuration section of IntelliJ IDEA SDK.
Following the tutorial, I will get a new configuration panel with an empty "Before launch" section. That section is added by default.
I would like to specify some targets by default, i.e. at least the Build target as done in several plugins (see next picture)
I'm trying to understand how, but I cannot find any example nor documentation on this.
How can I add default build task?
Your run configuration (DemoRunConfiguration in the example) should implement RunProfileWithCompileBeforeLaunchOption. This interface doesn't provide any methods to implement, so this is a kind of mark. build task will be added automatically, no additional steps are required.
How can I add my own task to before launch section?
Your plugin.xml should contain a line with stepsBeforeRunProvider
<stepsBeforeRunProvider implementation="com.MyBeforeRunProvider" id="myBeforeRun"/>
For that you should create "before run provider" with "before run task".
public class BeforeRunProvider extends BeforeRunTaskProvider<MyBeforeRunTask> {
#Override
public Key<MyBeforeRunTask> getId() {
return Key.create("ThisIsId");
}
#Override
public String getName() {
return "Nice name";
}
#Override
public String getDescription(MyBeforeRunTask task) {
return "Description";
}
#Nullable
#Override
public Icon getIcon() {
return AllIcons.Actions.Compile;
}
#Nullable
#Override
public MyBeforeRunTask createTask(#NotNull RunConfiguration runConfiguration) {
return new MyBeforeRunTask(getId());
}
#Override
public boolean executeTask(#NotNull DataContext dataContext, #NotNull RunConfiguration runConfiguration, #NotNull ExecutionEnvironment executionEnvironment, #NotNull MyBeforeRunTask myBeforeRunTask) {
return true;
}
}
And the task:
public class MyBeforeRunTask extends BeforeRunTask<MyBeforeRunTask> {
protected MyBeforeRunTask(#NotNull Key<MyBeforeRunTask> providerId) {
super(providerId);
setEnabled(true);
}
}

React Native importing React Navigation

I follow all instructions for installing React Navigation for Android.
First, "npm install --save react-navigation", " npm install --save react-native-gesture-handler". "react-native link react-native-gesture-handler" and lastly, updated my MainActivity.java
Here is my MainActivity.java:
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
#Override
protected String getMainComponentName() {
return "InstagramClone";
}
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this)
}
};
}
}
This is the error I got:
What went wrong:
Failed to create parent directory 'D:\React Native Projects\InstagramClone\node_modules\react-native-gesture-handler\android\build' when creating directory 'D:\React Native Projects\InstagramClone\node_modules\react-native-gesture-handler\android\build\intermediates\check-manifest\debug'
If you initiated your project with expo just install react-native-gesture-handler. But if your projected started by react-native cli you have to first install react-native-gesture-handler and then link it to all packages by the command:
react-native link
if you had to a link react-native-gesture-handler there should be no problem or you try react-native link it will be link all package. Or if you still face same problem delete app first on your emulator then React-native run-android again
You need to install react-native-gesture-handler npm separately. They create separated npm package for touch & gesture handling and recognition.
Step 1.
npm i react-native-gesture-handler
Step 2.
react-native link react-native-gesture-handler
Step 3.(optional )
If step 2 is not worked properly, code is not configured properly so we are manually configure it using step 3
To finalize the installation of react-native-gesture-handler for Android, be sure to make the necessary modifications to MainActivity.java:
import com.facebook.react.ReactActivity; import com.facebook.react.ReactActivityDelegate; import com.facebook.react.ReactRootView; import
com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
public class MainActivity extends ReactActivity {
#Override protected
String getMainComponentName() { return "Example"; }
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this); }
};
} }
No additional steps are required for iOS.
Please Refer the following document for more information:-
https://reactnavigation.org/docs/en/getting-started.html#installation
https://www.npmjs.com/package/react-native-gesture-handler/v/1.0.0-alpha.34?activeTab=readme
https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html

React Native: error: constructor MapsPackage in class MapsPackage cannot be applied to given types

I tried the both solution new MapsPackage(this) and new MapsPackage() .But nothing works for me.
And I see the MapsPackage class, The constructor are given as below
They handling the both compatibility. with argument and without argument.
public MapsPackage(Activity activity) {
} // backwards compatibility
public MapsPackage() {
}
your information doesn't sufficient.
you can see this error when adding new module in
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new LocationPackage());
return packages;
}
you should care about the point you import new module. This error occurs when you want to add package but import module. for example when you want to write new LocationPackage(), you write new LocationModule().