Skip to content

Instantly share code, notes, and snippets.

@cskeppstedt
Created March 13, 2022 22:05
Show Gist options
  • Save cskeppstedt/d44f043d7c621c4c800a66c9656b1d87 to your computer and use it in GitHub Desktop.
Save cskeppstedt/d44f043d7c621c4c800a66c9656b1d87 to your computer and use it in GitHub Desktop.
Modify proguard rules in a managed expo project

Modify proguard rules in a managed expo project

If you ever need to modify the proguard rules for the Android-part of your Expo managed workflow, you can achieve this by using a config plugin.

  1. Make a folder for config-plugins called ./plugins in the project root folder.
  2. Place the withProguardRules.js and my-proguard-rules.pro in the folder
  3. Add the config plugin to the plugins array of your app.config.js (or app.json if you're using that instead).

NOTE: if you rename your .pro file, don't forget to change the two occurrences of my-proguard-rules in withProguardRules.js as well.

Attribution

This solution was inspired by the detox config-plugin

export default {
expo: {
name: "My Project",
// ...
plugins: [
"./plugins/withProguardRules",
// ...
],
},
};
-keep class com.swmansion.reanimated.** { *; }
-keep class com.facebook.react.turbomodule.** { *; }
const { createRunOncePlugin, withAppBuildGradle } = require("@expo/config-plugins");
const pkg = require("../package.json");
const withProguardRules = (config) => {
return withAppBuildGradle(config, (config) => {
if (config.modResults.language === "groovy") {
config.modResults.contents = addProguardRules(config.modResults.contents);
} else {
throw new Error(
"Cannot add to maven gradle because the project build.gradle is not groovy"
);
}
return config;
});
};
function addProguardRules(buildGradleContent) {
const RE_EXISTS = /my-proguard-rules\.pro/g;
if (RE_EXISTS.test(buildGradleContent)) {
return buildGradleContent;
}
const RE_ENTRY = /proguardFiles getDefaultProguardFile\("proguard-android.txt"\),\s?"proguard-rules.pro"/;
if (!RE_ENTRY.test(buildGradleContent)) {
throw new Error(
"Cannot add to maven gradle because the proguard regex could not find the entrypoint row"
);
}
return buildGradleContent.replace(
RE_ENTRY,
`proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
// Project-specific additions to proguard
def myProguardRulesPath = new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../plugins/my-proguard-rules.pro")
proguardFile(myProguardRulesPath)
`
);
}
module.exports = createRunOncePlugin(
withProguardRules,
`${pkg.name}-withProguardRules`,
pkg.version
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment