Skip to main content

How to modify build.gradle file

In this guide, you will learn how to modify the build.gradle file using expo-config-plugins.

in your app.plugin.js file, you can write below to add new implementation in build.gradle file.

const { withAppBuildGradle } = require('@expo/config-plugins');
const PATH_TO_ADD = `implementation("com.facebook.react:react-android")`;
const withCustomAppBuildGradleImplementation = (
config,
{ implementationPackage },
) => {
return withAppBuildGradle(config, (config) => {
if (config.modResults.contents.includes(implementationPackage)) {
return config;
}
config.modResults.contents = config.modResults.contents.replace(
PATH_TO_ADD,
`${PATH_TO_ADD}
${implementationPackage}`,
);
return config;
});
};

module.exports = function withCustom(config) {
config = withCustomAppBuildGradleImplementation(config, {
implementationPackage: 'implementation("com.custom:app-android-sdk:12.5.06")',
});
return config;
};

In the above code, we are adding a new implementation in the build.gradle file. How it works is, it first checks if the implementation is already added in the file. If it is not added, then it adds the implementation in the file. using the withAppBuildGradle method.

inside withAppBuildGradle method, we are finding the the string implementation("com.facebook.react:react-android") and adding the new implementation below it.