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 ! ]); };