@rediads/react-native-ads 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/LICENSE +201 -0
- package/README.md +246 -2
- package/android/build.gradle +24 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/rediads/reactnative/RediadsAdsModule.kt +195 -0
- package/android/src/main/java/com/rediads/reactnative/RediadsBannerSlot.kt +58 -0
- package/app.plugin.js +2 -0
- package/build/RediadsAds.d.ts +60 -0
- package/build/RediadsAds.js +80 -0
- package/build/RediadsBanner.d.ts +20 -0
- package/build/RediadsBanner.js +38 -0
- package/build/RediadsFullScreenAd.d.ts +60 -0
- package/build/RediadsFullScreenAd.js +133 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +3 -0
- package/build/native.d.ts +40 -0
- package/build/native.js +4 -0
- package/build/types.d.ts +48 -0
- package/build/types.js +1 -0
- package/expo-module.config.json +9 -0
- package/ios/RediadsAds.podspec +31 -0
- package/ios/RediadsAdsModule.swift +211 -0
- package/package.json +51 -4
- package/plugin/withRediadsAds.js +291 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expo config plugin of @rediads/react-native-ads. It runs during `npx expo prebuild` (and
|
|
3
|
+
* `expo run:ios|android`, EAS Build) and points the generated projects at the native Rediads SDKs.
|
|
4
|
+
*
|
|
5
|
+
* plugins: ['@rediads/react-native-ads']
|
|
6
|
+
*
|
|
7
|
+
* With no options (apps):
|
|
8
|
+
* - iOS: the Rediads SDK pods are binaries in Rediads' pod list, github.com/rediads-hq/rediads-ios.
|
|
9
|
+
* The plugin adds it to the Podfile as a `source`, followed by CocoaPods' own (cdn.cocoapods.org):
|
|
10
|
+
* once a Podfile names a source, CocoaPods uses only the named ones.
|
|
11
|
+
* - Android: nothing to do. The SDK is on Maven Central, which Expo's android/build.gradle has.
|
|
12
|
+
*
|
|
13
|
+
* Options, for developing the SDKs themselves (local builds instead of the published ones):
|
|
14
|
+
*
|
|
15
|
+
* plugins: [
|
|
16
|
+
* ['@rediads/react-native-ads', {
|
|
17
|
+
* // A checkout of rediads-ios-sdk: the folder with RediadsDisplay.podspec. Give the AdMob and
|
|
18
|
+
* // GAM plugins the same path.
|
|
19
|
+
* iosSdkPath: '../rediads-ios-sdk',
|
|
20
|
+
* // The Android SDK's Maven folder (`./gradlew publishAllPublicationsToRediadsLocalRepository`
|
|
21
|
+
* // in rediads-android-sdk writes it), or the URL of a Maven repository that has
|
|
22
|
+
* // com.rediads:rediads-display.
|
|
23
|
+
* androidMavenRepo: '../rediads-android-sdk/build/rediads-maven',
|
|
24
|
+
* }],
|
|
25
|
+
* ]
|
|
26
|
+
*
|
|
27
|
+
* Relative paths are from the app's folder (the one with app.json). The local SDKs must have the
|
|
28
|
+
* versions the packages ask for (the podspecs and android/build.gradle).
|
|
29
|
+
*
|
|
30
|
+
* iOS with iosSdkPath: the pods go to the app's own target only (the first `target ... do` with
|
|
31
|
+
* `use_expo_modules!`), not to app extensions, which can't use the SDK (it uses UIApplication.shared).
|
|
32
|
+
* Android with androidMavenRepo: the folder goes to `allprojects.repositories` in android/build.gradle.
|
|
33
|
+
*/
|
|
34
|
+
const {
|
|
35
|
+
AndroidConfig,
|
|
36
|
+
createRunOncePlugin,
|
|
37
|
+
withGradleProperties,
|
|
38
|
+
withPodfile,
|
|
39
|
+
withProjectBuildGradle,
|
|
40
|
+
} = require('expo/config-plugins');
|
|
41
|
+
const fs = require('fs');
|
|
42
|
+
const path = require('path');
|
|
43
|
+
const { pathToFileURL } = require('url');
|
|
44
|
+
|
|
45
|
+
const pkg = require('../package.json');
|
|
46
|
+
|
|
47
|
+
const PODS = ['RediadsEngine', 'RediadsCore', 'RediadsDisplay', 'RediadsOM'];
|
|
48
|
+
const TAG = '@rediads/react-native-ads';
|
|
49
|
+
/** The Podfile block with the pod sources; the AdMob and GAM plugins add the same block. */
|
|
50
|
+
const SOURCES_TAG = '@rediads/react-native-ads-pod-sources';
|
|
51
|
+
/** Rediads' pod list first, then CocoaPods' own, which has everything else. */
|
|
52
|
+
const POD_SOURCES = [
|
|
53
|
+
{ url: 'https://github.com/rediads-hq/rediads-ios.git', pattern: /^\s*source\s+['"]https:\/\/github\.com\/rediads-hq\/rediads-ios(\.git)?['"]/m },
|
|
54
|
+
{ url: 'https://cdn.cocoapods.org/', pattern: /^\s*source\s+['"]https:\/\/cdn\.cocoapods\.org\/?['"]/m },
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
function escapeRegExp(text) {
|
|
58
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Removes the block an earlier prebuild added, so a changed path replaces it. */
|
|
62
|
+
function removeBlock(contents, comment, tag = TAG) {
|
|
63
|
+
const begin = escapeRegExp(`${comment} @generated begin ${tag}`);
|
|
64
|
+
const end = escapeRegExp(`${comment} @generated end ${tag}`);
|
|
65
|
+
// Exact tags (a space follows the tag): '@rediads/react-native-ads-admob' blocks must not match.
|
|
66
|
+
return contents.replace(new RegExp(`[ \\t]*${begin} [\\s\\S]*?${end}\\n`), '');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function block(comment, indent, lines, note, tag = TAG) {
|
|
70
|
+
return [
|
|
71
|
+
`${indent}${comment} @generated begin ${tag} - ${note}`,
|
|
72
|
+
...lines.map((line) => `${indent}${line}`),
|
|
73
|
+
`${indent}${comment} @generated end ${tag}`,
|
|
74
|
+
'',
|
|
75
|
+
].join('\n');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Adds Rediads' pod list and CocoaPods' own as Podfile sources, before `platform :ios` (or the
|
|
80
|
+
* first target). Sources the Podfile already names are left alone; running it again changes nothing.
|
|
81
|
+
*/
|
|
82
|
+
function addSources(podfile) {
|
|
83
|
+
const contents = removeBlock(podfile, '#', SOURCES_TAG);
|
|
84
|
+
const missing = POD_SOURCES.filter(({ pattern }) => !pattern.test(contents));
|
|
85
|
+
if (missing.length === 0) return contents;
|
|
86
|
+
const anchor = /^platform :ios\b/m.test(contents) ? /^platform :ios\b/m : /^target '/m;
|
|
87
|
+
if (!anchor.test(contents)) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`${TAG}: could not find \`platform :ios\` or a target in ios/Podfile, so the Rediads pod sources could not be added.`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const lines = missing.map(({ url }) => `source '${url}'`);
|
|
93
|
+
const text = block('#', '', lines, "Rediads' pod list, then CocoaPods' own", SOURCES_TAG);
|
|
94
|
+
return contents.replace(anchor, (found) => `${text}${found}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function addPods(podfile, sdkPath) {
|
|
98
|
+
const contents = removeBlock(podfile, '#');
|
|
99
|
+
// The app's own target; extension targets (widgets, notification services) come after it.
|
|
100
|
+
const anchor = /^target '[^']+' do\n([ \t]*)use_expo_modules![^\n]*\n/m;
|
|
101
|
+
const match = contents.match(anchor);
|
|
102
|
+
if (!match) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`${TAG}: could not find the app target's \`use_expo_modules!\` in ios/Podfile, so the Rediads pods could not be added.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const lines = PODS.map((name) => `pod '${name}', :path => ${JSON.stringify(sdkPath)}`);
|
|
108
|
+
const text = block('#', match[1], lines, 'Rediads SDK pods from a local checkout');
|
|
109
|
+
return contents.replace(anchor, (found) => `${found}${text}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function addMavenRepo(buildGradle, url) {
|
|
113
|
+
const contents = removeBlock(buildGradle, '//');
|
|
114
|
+
const anchor = /allprojects\s*\{\s*\n[ \t]*repositories\s*\{[^\n]*\n/;
|
|
115
|
+
if (!anchor.test(contents)) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`${TAG}: could not find \`allprojects { repositories { ... } }\` in android/build.gradle, so the Rediads Maven repository could not be added.`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const text = block('//', ' ', [`maven { url '${url}' }`], 'Rediads SDK from a local Maven folder');
|
|
121
|
+
return contents.replace(anchor, (found) => `${found}${text}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function iosSdkFolder(projectRoot, option) {
|
|
125
|
+
const folder = path.resolve(projectRoot, option);
|
|
126
|
+
if (!fs.existsSync(path.join(folder, 'RediadsDisplay.podspec'))) {
|
|
127
|
+
throw new Error(`${TAG}: iosSdkPath ${folder} has no RediadsDisplay.podspec.`);
|
|
128
|
+
}
|
|
129
|
+
return folder;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function mavenUrl(projectRoot, option) {
|
|
133
|
+
if (/^(https?|file):/.test(option)) return option;
|
|
134
|
+
const folder = path.resolve(projectRoot, option);
|
|
135
|
+
if (!fs.existsSync(path.join(folder, 'com', 'rediads', 'rediads-display'))) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
`${TAG}: androidMavenRepo ${folder} has no com/rediads/rediads-display. Build it with ./gradlew publishAllPublicationsToRediadsLocalRepository in rediads-android-sdk.`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return pathToFileURL(folder).href;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const GOOGLE_MOBILE_ADS = 'react-native-google-mobile-ads';
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Google's app IDs for the AdMob and GAM plugins (one call each), in apps that may also use
|
|
147
|
+
* react-native-google-mobile-ads. Google's SDK reads one app ID per platform, so an ID given only to
|
|
148
|
+
* that library's config plugin is used here too, and two different IDs are an error. `withGoogleMobileAds`
|
|
149
|
+
* says whether that plugin is in the app's plugin list.
|
|
150
|
+
*
|
|
151
|
+
* @param {import('expo/config').ExpoConfig} config
|
|
152
|
+
* @param {string} tag the calling plugin's name, for errors
|
|
153
|
+
* @param {{ iosAppId?: string, androidAppId?: string }} ids the calling plugin's options
|
|
154
|
+
*/
|
|
155
|
+
function googleAppIds(config, tag, ids) {
|
|
156
|
+
const entry = (config.plugins ?? [])
|
|
157
|
+
.map((plugin) => (Array.isArray(plugin) ? plugin : [plugin]))
|
|
158
|
+
.find(([name]) => name === GOOGLE_MOBILE_ADS);
|
|
159
|
+
if (!entry) {
|
|
160
|
+
return { iosAppId: ids.iosAppId, androidAppId: ids.androidAppId, withGoogleMobileAds: false };
|
|
161
|
+
}
|
|
162
|
+
const theirs = entry[1] ?? {};
|
|
163
|
+
const pick = (name) => {
|
|
164
|
+
const ours = ids[name];
|
|
165
|
+
if (ours && theirs[name] && ours !== theirs[name]) {
|
|
166
|
+
throw new Error(
|
|
167
|
+
`${tag}: ${name} "${ours}" is not the "${theirs[name]}" given to ${GOOGLE_MOBILE_ADS}. Google's SDK reads one app ID per platform: give both plugins the same one, or leave it out of ${tag}'s options.`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return ours || theirs[name];
|
|
171
|
+
};
|
|
172
|
+
return { iosAppId: pick('iosAppId'), androidAppId: pick('androidAppId'), withGoogleMobileAds: true };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Adds (or updates) one of Google's meta-data items in the app's AndroidManifest.xml, marked
|
|
177
|
+
* `tools:replace="android:value"`: react-native-google-mobile-ads's library manifest declares the
|
|
178
|
+
* app ID item too, with the value from its own app.json key (often empty in Expo apps), and the app's
|
|
179
|
+
* value has to win the manifest merge.
|
|
180
|
+
*
|
|
181
|
+
* @param {import('expo/config-plugins').AndroidConfig.Manifest.AndroidManifest} manifest
|
|
182
|
+
* @param {string} name
|
|
183
|
+
* @param {string} value
|
|
184
|
+
*/
|
|
185
|
+
function addGoogleMetaData(manifest, name, value) {
|
|
186
|
+
const { Manifest } = AndroidConfig;
|
|
187
|
+
Manifest.ensureToolsAvailable(manifest);
|
|
188
|
+
const application = Manifest.getMainApplicationOrThrow(manifest);
|
|
189
|
+
Manifest.addMetaDataItemToMainApplication(application, name, value);
|
|
190
|
+
const item = application['meta-data'].find((each) => each.$['android:name'] === name);
|
|
191
|
+
item.$['tools:replace'] = 'android:value';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The Gradle property the AdMob and GAM packages' android/build.gradle read. */
|
|
195
|
+
const GOOGLE_ADS_VERSION_PROPERTY = 'rediadsGoogleMobileAdsVersion';
|
|
196
|
+
const GOOGLE_ADS_VERSION_PLUGINS = ['@rediads/react-native-ads-admob', '@rediads/react-native-ads-gam'];
|
|
197
|
+
/** The oldest Google Mobile Ads SDK (Android) the Rediads SDK works with. */
|
|
198
|
+
const GOOGLE_ADS_MINIMUM = [23, 0, 0];
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Sets (or, without the option, removes) the Gradle property with the Google Mobile Ads SDK version
|
|
202
|
+
* the AdMob and GAM packages add when nothing else in the app asks for play-services-ads. Their
|
|
203
|
+
* android/build.gradle adds it as a "prefer", so the app's own version (or
|
|
204
|
+
* react-native-google-mobile-ads's) always wins over it. "none" adds nothing. Both plugins may give it;
|
|
205
|
+
* two different values are an error.
|
|
206
|
+
*
|
|
207
|
+
* @param {import('expo/config').ExpoConfig} config
|
|
208
|
+
* @param {string} tag the calling plugin's name, for errors
|
|
209
|
+
* @param {string | undefined} version the calling plugin's androidGoogleMobileAdsVersion option
|
|
210
|
+
*/
|
|
211
|
+
function withGoogleMobileAdsVersion(config, tag, version) {
|
|
212
|
+
if (version !== undefined) {
|
|
213
|
+
const text = String(version);
|
|
214
|
+
const parts = text.split('.').map(Number);
|
|
215
|
+
const tooOld =
|
|
216
|
+
parts.length === 3 &&
|
|
217
|
+
parts.every(Number.isInteger) &&
|
|
218
|
+
(parts[0] - GOOGLE_ADS_MINIMUM[0] || parts[1] - GOOGLE_ADS_MINIMUM[1] || parts[2] - GOOGLE_ADS_MINIMUM[2]) < 0;
|
|
219
|
+
if (text !== 'none' && (!/^\d+\.\d+\.\d+$/.test(text) || tooOld)) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`${tag}: androidGoogleMobileAdsVersion "${text}" must be a Google Mobile Ads SDK version, 23.0.0 or newer (like "25.5.0"), or "none".`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
for (const plugin of config.plugins ?? []) {
|
|
225
|
+
if (!Array.isArray(plugin) || !GOOGLE_ADS_VERSION_PLUGINS.includes(plugin[0])) continue;
|
|
226
|
+
const theirs = plugin[1]?.androidGoogleMobileAdsVersion;
|
|
227
|
+
if (theirs !== undefined && String(theirs) !== text) {
|
|
228
|
+
throw new Error(
|
|
229
|
+
`${tag}: androidGoogleMobileAdsVersion "${text}" differs from the "${theirs}" given to ${plugin[0]}. Give both plugins the same one, or only one of them.`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return withGradleProperties(config, (config) => {
|
|
235
|
+
const others = config.modResults.filter(
|
|
236
|
+
(item) => !(item.type === 'property' && item.key === GOOGLE_ADS_VERSION_PROPERTY)
|
|
237
|
+
);
|
|
238
|
+
const given = (config.plugins ?? []).some(
|
|
239
|
+
(plugin) => Array.isArray(plugin) && GOOGLE_ADS_VERSION_PLUGINS.includes(plugin[0]) && plugin[1]?.androidGoogleMobileAdsVersion !== undefined
|
|
240
|
+
);
|
|
241
|
+
// Removed only when no Rediads plugin gives it: the other plugin may have set it already.
|
|
242
|
+
if (version === undefined) {
|
|
243
|
+
if (!given) config.modResults = others;
|
|
244
|
+
return config;
|
|
245
|
+
}
|
|
246
|
+
config.modResults = [...others, { type: 'property', key: GOOGLE_ADS_VERSION_PROPERTY, value: String(version) }];
|
|
247
|
+
return config;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {import('expo/config').ExpoConfig} config
|
|
253
|
+
* @param {{ iosSdkPath?: string, androidMavenRepo?: string } | undefined} options
|
|
254
|
+
*/
|
|
255
|
+
function withRediadsAds(config, options = {}) {
|
|
256
|
+
const { iosSdkPath, androidMavenRepo } = options ?? {};
|
|
257
|
+
|
|
258
|
+
config = withPodfile(config, (config) => {
|
|
259
|
+
const contents = config.modResults.contents;
|
|
260
|
+
if (iosSdkPath) {
|
|
261
|
+
// Local pods; the pod sources aren't needed (and Rediads' list may not have this version yet).
|
|
262
|
+
const folder = iosSdkFolder(config.modRequest.projectRoot, iosSdkPath);
|
|
263
|
+
config.modResults.contents = addPods(removeBlock(contents, '#', SOURCES_TAG), folder);
|
|
264
|
+
} else {
|
|
265
|
+
// Published pods; drop local pods an earlier prebuild added.
|
|
266
|
+
config.modResults.contents = addSources(removeBlock(contents, '#'));
|
|
267
|
+
}
|
|
268
|
+
return config;
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
if (androidMavenRepo) {
|
|
272
|
+
config = withProjectBuildGradle(config, (config) => {
|
|
273
|
+
if (config.modResults.language !== 'groovy') {
|
|
274
|
+
throw new Error(`${TAG}: android/build.gradle.kts is not supported yet; add the Maven repository by hand.`);
|
|
275
|
+
}
|
|
276
|
+
const url = mavenUrl(config.modRequest.projectRoot, androidMavenRepo);
|
|
277
|
+
config.modResults.contents = addMavenRepo(config.modResults.contents, url);
|
|
278
|
+
return config;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return config;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
module.exports = createRunOncePlugin(withRediadsAds, pkg.name, pkg.version);
|
|
286
|
+
module.exports.addPods = addPods;
|
|
287
|
+
module.exports.addSources = addSources;
|
|
288
|
+
module.exports.addMavenRepo = addMavenRepo;
|
|
289
|
+
module.exports.googleAppIds = googleAppIds;
|
|
290
|
+
module.exports.addGoogleMetaData = addGoogleMetaData;
|
|
291
|
+
module.exports.withGoogleMobileAdsVersion = withGoogleMobileAdsVersion;
|