diff --git a/packages/react-native-adjust/README.md b/packages/react-native-adjust/README.md index ebec87a7..540795d6 100644 --- a/packages/react-native-adjust/README.md +++ b/packages/react-native-adjust/README.md @@ -39,4 +39,22 @@ If you are targeting Android 12 and above (API level 31), you need to add it in This will add the [appropriate permission](https://github.com/adjust/react_native_sdk#add-permission-to-gather-google-advertising-id) for you. +To set up Adjust [SDK Signature](https://github.com/adjust/react_native_sdk/blob/master/README.md#sdk-signature) you need to specify the path for the xcframework and the aar file. +```json +{ + "expo": { + "plugins": [ + "@config-plugins/react-native-adjust", + { + "targetAndroid12": true , + "sdkSignature" : { + "android" : "./path/to.aar", + "ios" : "./path/to.xcframework" + } + } + ] + } +} +``` + Next, rebuild your app as described in the ["Adding custom native code"](https://docs.expo.io/workflow/customizing/) guide. diff --git a/packages/react-native-adjust/src/withReactNativeAdjust.ts b/packages/react-native-adjust/src/withReactNativeAdjust.ts index 8f1cb45e..287f22d5 100644 --- a/packages/react-native-adjust/src/withReactNativeAdjust.ts +++ b/packages/react-native-adjust/src/withReactNativeAdjust.ts @@ -6,7 +6,10 @@ import { IOSConfig, withAppBuildGradle, withXcodeProject, + withDangerousMod, } from "expo/config-plugins"; +import fs from "fs"; +import path from "path"; const withXcodeLinkBinaryWithLibraries: ConfigPlugin<{ library: string; @@ -44,30 +47,154 @@ const addAndroidPackagingOptions = (src: string) => { }); }; -const withGradle: ConfigPlugin = (config) => { +const addSdkSignatureDependency = (src: string) => { + return mergeContents({ + tag: "react-native-adjust arr dependency", + src, + newSrc: "implementation files('libs/adjust-lib.aar')", + anchor: /dependencies(?:\s+)?\{/, + // Inside the dependencies block. + offset: 1, + comment: "//", + }); +}; + +const addNdkAbiFilters = (src: string) => { + return mergeContents({ + tag: "react-native-adjust NDK abi filters", + src, + newSrc: "ndk.abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'", + anchor: /defaultConfig(?:\s+)?\{/, + // Inside the dependencies block. + offset: 1, + comment: "//", + }); +}; + +const withGradle: ConfigPlugin<{ isSdkSignatureSupported: boolean }> = ( + config, + { isSdkSignatureSupported } +) => { return withAppBuildGradle(config, (config) => { - if (config.modResults.language === "groovy") { - config.modResults.contents = addAndroidPackagingOptions( - config.modResults.contents - ).contents; - } else { + if (config.modResults.language !== "groovy") { throw new Error( "Cannot add Play Services maven gradle because the project build.gradle is not groovy" ); } + + if (isSdkSignatureSupported) { + config.modResults.contents = addNdkAbiFilters( + config.modResults.contents + ).contents; + + config.modResults.contents = addSdkSignatureDependency( + config.modResults.contents + ).contents; + } + + config.modResults.contents = addAndroidPackagingOptions( + config.modResults.contents + ).contents; + + return config; + }); +}; + +const withAndroidSdkSignature: ConfigPlugin<{ sdkSignaturePath: string }> = ( + config, + props +) => { + return withDangerousMod(config, [ + "android", + async (config) => { + const projectRoot = config.modRequest.projectRoot; + const libPath = path.join(projectRoot, props.sdkSignaturePath); + + if (!fs.existsSync(libPath)) { + throw new Error("Cannot find Adjust Android sdk signature library !"); + } + + const libsDirectoryPath = path.join(projectRoot, "android/app/libs"); + + if (!fs.existsSync(libsDirectoryPath)) { + fs.mkdirSync(libsDirectoryPath); + } + + fs.cpSync(libPath, path.join(libsDirectoryPath, "adjust-lib.aar")); + + return config; + }, + ]); +}; + +const withIosSdkSignature: ConfigPlugin<{ sdkSignaturePath: string }> = ( + config, + props +) => { + return withXcodeProject(config, (config) => { + const projectRoot = config.modRequest.projectRoot; + const libPath = path.join(projectRoot, props.sdkSignaturePath); + + if (!fs.existsSync(libPath)) { + throw new Error("Cannot find Adjust iOS sdk signature library !"); + } + + const newLibPath = path.join( + projectRoot, + "ios", + config.modRequest.projectName!, + path.basename(libPath) + ); + const target = IOSConfig.XcodeUtils.getApplicationNativeTarget({ + project: config.modResults, + projectName: config.modRequest.projectName!, + }); + + fs.cpSync(libPath, newLibPath, { recursive: true }); + + const embedFrameworksBuildPhase = + config.modResults.pbxEmbedFrameworksBuildPhaseObj(target.uuid); + + if (!embedFrameworksBuildPhase) { + config.modResults.addBuildPhase( + [], + "PBXCopyFilesBuildPhase", + "Embed Frameworks", + target.uuid, + "frameworks" + ); + } + + config.modResults.addFramework(newLibPath, { + target: target.uuid, + embed: true, + sign: true, + customFramework: true, + }); + return config; }); }; +type Props = { + targetAndroid12?: boolean; + sdkSignature?: { + ios?: string; + android?: string; + }; +}; + /** * Apply react-native-adjust configuration for Expo SDK +44 projects. */ -const withAdjustPlugin: ConfigPlugin = ( - config, - _props -) => { +const withAdjustPlugin: ConfigPlugin = (config, _props) => { const props = _props || {}; + const androidSdkSignaturePath = props.sdkSignature?.android ?? ""; + const iosSdkSignaturePath = props.sdkSignature?.ios ?? ""; + const isAndroidSdkSignatureSupported = androidSdkSignaturePath !== ""; + const isIosSdkSignatureSupported = iosSdkSignaturePath !== ""; + config = withXcodeLinkBinaryWithLibraries(config, { library: "iAd.framework", status: "optional", @@ -93,13 +220,27 @@ const withAdjustPlugin: ConfigPlugin = ( status: "optional", }); + if (isIosSdkSignatureSupported) { + config = withIosSdkSignature(config, { + sdkSignaturePath: iosSdkSignaturePath, + }); + } + if (props.targetAndroid12) { config = AndroidConfig.Permissions.withPermissions(config, [ "com.google.android.gms.permission.AD_ID", ]); } - config = withGradle(config); + config = withGradle(config, { + isSdkSignatureSupported: isAndroidSdkSignatureSupported, + }); + + if (isAndroidSdkSignatureSupported) { + config = withAndroidSdkSignature(config, { + sdkSignaturePath: androidSdkSignaturePath, + }); + } // Return the modified config. return config;