Last active
February 7, 2024 21:52
-
-
Save salman-ibrahim/660008506b25f164633bbb393244e013 to your computer and use it in GitHub Desktop.
Appodeal Expo Support Plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<network-security-config> | |
<base-config cleartextTrafficPermitted="true"> | |
<trust-anchors> | |
<certificates src="system" /> | |
</trust-anchors> | |
</base-config> | |
<domain-config cleartextTrafficPermitted="true"> | |
<domain includeSubdomains="true">127.0.0.1</domain> | |
</domain-config> | |
</network-security-config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// update root level build.gradle file (module:project) | |
const { withProjectBuildGradle } = require('@expo/config-plugins'); | |
module.exports = function withAppodealProjectGradle (config) { | |
return withProjectBuildGradle(config, (config) => { | |
config.modResults = modifyRootGradle(config.modResults); | |
return config; | |
}); | |
} | |
function modifyRootGradle(gradle) { | |
const jitpackRepository = "maven { url 'https://www.jitpack.io' }"; | |
const appodealRepository = "maven { url 'https://artifactory.appodeal.com/appodeal' }"; | |
try { | |
if (gradle.contents.includes(jitpackRepository) && !gradle.contents.includes(appodealRepository)) { | |
gradle.contents = gradle.contents.replace( | |
jitpackRepository, | |
`${jitpackRepository}\n ${appodealRepository}` | |
); | |
} | |
else { | |
console.error('Failed to modify root Gradle: Jitpack repository not found'); | |
} | |
} catch (error) { | |
console.error('Failed to modify root Gradle:', error); | |
} | |
return gradle; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// update app level build.gradle file (module:app) | |
const { withAppBuildGradle } = require('@expo/config-plugins'); | |
module.exports = function withAppodealGradle (config) { | |
return withAppBuildGradle(config, (config) => { | |
config.modResults = modifyAppGradle(config.modResults); | |
return config; | |
}); | |
} | |
function modifyAppGradle(gradle) { | |
const dependenciesPattern = /dependencies\s*{([\s\S]*?)}/; | |
const implementationPattern = /\s*implementation\s*([^\n]*)\n/; | |
// Find the 'dependencies' block | |
const dependenciesMatch = gradle.contents.match(dependenciesPattern); | |
if (dependenciesMatch) { | |
const dependenciesBlock = dependenciesMatch[1]; | |
// Find the first 'implementation' line within 'dependencies' | |
const implementationMatch = dependenciesBlock.match(implementationPattern); | |
if (implementationMatch) { | |
const existingImplementationLine = implementationMatch[0]; | |
// Insert the new implementation statement below the existing one | |
const appodealImplementationBlock = `\n implementation("com.appodeal.ads:sdk:3.2.0.+")\n`; | |
// const multidexImplementationLine = `\n implementation("com.android.support:multidex:1.0.3")\n`; | |
gradle.contents = gradle.contents.replace( | |
existingImplementationLine, | |
existingImplementationLine + appodealImplementationBlock | |
); | |
} | |
} | |
return gradle; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// update AndroidManifest.xml | |
const { withAndroidManifest } = require('@expo/config-plugins'); | |
module.exports = function withAppodealManifest(config, metaData) { | |
return withAndroidManifest(config, (config) => { | |
config.modResults = modifyAndroidManifest(config.modResults, metaData); | |
return config; | |
}); | |
} | |
function modifyAndroidManifest(androidManifest, metaData) { | |
const application = getApplication(androidManifest); | |
// Iterate over each key-value pair in manifestConfig.metaData | |
if (metaData.metaData) { | |
Object.entries(metaData.metaData).forEach(([key, value]) => { | |
const existingMetaData = findMetaData(application, key); | |
if (existingMetaData) { | |
existingMetaData.$['android:value'] = value; | |
} | |
else { | |
application['meta-data'].push({ | |
$: { | |
'android:name': key, | |
'android:value': value, | |
}, | |
}); | |
} | |
}); | |
} | |
return androidManifest; | |
} | |
function findMetaData(application, key) { | |
const metaDatas = application['meta-data']; | |
if (metaDatas) { | |
return metaDatas.find((metaData) => metaData.$['android:name'] === key); | |
} | |
return null; | |
} | |
function getApplication(androidManifest) { | |
return androidManifest.manifest.application[0]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add network security config xml | |
const {AndroidConfig, withAndroidManifest } = require('@expo/config-plugins'); | |
const {Paths} = require('@expo/config-plugins/build/android'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const fsPromises = fs.promises; | |
const { getMainApplicationOrThrow} = AndroidConfig.Manifest | |
const withTrustLocalCerts = config => { | |
return withAndroidManifest(config, async config => { | |
config.modResults = await setCustomConfigAsync(config, config.modResults); | |
return config; | |
}); | |
} | |
async function setCustomConfigAsync( | |
config, | |
androidManifest | |
) { | |
const src_file_pat = path.join(__dirname, "network_security_config.xml"); | |
const res_file_path = path.join(await Paths.getResourceFolderAsync(config.modRequest.projectRoot), | |
"xml", "network_security_config.xml"); | |
const res_dir = path.resolve(res_file_path, ".."); | |
if (!fs.existsSync(res_dir)) { | |
await fsPromises.mkdir(res_dir); | |
} | |
try { | |
await fsPromises.copyFile(src_file_pat, res_file_path); | |
} catch (e) { | |
throw e; | |
} | |
const mainApplication = getMainApplicationOrThrow(androidManifest); | |
mainApplication.$["android:networkSecurityConfig"] = "@xml/network_security_config"; | |
return androidManifest; | |
} | |
module.exports = withTrustLocalCerts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the help, but it didn't work.
Could you give me a contact so we can try together and I can pay you?