react-nativeexpo

How to create a new file in native folder with expo config plugins?


As the title it is. with expo config plugin, I already figured out how to make changes on manifest, main activity and ... etc.

And Now I need to create some new activity files into the android folder (i am not sure if this is even possible) or there is some way could work around

The structure in android folder should looks like below

android
|-- app
|   |-- src
|   |   |-- main
|   |   |   |-- java/com/my/app
|   |   |   |    |-- [folder I want to create]
|   |   |   |    |   |-- [new activity files shall goes here]
|   |   |   |    |-- MainActivity.java
|   |   |   |    |-- MainApplication.java

Of course I could just apply the bare workflow and change the files directly inside of the native folder. However I would like to remaining use the managed workflow. so if there is any way I could archive this, will be nice!


Solution

  • The certain example could be found HERE

    dangerous mod provide the tool for us to create folders or files.

    Example:

    import { withDangerousMod } from '@expo/config-plugins'
    
    const wxapiAndroid: ConfigPlugin = (config) => {
      return withDangerousMod(config, [
        'android',
        async (config) => {
          const customePackageName = 'com.your_package.name'
          const srcPath = path.resolve(config.modRequest.projectRoot, config.modRequest.platformProjectRoot, `app/src/main/java/com/your/package_name`)
          //create a new file
          const newFileName = 'WXEntryActivity.java'
          const newFolderName = 'wxapi'
          const newFileContent = `
    package ${customePackageName}.wxapi;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    public class WXEntryActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
          Intent intent = getIntent();
          Intent intentToBroadcast = new Intent();
          intentToBroadcast.setAction("com.hector.nativewechat.ACTION_REDIRECT_INTENT");
          intentToBroadcast.putExtra("intent", intent);
          sendBroadcast(intentToBroadcast);
          finish();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    `
          const newFilePath = path.resolve(srcPath, newFolderName, newFileName)
    
          //create a new folder
          const newFolderPath = path.resolve(srcPath, newFolderName)
          if (!fs.existsSync(newFolderPath)) {
            fs.mkdirSync(newFolderPath)
          }
    
          //create a new file
          if (!fs.existsSync(newFilePath)) {
            fs.writeFileSync(newFilePath, newFileContent)
          }
    
          //create the pay api file
          const payApiFileName = 'WXPayEntryActivity.java'
          const payApiFileContent = `
            package ${customePackageName}.wxapi;
            import android.app.Activity;
            import android.os.Bundle;
            public class WXPayEntryActivity extends Activity {
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    
                    finish();
                }
            }
            `
    
          const payApiFilePath = path.resolve(srcPath, newFolderName, payApiFileName)
          if (!fs.existsSync(payApiFilePath)) {
            fs.writeFileSync(payApiFilePath, payApiFileContent)
          }
    
          return config
        },
      ])
    }
    
    export default wxapiAndroid