Skip to content

Commit

Permalink
Add Android support
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelien-iapp committed Sep 6, 2024
1 parent 673eb65 commit 94d6a2c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
54 changes: 54 additions & 0 deletions plugin/android/app-build-gradle.js
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 5 additions & 0 deletions plugin/android/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { withAppAuthAppBuildGradle } = require('./app-build-gradle');

module.exports = {
withAppAuthAppBuildGradle,
};
4 changes: 4 additions & 0 deletions plugin/index.js
Original file line number Diff line number Diff line change
@@ -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 !
]);
};

Expand Down

0 comments on commit 94d6a2c

Please sign in to comment.