diff --git a/cli/src/android/update.ts b/cli/src/android/update.ts index 16e8c4935f..1ec51fe81e 100644 --- a/cli/src/android/update.ts +++ b/cli/src/android/update.ts @@ -41,6 +41,7 @@ export async function updateAndroid(config: Config): Promise { if (cordovaPlugins.length > 0) { await copyPluginsNativeFiles(config, cordovaPlugins); } + await copyPluginsNativeFiles(config, capacitorPlugins); if (!(await pathExists(config.android.webDirAbs))) { await copyTask(config, platform); } @@ -139,7 +140,7 @@ export async function installGradlePlugins( if (!capacitorAndroidPackagePath) { fatal( `Unable to find ${c.strong('node_modules/@capacitor/android')}.\n` + - `Are you sure ${c.strong('@capacitor/android')} is installed?`, + `Are you sure ${c.strong('@capacitor/android')} is installed?`, ); } @@ -152,19 +153,19 @@ export async function installGradlePlugins( include ':capacitor-android' project(':capacitor-android').projectDir = new File('${relativeCapcitorAndroidPath}') ${capacitorPlugins - .map((p) => { - if (!p.android) { - return ''; - } + .map((p) => { + if (!p.android) { + return ''; + } - const relativePluginPath = convertToUnixPath(relative(settingsPath, p.rootPath)); + const relativePluginPath = convertToUnixPath(relative(settingsPath, p.rootPath)); - return ` + return ` include ':${getGradlePackageName(p.id)}' project(':${getGradlePackageName(p.id)}').projectDir = new File('${relativePluginPath}/${p.android.path}') `; - }) - .join('')}`; + }) + .join('')}`; const applyArray: any[] = []; const frameworksArray: any[] = []; @@ -204,10 +205,10 @@ android { apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" dependencies { ${capacitorPlugins - .map((p) => { - return ` implementation project(':${getGradlePackageName(p.id)}')`; - }) - .join('\n')} + .map((p) => { + return ` implementation project(':${getGradlePackageName(p.id)}')`; + }) + .join('\n')} ${frameworkString} } ${applyArray.join('\n')} diff --git a/cli/src/plugin.ts b/cli/src/plugin.ts index 884bba998a..daa10d6816 100644 --- a/cli/src/plugin.ts +++ b/cli/src/plugin.ts @@ -72,6 +72,9 @@ export async function resolvePlugin(config: Config, name: string): Promise + + Capacitor Resource Plugin + + + + +`; + +const CAPACITOR_PLUGIN_PACKAGE = ` +{ + "name": "${CAPACITOR_PLUGIN_ID}", + "version": "1.0.0", + "description": "Capacitor Resource Plugin", + "capacitor": { + "android": { + "src": "android" + } + }, + "main": "plugin.js" +} +`; + +const AUTOMOTIVE_APP_DESC = ` + + + + +`; + +async function makeCapacitorPlugin(pluginPath: string) { + const androidPath = join(pluginPath, 'android/src/main'); + const resPath = join(androidPath, 'res/xml'); + await mkdirp(resPath); + await writeFile(join(pluginPath, 'plugin.js'), CAPACITOR_PLUGIN_JS); + await writeFile(join(pluginPath, 'plugin.xml'), CAPACITOR_PLUGIN_XML); + await writeFile(join(pluginPath, 'package.json'), CAPACITOR_PLUGIN_PACKAGE); + await writeFile(join(resPath, 'automotive_app_desc.xml'), AUTOMOTIVE_APP_DESC); + + // Create dummy android project structure for the plugin so it looks valid + await writeFile(join(androidPath, 'AndroidManifest.xml'), ''); +} + +describe('Update: Android Resources', () => { + let appDirObj: any; + let appDir: string; + let FS: MappedFS; + + beforeAll(async () => { + jest.setTimeout(150000); + appDirObj = await makeAppDir(); + appDir = appDirObj.appDir; + + // Create and install custom capacitor plugin + const pluginPath = join(appDirObj.path, CAPACITOR_PLUGIN_ID); + await makeCapacitorPlugin(pluginPath); + + await runCommand('npm', ['install', '--save', pluginPath], { + cwd: appDir, + }); + + await run(appDir, `init "${APP_NAME}" "${APP_ID}"`); + await installPlatform(appDir, 'android'); + await run(appDir, `add android`); + FS = new MappedFS(appDir); + }); + + afterAll(() => { + //appDirObj.cleanupCallback(); + }); + + it('Should copy resource-file from Capacitor plugin', async () => { + // Run update to trigger the copy + await run(appDir, `update android`); + + const resourcePath = 'android/capacitor-cordova-android-plugins/src/main/res/xml/automotive_app_desc.xml'; + const exists = await FS.exists(resourcePath); + expect(exists).toBe(true); + + if (exists) { + const content = await FS.read(resourcePath); + expect(content).toContain(''); + } + }); +});