Skip to content

Commit f04fe97

Browse files
committed
Add Android support
1 parent 33eedd5 commit f04fe97

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

plugin/android/app-build-gradle.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { AndroidConfig, withDangerousMod } = require('@expo/config-plugins');
2+
const {
3+
createGeneratedHeaderComment,
4+
removeContents,
5+
} = require('@expo/config-plugins/build/utils/generateCode');
6+
const codeModAndroid = require('@expo/config-plugins/build/android/codeMod');
7+
const fs = require('fs');
8+
9+
const withAppAuthAppBuildGradle = (rootConfig, options) =>
10+
withDangerousMod(rootConfig, [
11+
'android',
12+
config => {
13+
// detauls to app scheme
14+
const authScheme = options?.authScheme ?? config.scheme ?? '';
15+
16+
// find the app/build.gradle file and checks its format
17+
const appBuildGradlePath = AndroidConfig.Paths.getAppBuildGradleFilePath(
18+
config.modRequest.projectRoot
19+
);
20+
21+
// BEWARE: we update the app/build.gradle file *outside* of the standard Expo config procedure !
22+
let contents = fs.readFileSync(appBuildGradlePath, 'utf-8');
23+
24+
if (contents.includes('manifestPlaceholders')) {
25+
throw new Error(
26+
'app/build.gradle already contains manifestPlaceholders, cannot update automatically !'
27+
);
28+
}
29+
30+
// let's add the manifestPlaceholders section !
31+
contents = removeContents({
32+
src: contents,
33+
tag: 'react-native-app-auth',
34+
}).contents;
35+
contents = codeModAndroid.appendContentsInsideDeclarationBlock(
36+
contents,
37+
'defaultConfig',
38+
`
39+
${createGeneratedHeaderComment(contents, 'react-native-app-auth', '//')}
40+
manifestPlaceholders = [
41+
'appAuthRedirectScheme': '${authScheme}',
42+
]
43+
// @generated end react-native-app-auth
44+
`
45+
);
46+
47+
// and finally we write the file back to the disk
48+
fs.writeFileSync(appBuildGradlePath, contents, 'utf-8');
49+
50+
return config;
51+
},
52+
]);
53+
54+
module.exports = { withAppAuthAppBuildGradle };

plugin/android/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { withAppAuthAppBuildGradle } = require('./app-build-gradle');
2+
3+
module.exports = {
4+
withAppAuthAppBuildGradle,
5+
};

plugin/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const { withPlugins, createRunOncePlugin } = require('@expo/config-plugins');
22
const { withAppAuthAppDelegate, withAppAuthAppDelegateHeader } = require('./ios');
3+
const { withAppAuthAppBuildGradle } = require('./android');
34

45
const withAppAuth = config => {
56
return withPlugins(config, [
67
// iOS
78
withAppAuthAppDelegate,
89
withAppAuthAppDelegateHeader, // 👈 ️this one uses withDangerousMod !
10+
11+
// Android
12+
withAppAuthAppBuildGradle, // 👈 ️this one uses withDangerousMod !
913
]);
1014
};
1115

0 commit comments

Comments
 (0)