Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add google places sdk to app/build.gradle with expo config plugin #1564

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ Next, add:
"@stripe/stripe-react-native",
{
"merchantIdentifier": string | string [],
"enableGooglePay": boolean
"enableGooglePay": boolean,
"enableGooglePlacesSdk": boolean
}
]
],
Expand Down
41 changes: 41 additions & 0 deletions src/plugin/__tests__/fixtures/sample-build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")

def isGifEnabled = (findProperty('expo.gif.enabled') ?: "") == "true";
def isWebpEnabled = (findProperty('expo.webp.enabled') ?: "") == "true";
def isWebpAnimatedEnabled = (findProperty('expo.webp.animated') ?: "") == "true";
def frescoVersion = rootProject.ext.frescoVersion

// If your app supports Android versions before Ice Cream Sandwich (API level 14)
if (isGifEnabled || isWebpEnabled) {
implementation("com.facebook.fresco:fresco:${frescoVersion}")
implementation("com.facebook.fresco:imagepipeline-okhttp3:${frescoVersion}")
}

if (isGifEnabled) {
// For animated gif support
implementation("com.facebook.fresco:animated-gif:${frescoVersion}")
}

if (isWebpEnabled) {
// For webp support
implementation("com.facebook.fresco:webpsupport:${frescoVersion}")
if (isWebpAnimatedEnabled) {
// Animated webp support
implementation("com.facebook.fresco:animated-webp:${frescoVersion}")
}
}

debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
exclude group:'com.squareup.okhttp3', module:'okhttp'
}
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
implementation jscFlavor
}
}
19 changes: 18 additions & 1 deletion src/plugin/__tests__/withStripe-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AndroidConfig } from '@expo/config-plugins';
import { resolve } from 'path';

import { setApplePayEntitlement, setGooglePayMetaData } from '../withStripe';
import {
setApplePayEntitlement,
setGooglePayMetaData,
addGooglePlacesSdk,
} from '../withStripe';

jest.mock(
'@stripe/stripe-react-native/package.json',
Expand All @@ -17,6 +21,7 @@ const { getMainApplicationOrThrow, readAndroidManifestAsync } =

const fixturesPath = resolve(__dirname, 'fixtures');
const sampleManifestPath = resolve(fixturesPath, 'sample-AndroidManifest.xml');
const sampleBuildGradlePath = resolve(fixturesPath, 'sample-build.gradle');

describe('setApplePayEntitlement', () => {
it(`sets the apple pay entitlement when none exist`, () => {
Expand Down Expand Up @@ -109,3 +114,15 @@ describe('setGooglePayMetaData', () => {
expect(apiKeyItem).toHaveLength(0);
});
});

describe('addGooglePlacesSdk', () => {
it(`Properly adds Google Places SDK to build.gradle`, async () => {
let buildGradle = AndroidConfig.Paths.getFileInfo(sampleBuildGradlePath);
buildGradle.contents = addGooglePlacesSdk(buildGradle.contents);
expect(
buildGradle.contents.includes(
'com.google.android.libraries.places:places:2.6.0'
)
).toBe(true);
});
});
45 changes: 43 additions & 2 deletions src/plugin/withStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IOSConfig,
withAndroidManifest,
withEntitlementsPlist,
withAppBuildGradle,
} from '@expo/config-plugins';

const {
Expand All @@ -22,6 +23,7 @@ type StripePluginProps = {
*/
merchantIdentifier: string | string[];
enableGooglePay: boolean;
enableGooglePlacesSdk: boolean;
};

const withStripe: ConfigPlugin<StripePluginProps> = (config, props) => {
Expand Down Expand Up @@ -94,16 +96,25 @@ export const withNoopSwiftFile: ConfigPlugin = (config) => {

const withStripeAndroid: ConfigPlugin<StripePluginProps> = (
expoConfig,
{ enableGooglePay = false }
{ enableGooglePay = false, enableGooglePlacesSdk = false }
) => {
return withAndroidManifest(expoConfig, (config) => {
expoConfig = withAndroidManifest(expoConfig, (config) => {
config.modResults = setGooglePayMetaData(
enableGooglePay,
config.modResults
);

return config;
});

return withAppBuildGradle(expoConfig, (config) => {
if (enableGooglePlacesSdk) {
config.modResults.contents = addGooglePlacesSdk(
config.modResults.contents
);
}
return config;
});
};

/**
Expand Down Expand Up @@ -138,4 +149,34 @@ export function setGooglePayMetaData(
return modResults;
}

/**
* Adds the following to app/build.gradle:
*
* dependencies {
* implementation 'com.google.android.libraries.places:places:2.6.0'
* ...
* }
*/
export function addGooglePlacesSdk(buildGradle: string) {
const dependency = 'com.google.android.libraries.places:places:2.6.0';

if (buildGradle.includes(dependency)) {
return buildGradle;
}

const impl = `implementation '${dependency}'`;

if (buildGradle.includes('dependencies {')) {
return buildGradle.replace(
'dependencies {',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't some apps' build.gradle have >1 dependencies {} block?

`dependencies {
// Added by @stripe/stripe-react-native
${impl}
`
);
} else {
return buildGradle;
}
}

export default createRunOncePlugin(withStripe, pkg.name, pkg.version);