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

feat(adjust): add support for sdk signature #172

Open
wants to merge 1 commit into
base: main
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
18 changes: 18 additions & 0 deletions packages/react-native-adjust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
163 changes: 152 additions & 11 deletions packages/react-native-adjust/src/withReactNativeAdjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<void | { targetAndroid12?: boolean }> = (
config,
_props
) => {
const withAdjustPlugin: ConfigPlugin<void | Props> = (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",
Expand All @@ -93,13 +220,27 @@ const withAdjustPlugin: ConfigPlugin<void | { targetAndroid12?: boolean }> = (
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;
Expand Down