Skip to content

Instantly share code, notes, and snippets.

@esinanturan
Forked from cskeppstedt/README.md
Created March 29, 2022 21:27
Show Gist options
  • Save esinanturan/3e510444c602796694dee5943470c742 to your computer and use it in GitHub Desktop.
Save esinanturan/3e510444c602796694dee5943470c742 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
);
@dev-shahbazshaikh
Copy link

What is current situation? Does it still works? Also config in app.json have changed. Using expo build properties, there is extraProguardRules key that can be configured insid android, but it only takes a single string.

@dev-shahbazshaikh
Copy link

`
export default {
expo: {
name: "My Project",
// ...
plugins: [
[
"expo-build-properties" ,
{
"android": {
"enableProguardInReleaseBuilds": true,
"extraProguardRules": " - keep single string only"
}
}
]
// ...
],

},
};
`

@angel-rs
Copy link

@dev-shahbazshaikh Has that setting worked for you?

Once I enable it, my app stays as a blank screen. (Only using enableProguardInReleaseBuilds) I suppose proguard is removing something that it shouldn't, but no clue on how to address it.

@suhitaga
Copy link

"extraProguardRules": " - keep single string only" } } ] // ... ],

@dev-shahbazshaikh you can add multiple lines to the extraProguardRules using:

extraProguardRules: "-keep class com.facebook.**\n-keep class com.facebook.react.**" // among other lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment