Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Expo android config plugins for onNewIntent #559

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions expo/withAppsFlyer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const withAppsFlyerIos = require('./withAppsFlyerIos');
module.exports = function withAppsFlyer(config, { shouldUseStrictMode = false }) {
config = withAppsFlyerIos(config, shouldUseStrictMode);
return config;
const withAppsFlyerIos = require("./withAppsFlyerIos");
const withAppsFlyerAndroid = require("./withAppsFlyerAndroid");
module.exports = function withAppsFlyer(
config,
{ shouldUseStrictMode = false }
) {
config = withAppsFlyerIos(config, shouldUseStrictMode);
config = withAppsFlyerAndroid(config);
return config;
};
65 changes: 65 additions & 0 deletions expo/withAppsFlyerAndroid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const {
mergeContents,
} = require("@expo/config-plugins/build/utils/generateCode");
const { withDangerousMod } = require("@expo/config-plugins");
const fs = require("fs");
const path = require("path");

async function readFileAsync(path) {
return fs.promises.readFile(path, "utf8");
}

async function saveFileAsync(path, content) {
return fs.promises.writeFile(path, content, "utf8");
}

function getPath(root, bundleId) {
return path.join(
root,
"app/src/main/java",
...bundleId.split("."),
"MainActivity.kt"
);
}

const withOnNewIntent = (config) => {
return withDangerousMod(config, [
"android",
async (mod) => {
const path = getPath(
mod.modRequest.platformProjectRoot,
mod.android.package
);
const file = await readFileAsync(path);
const updatedWithIntentOverride = mergeContents({
src: file,
newSrc: ` override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
}`,
// inserts it after the first function in the activity class
anchor: /\}/,
offset: 2,
tag: "appsflyer-on-new-intent",
comment: "//",
});
// import android.content.Intent;
const updatedWithImport = mergeContents({
src: updatedWithIntentOverride.contents,
newSrc: `import android.content.Intent`,
// import as second line in the file
anchor: /package/,
offset: 1,
tag: "appsflyer-import-intent",
comment: "//",
});
await saveFileAsync(path, updatedWithImport.contents);
return mod;
},
]);
};

module.exports = function withAppsFlyerAndroid(config) {
config = withOnNewIntent(config);
return config;
};