Skip to content

Instantly share code, notes, and snippets.

@hassankhan
Created November 17, 2024 13:31
Show Gist options
  • Save hassankhan/9b5e26ab611010e28d4c84a365552d24 to your computer and use it in GitHub Desktop.
Save hassankhan/9b5e26ab611010e28d4c84a365552d24 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const {
AndroidConfig,
WarningAggregator,
withAndroidManifest,
withAppBuildGradle,
withDangerousMod,
} = require('@expo/config-plugins');
const {
mergeContents,
} = require('@expo/config-plugins/build/utils/generateCode');
const withAndroidCompileOptions = (config) => {
return withAppBuildGradle(config, (config) => {
if (config.modResults.language === 'groovy') {
config.modResults.contents = mergeContents({
tag: 'react-native-track-player-android-auto-compile-opts',
src: config.modResults.contents,
newSrc: `
compileOptions {
coreLibraryDesugaringEnabled true
}
`,
anchor: / packagingOptions {/,
// Inside the packagingOptions block.
offset: 0,
comment: '//',
}).contents;
} else {
WarningAggregator.addWarningAndroid(
'react-native-track-player',
`Cannot automatically configure app build.gradle if it's not groovy`
);
}
return config;
});
};
const withAndroidDependencies = (config) => {
return withAppBuildGradle(config, (config) => {
if (config.modResults.language === 'groovy') {
config.modResults.contents = mergeContents({
tag: 'react-native-track-player-android-auto-deps',
src: config.modResults.contents,
newSrc: `
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2'
`,
anchor: /dependencies {/,
// Inside the packagingOptions block.
offset: 1,
comment: '//',
}).contents;
} else {
WarningAggregator.addWarningAndroid(
'react-native-track-player',
`Cannot automatically configure app build.gradle if it's not groovy`
);
}
return config;
});
};
/**
* Create `automotive_app_desc.xml` resource file.
*/
const withAndroidAutoAppDescriptorFile = (config) => {
const template = `<automotiveApp><uses name="media"/></automotiveApp>`;
return withDangerousMod(config, [
'android',
async (config) => {
const folder = path.join(
config.modRequest.platformProjectRoot,
`app/src/main/res/xml`
);
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(path.join(folder, 'automotive_app_desc.xml'), template, {
encoding: 'utf8',
});
return config;
},
]);
};
const withAndroidAutoManifest = (config) => {
return withAndroidManifest(config, async (config) => {
const manifest = config.modResults;
const application =
AndroidConfig.Manifest.getMainApplicationOrThrow(manifest);
const appMeta = application['meta-data'] ?? [];
appMeta.push({
$: {
'android:name': 'com.google.android.gms.car.application',
'android:resource': '@xml/automotive_app_desc',
},
});
config.modResults = manifest;
return config;
});
};
const withAndroidAuto = (config) => {
config = withAndroidCompileOptions(config);
config = withAndroidDependencies(config);
config = withAndroidAutoAppDescriptorFile(config);
config = withAndroidAutoManifest(config);
return config;
};
const withReactNativeTrackPlayer = (config) => {
config = withAndroidAuto(config);
return config;
};
module.exports = withReactNativeTrackPlayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment