Skip to content

Commit 9887f1a

Browse files
committed
📝 register instabug app class in manifest only if there is no other class registered and remove it from manifest on uninstalling instabug
1 parent 7852265 commit 9887f1a

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

‎plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<source-file src="src/android/IBGPlugin.java" target-dir="src/com/instabug/cordova/plugin"/>
8484
<source-file src="src/android/MyApplication.java" target-dir="src/com/instabug/cordova/plugin"/>
8585
<hook type="before_plugin_install" src="scripts/android/before_plugin_install.js"/>
86+
<hook type="before_plugin_uninstall" src="scripts/android/before_plugin_uninstall.js"/>
8687
</platform>
8788

8889
<!-- ios -->

‎scripts/android/before_plugin_install.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ module.exports = function(ctx) {
5454
);
5555
writeIbgBuildGradle(buildGradle);
5656
}
57-
return editManist(ctx);
57+
return editManist(ctx, true);
5858
}
5959
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const editManifest = require('./edit_manifest');
2+
module.exports = (ctx) => {
3+
return editManifest(ctx, false);
4+
}

‎scripts/android/edit_manifest.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
1-
module.exports = function(ctx) {
2-
console.log('INNN');
3-
const fs = ctx.requireCordovaModule('fs');
4-
const path = ctx.requireCordovaModule('path');
5-
const Q = ctx.requireCordovaModule('q');
6-
const xml = ctx.requireCordovaModule('cordova-common').xmlHelpers;
1+
module.exports = (ctx, isInstalling) => {
2+
const fs = ctx.requireCordovaModule('fs');
3+
const path = ctx.requireCordovaModule('path');
4+
const Q = ctx.requireCordovaModule('q');
5+
const xml = ctx.requireCordovaModule('cordova-common').xmlHelpers;
76

8-
const deferred = Q.defer();
9-
const platformPath = path.join(ctx.opts.projectRoot, './platforms/android');
10-
const manifestPaths = [
11-
path.join(platformPath, './AndroidManifest.xml'),
12-
path.join(platformPath, './app/src/main/AndroidManifest.xml'),
13-
];
7+
const deferred = Q.defer();
8+
const platformPath = path.join(ctx.opts.projectRoot, './platforms/android');
9+
const manifestPaths = [
10+
path.join(platformPath, './AndroidManifest.xml'),
11+
path.join(platformPath, './app/src/main/AndroidManifest.xml')
12+
];
1413

15-
const manifestPath = manifestPaths.find((filepath) => {
16-
try {
17-
fs.accessSync(filepath, fs.constants.F_OK)
18-
return true
19-
} catch (err) {
20-
return false
21-
}
22-
});
14+
const manifestPath = manifestPaths.find(filepath => {
15+
try {
16+
fs.accessSync(filepath, fs.constants.F_OK);
17+
return true;
18+
} catch (err) {
19+
return false;
20+
}
21+
});
2322

24-
var doc;
23+
if (manifestPath != null) {
24+
const appName = 'com.instabug.cordova.plugin.MyApplication';
25+
let doc = xml.parseElementtreeSync(manifestPath);
26+
let appAttr = doc.getroot().find('./application').attrib['android:name'];
2527

26-
if (manifestPath != null) {
27-
doc = xml.parseElementtreeSync(manifestPath)
28-
doc.getroot().find('./application').attrib['android:name'] =
29-
'com.instabug.cordova.plugin.MyApplication'
30-
fs.writeFileSync(manifestPath, doc.write({ indent: 4 }))
31-
deferred.resolve()
28+
if (isInstalling) {
29+
if (!appAttr) {
30+
doc.getroot().find('./application').attrib['android:name'] = appName;
31+
}
3232
} else {
33-
deferred.reject(new Error("Can't find AndroidManifest.xml"))
33+
if (appAttr === appName) {
34+
delete doc.getroot().find('./application').attrib['android:name'];
35+
}
3436
}
37+
38+
fs.writeFileSync(manifestPath, doc.write({ indent: 4 }));
39+
deferred.resolve();
40+
} else {
41+
deferred.reject(new Error("Can't find AndroidManifest.xml"));
42+
}
3543

36-
return deferred.promise
37-
}
44+
return deferred.promise;
45+
};

0 commit comments

Comments
 (0)