From 94d6a2cda3c0342c2f5bd704e872bac962812363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Noce?= Date: Mon, 23 Oct 2023 17:11:06 +0200 Subject: [PATCH] Add Android support --- plugin/android/app-build-gradle.js | 54 ++++++++++++++++++++++++++++++ plugin/android/index.js | 5 +++ plugin/index.js | 4 +++ 3 files changed, 63 insertions(+) create mode 100644 plugin/android/app-build-gradle.js create mode 100644 plugin/android/index.js diff --git a/plugin/android/app-build-gradle.js b/plugin/android/app-build-gradle.js new file mode 100644 index 00000000..5c1e4cfb --- /dev/null +++ b/plugin/android/app-build-gradle.js @@ -0,0 +1,54 @@ +const { AndroidConfig, withDangerousMod } = require('@expo/config-plugins'); +const { + createGeneratedHeaderComment, + removeContents, +} = require('@expo/config-plugins/build/utils/generateCode'); +const codeModAndroid = require('@expo/config-plugins/build/android/codeMod'); +const fs = require('fs'); + +const withAppAuthAppBuildGradle = (rootConfig, options) => + withDangerousMod(rootConfig, [ + 'android', + config => { + // detauls to app scheme + const authScheme = options?.authScheme ?? config.scheme ?? ''; + + // find the app/build.gradle file and checks its format + const appBuildGradlePath = AndroidConfig.Paths.getAppBuildGradleFilePath( + config.modRequest.projectRoot + ); + + // BEWARE: we update the app/build.gradle file *outside* of the standard Expo config procedure ! + let contents = fs.readFileSync(appBuildGradlePath, 'utf-8'); + + if (contents.includes('manifestPlaceholders')) { + throw new Error( + 'app/build.gradle already contains manifestPlaceholders, cannot update automatically !' + ); + } + + // let's add the manifestPlaceholders section ! + contents = removeContents({ + src: contents, + tag: 'react-native-app-auth', + }).contents; + contents = codeModAndroid.appendContentsInsideDeclarationBlock( + contents, + 'defaultConfig', + ` + ${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')} + manifestPlaceholders = [ + 'appAuthRedirectScheme': '${authScheme}', + ] + // @generated end react-native-app-auth +` + ); + + // and finally we write the file back to the disk + fs.writeFileSync(appBuildGradlePath, contents, 'utf-8'); + + return config; + }, + ]); + +module.exports = { withAppAuthAppBuildGradle }; diff --git a/plugin/android/index.js b/plugin/android/index.js new file mode 100644 index 00000000..9b9a5d7f --- /dev/null +++ b/plugin/android/index.js @@ -0,0 +1,5 @@ +const { withAppAuthAppBuildGradle } = require('./app-build-gradle'); + +module.exports = { + withAppAuthAppBuildGradle, +}; diff --git a/plugin/index.js b/plugin/index.js index 602232c4..b1fe4579 100644 --- a/plugin/index.js +++ b/plugin/index.js @@ -1,11 +1,15 @@ const { withPlugins, createRunOncePlugin } = require('@expo/config-plugins'); const { withAppAuthAppDelegate, withAppAuthAppDelegateHeader } = require('./ios'); +const { withAppAuthAppBuildGradle } = require('./android'); const withAppAuth = config => { return withPlugins(config, [ // iOS withAppAuthAppDelegate, withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod ! + + // Android + withAppAuthAppBuildGradle, // 👈 ️this one uses withDangerousMod ! ]); };