# All files / src/platformTools/android kotlinParser.js

31.71% Statements 26/82
0% Branches 0/38
0% Functions 0/10
30.26% Lines 23/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    1622x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x
     import path from 'path';
    import os from 'os';
    import fs from 'fs';
    import net from 'net';
    import chalk from 'chalk';
    import shell from 'shelljs';
    import child_process from 'child_process';
    import inquirer from 'inquirer';
    import {
        logTask,
        logError,
        getAppFolder,
        isPlatformActive,
        getAppVersion,
        getAppTitle,
        getAppVersionCode,
        writeCleanFile,
        getAppId,
        getAppTemplateFolder,
        getBuildFilePath,
        getEntryFile,
        getGetJsBundleFile,
        logWarning,
        logDebug,
        getConfigProp,
        logInfo,
        logSuccess,
        getBuildsFolder,
    } from '../../common';
    import { copyBuildsFolder } from '../../projectTools/projectParser';
    import { copyFolderContentsRecursiveSync, copyFileSync, mkdirSync, readObjectSync } from '../../systemTools/fileutils';
    import { getMergedPlugin, parsePlugins } from '../../pluginTools';

    export const parseMainApplicationSync = (c, platform) => {
        const appFolder = getAppFolder(c, platform);
        const applicationPath = 'app/src/main/java/rnv/MainApplication.kt';
        const bundleFile = getGetJsBundleFile(c, platform) || JS_BUNDLE_DEFAULTS[platform];
        writeCleanFile(getBuildFilePath(c, platform, applicationPath), path.join(appFolder, applicationPath), [
            { pattern: '{{APPLICATION_ID}}', override: getAppId(c, platform) },
            { pattern: '{{ENTRY_FILE}}', override: getEntryFile(c, platform) },
            { pattern: '{{GET_JS_BUNDLE_FILE}}', override: bundleFile },
            { pattern: '{{PLUGIN_IMPORTS}}', override: c.pluginConfigAndroid.pluginImports },
            { pattern: '{{PLUGIN_PACKAGES}}', override: c.pluginConfigAndroid.pluginPackages },
            { pattern: '{{PLUGIN_METHODS}}', override: c.pluginConfigAndroid.mainApplicationMethods },
        ]);
    };

    const JS_BUNDLE_DEFAULTS = {
        android: 'super.getJSBundleFile()',
        androidtv: 'super.getJSBundleFile()',
        // CRAPPY BUT Android Wear does not support webview required for connecting to packager
        androidwear: '"assets://index.androidwear.bundle"',
    };

    export const parseMainActivitySync = (c, platform) => {
        const appFolder = getAppFolder(c, platform);
        const activityPath = 'app/src/main/java/rnv/MainActivity.kt';
        writeCleanFile(getBuildFilePath(c, platform, activityPath), path.join(appFolder, activityPath), [
            { pattern: '{{APPLICATION_ID}}', override: getAppId(c, platform) },
            { pattern: '{{PLUGIN_ACTIVITY_IMPORTS}}', override: c.pluginConfigAndroid.pluginActivityImports },
            { pattern: '{{PLUGIN_ACTIVITY_METHODS}}', override: c.pluginConfigAndroid.pluginActivityMethods },
            { pattern: '{{PLUGIN_ON_CREATE}}', override: c.pluginConfigAndroid.pluginActivityCreateMethods },
            { pattern: '{{PLUGIN_ON_ACTIVITY_RESULT}}', override: c.pluginConfigAndroid.pluginActivityResultMethods },
        ]);
    };

    export const parseSplashActivitySync = (c, platform) => {
        const appFolder = getAppFolder(c, platform);
        const splashPath = 'app/src/main/java/rnv/SplashActivity.kt';

        // TODO This is temporary ANDROIDX support. whole kotlin parser will be refactored in the near future
        const enableAndroidX = getConfigProp(c, platform, 'enableAndroidX');
        if (enableAndroidX === true) {
            c.pluginConfigAndroid.pluginSplashActivityImports += 'import androidx.appcompat.app.AppCompatActivity\n';
        } else {
            c.pluginConfigAndroid.pluginSplashActivityImports += 'import android.support.v7.app.AppCompatActivity\n';
        }

        writeCleanFile(getBuildFilePath(c, platform, splashPath), path.join(appFolder, splashPath), [
            { pattern: '{{APPLICATION_ID}}', override: getAppId(c, platform) },
            { pattern: '{{PLUGIN_SPLASH_ACTIVITY_IMPORTS}}', override: c.pluginConfigAndroid.pluginSplashActivityImports }
        ]);
    };

    export const injectPluginKotlinSync = (c, plugin, key, pkg) => {
        const pathFixed = plugin.path ? `${plugin.path}` : `node_modules/${key}/android`;
        const modulePath = `../../${pathFixed}`;

        if (plugin.activityImports instanceof Array) {
            plugin.activityImports.forEach((activityImport) => {
                // Avoid duplicate imports
                if (c.pluginConfigAndroid.pluginActivityImports.indexOf(activityImport) === -1) {
                    c.pluginConfigAndroid.pluginActivityImports += `import ${activityImport}\n`;
                }
            });
        }

        if (plugin.activityMethods instanceof Array) {
            c.pluginConfigAndroid.pluginActivityMethods += '\n';
            c.pluginConfigAndroid.pluginActivityMethods += `${plugin.activityMethods.join('\n    ')}`;
        }

        const { mainActivity } = plugin;
        if (mainActivity) {
            if (mainActivity.createMethods instanceof Array) {
                c.pluginConfigAndroid.pluginActivityCreateMethods += '\n';
                c.pluginConfigAndroid.pluginActivityCreateMethods += `${mainActivity.createMethods.join('\n    ')}`;
            }

            if (mainActivity.resultMethods instanceof Array) {
                c.pluginConfigAndroid.pluginActivityResultMethods += '\n';
                c.pluginConfigAndroid.pluginActivityResultMethods += `${mainActivity.resultMethods.join('\n    ')}`;
            }

            if (mainActivity.imports instanceof Array) {
                mainActivity.imports.forEach((v) => {
                    c.pluginConfigAndroid.pluginActivityImports += `import ${v}\n`;
                });
            }

            if (mainActivity.methods instanceof Array) {
                c.pluginConfigAndroid.pluginActivityMethods += '\n';
                c.pluginConfigAndroid.pluginActivityMethods += `${mainActivity.methods.join('\n    ')}`;
            }
        }

        if (plugin.imports) {
            plugin.imports.forEach((v) => {
                c.pluginConfigAndroid.pluginImports += `import ${v}\n`;
            });
        }

        _injectPackage(c, plugin, pkg);

        if (plugin.MainApplication) {
            if (plugin.MainApplication.packages) {
                plugin.MainApplication.packages.forEach((v) => {
                    _injectPackage(c, plugin, v);
                });
            }
        }

        if (plugin.mainApplicationMethods) {
            c.pluginConfigAndroid.mainApplicationMethods += `\n${plugin.mainApplicationMethods}\n`;
        }
    };

    const _injectPackage = (c, plugin, pkg) => {
        if (pkg) c.pluginConfigAndroid.pluginImports += `import ${pkg}\n`;
        let packageParams = '';
        if (plugin.packageParams) {
            packageParams = plugin.packageParams.join(',');
        }

        const className = _extractClassName(pkg);
        if (className) c.pluginConfigAndroid.pluginPackages += `${className}(${packageParams}),\n`;
    };

    const _extractClassName = pkg => (pkg ? pkg.split('.').pop() : null);