Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit f2792b8

Browse files
Basic integration of Crashlytics for iOS... #549 (added Android as well)
1 parent c4519aa commit f2792b8

File tree

6 files changed

+108
-15
lines changed

6 files changed

+108
-15
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ src/**/*.d.ts
1515
!src/platforms/web/typings/firebase-webapi.d.ts
1616
!src/platforms/ios/typings/*.d.ts
1717
!src/platforms/android/typings/**/*.d.ts
18-
!src/scripts/*.js
18+
!src/scripts/entitlements*.js
1919
!demo/karma.conf.js
2020
demo/*.d.ts
2121
demo-ng/*.d.ts

demo/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"nativescript": {
33
"id": "org.nativescript.firebasedemo",
4-
"tns-android": {
5-
"version": "4.0.0-rc-2018.4.2.1"
6-
},
74
"tns-ios": {
85
"version": "4.0.1"
6+
},
7+
"tns-android": {
8+
"version": "4.0.1"
99
}
1010
},
1111
"dependencies": {

publish/scripts/installer.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ function promptQuestions() {
145145
default: 'n'
146146
}, {
147147
name: 'crashlytics',
148-
description: 'Are you using Firebase Crashlytics (currently iOS only; both crashlytics and crash_reporting can be enabled and crashlytics will be used for iOS and crash_reporting for android) (y/n)',
148+
description: 'Are you using Firebase Crashlytics (y/n)',
149149
default: 'n'
150150
}, {
151151
name: 'crash_reporting',
152-
description: 'Are you using Firebase Crash Reporting (y/n)',
152+
description: 'Are you using Firebase Crash Reporting (answer "n" if you want to use Crashlytics instead) (y/n)',
153153
default: 'n'
154154
}, {
155155
name: 'storage',
@@ -483,7 +483,10 @@ dependencies {
483483
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:$firebaseVersion"
484484
485485
// Uncomment if you want to use 'Crash Reporting'
486-
` + (isSelected(result.crash_reporting) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
486+
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
487+
488+
// Uncomment if you want to use 'Crashlytics'
489+
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.1"
487490
488491
// Uncomment if you want FCM (Firebase Cloud Messaging)
489492
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:$firebaseVersion"
@@ -505,6 +508,9 @@ dependencies {
505508
}
506509
507510
apply plugin: "com.google.gms.google-services"
511+
512+
// Uncomment if you want to use 'Crashlytics'
513+
` + (isSelected(result.crashlytics) ? `` : `//`) + `apply plugin: "io.fabric"
508514
`);
509515
console.log('Successfully created Android (include.gradle) file.');
510516
} catch(e) {
@@ -575,10 +581,24 @@ module.exports = function($logger, $projectData) {
575581
let projectBuildGradlePath = path.join($projectData.platformsDir, "android", "build.gradle");
576582
if (fs.existsSync(projectBuildGradlePath)) {
577583
let buildGradleContent = fs.readFileSync(projectBuildGradlePath).toString();
578-
584+
585+
if (buildGradleContent.indexOf("fabric.io") === -1) {
586+
let repositoriesNode = buildGradleContent.indexOf("repositories", 0);
587+
if (repositoriesNode > -1) {
588+
repositoriesNode = buildGradleContent.indexOf("}", repositoriesNode);
589+
buildGradleContent = buildGradleContent.substr(0, repositoriesNode - 1) + ' maven { url "https://maven.fabric.io/public" }\\n' + buildGradleContent.substr(repositoriesNode - 1);
590+
}
591+
592+
let dependenciesNode = buildGradleContent.indexOf("dependencies", 0);
593+
if (dependenciesNode > -1) {
594+
dependenciesNode = buildGradleContent.indexOf("}", dependenciesNode);
595+
buildGradleContent = buildGradleContent.substr(0, dependenciesNode - 1) + ' classpath "io.fabric.tools:gradle:1.25.1"\\n' + buildGradleContent.substr(dependenciesNode - 1);
596+
}
597+
}
598+
579599
let gradlePattern = /classpath ('|")com\\.android\\.tools\\.build:gradle:\\d+\\.\\d+\\.\\d+('|")/;
580600
let googleServicesPattern = /classpath ('|")com\\.google\\.gms:google-services:\\d+\\.\\d+\\.\\d+('|")/;
581-
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.1"';
601+
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.2"';
582602
if (googleServicesPattern.test(buildGradleContent)) {
583603
buildGradleContent = buildGradleContent.replace(googleServicesPattern, latestGoogleServicesPlugin);
584604
} else {
@@ -589,6 +609,29 @@ module.exports = function($logger, $projectData) {
589609
590610
fs.writeFileSync(projectBuildGradlePath, buildGradleContent);
591611
}
612+
613+
let projectAppBuildGradlePath = path.join($projectData.platformsDir, "android", "app", "build.gradle");
614+
if (fs.existsSync(projectAppBuildGradlePath)) {
615+
let appBuildGradleContent = fs.readFileSync(projectAppBuildGradlePath).toString();
616+
if (appBuildGradleContent.indexOf("buildMetadata.finalizedBy(copyMetadata)") === -1) {
617+
appBuildGradleContent = appBuildGradleContent.replace("ensureMetadataOutDir.finalizedBy(buildMetadata)", "ensureMetadataOutDir.finalizedBy(buildMetadata)\\n\\t\\tbuildMetadata.finalizedBy(copyMetadata)");
618+
appBuildGradleContent += \`
619+
task copyMetadata {
620+
doLast {
621+
copy {
622+
from "$projectDir/src/main/assets/metadata"
623+
def toDir = new File("$projectDir/build/intermediates/assets").listFiles()[0].name
624+
if (toDir != 'debug' && toDir != 'release') {
625+
toDir += "/release"
626+
}
627+
into "$projectDir/build/intermediates/assets/" + toDir + "/metadata"
628+
}
629+
}
630+
}\`;
631+
fs.writeFileSync(projectAppBuildGradlePath, appBuildGradleContent);
632+
}
633+
}
634+
592635
resolve();
593636
});
594637
};

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"package": "cd ../publish && rm -rf ./package && ./pack.sh",
4242
"demo.ios": "npm run preparedemo && cd ../demo && tns platform remove ios && tns run ios",
4343
"demo-ng.ios": "npm run preparedemo-ng && cd ../demo-ng && tns platform remove ios && tns run ios",
44-
"demo.android": "npm run preparedemo && cd ../demo && tns platform remove android && tns run android --justlaunch",
44+
"demo.android": "npm run preparedemo && cd ../demo && tns platform remove android && tns run android",
4545
"demo-ng.android": "npm run preparedemo-ng && cd ../demo-ng && tns run android",
4646
"test": "npm run tslint && npm run tslint.demo && cd ../demo && tns build ios && tns build android",
4747
"test.ios": "cd ../demo && tns test ios --emulator",

src/platforms/android/include.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ dependencies {
4646
// Uncomment if you want to use 'Crash Reporting'
4747
// compile "com.google.firebase:firebase-crash:$firebaseVersion"
4848

49+
// Uncomment if you want to enable 'Crashlytics' (1 / 2)
50+
// compile 'com.crashlytics.sdk.android:crashlytics:2.9.1'
51+
4952
// Uncomment if you want FCM (Firebase Cloud Messaging)
5053
// compile "com.google.firebase:firebase-messaging:$firebaseVersion"
5154

@@ -66,3 +69,6 @@ dependencies {
6669
}
6770

6871
apply plugin: "com.google.gms.google-services"
72+
73+
// Uncomment if you want to enable 'Crashlytics' (2 / 2)
74+
//apply plugin: "io.fabric"

src/scripts/postinstall.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,11 +2943,11 @@ function promptQuestions() {
29432943
default: 'n'
29442944
}, {
29452945
name: 'crashlytics',
2946-
description: 'Are you using Firebase Crashlytics (currently iOS only; both crashlytics and crash_reporting can be enabled and crashlytics will be used for iOS and crash_reporting for android) (y/n)',
2946+
description: 'Are you using Firebase Crashlytics (y/n)',
29472947
default: 'n'
29482948
}, {
29492949
name: 'crash_reporting',
2950-
description: 'Are you using Firebase Crash Reporting (y/n)',
2950+
description: 'Are you using Firebase Crash Reporting (answer "n" if you want to use Crashlytics instead) (y/n)',
29512951
default: 'n'
29522952
}, {
29532953
name: 'storage',
@@ -3281,7 +3281,10 @@ dependencies {
32813281
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:$firebaseVersion"
32823282
32833283
// Uncomment if you want to use 'Crash Reporting'
3284-
` + (isSelected(result.crash_reporting) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
3284+
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
3285+
3286+
// Uncomment if you want to use 'Crashlytics'
3287+
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.1"
32853288
32863289
// Uncomment if you want FCM (Firebase Cloud Messaging)
32873290
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:$firebaseVersion"
@@ -3303,6 +3306,9 @@ dependencies {
33033306
}
33043307
33053308
apply plugin: "com.google.gms.google-services"
3309+
3310+
// Uncomment if you want to use 'Crashlytics'
3311+
` + (isSelected(result.crashlytics) ? `` : `//`) + `apply plugin: "io.fabric"
33063312
`);
33073313
console.log('Successfully created Android (include.gradle) file.');
33083314
} catch(e) {
@@ -3363,6 +3369,7 @@ function writeGoogleServiceGradleHook(result) {
33633369
try {
33643370
var scriptContent =
33653371
`
3372+
33663373
var path = require("path");
33673374
var fs = require("fs");
33683375
@@ -3373,10 +3380,24 @@ module.exports = function($logger, $projectData) {
33733380
let projectBuildGradlePath = path.join($projectData.platformsDir, "android", "build.gradle");
33743381
if (fs.existsSync(projectBuildGradlePath)) {
33753382
let buildGradleContent = fs.readFileSync(projectBuildGradlePath).toString();
3376-
3383+
3384+
if (buildGradleContent.indexOf("fabric.io") === -1) {
3385+
let repositoriesNode = buildGradleContent.indexOf("repositories", 0);
3386+
if (repositoriesNode > -1) {
3387+
repositoriesNode = buildGradleContent.indexOf("}", repositoriesNode);
3388+
buildGradleContent = buildGradleContent.substr(0, repositoriesNode - 1) + ' maven { url "https://maven.fabric.io/public" }\\n' + buildGradleContent.substr(repositoriesNode - 1);
3389+
}
3390+
3391+
let dependenciesNode = buildGradleContent.indexOf("dependencies", 0);
3392+
if (dependenciesNode > -1) {
3393+
dependenciesNode = buildGradleContent.indexOf("}", dependenciesNode);
3394+
buildGradleContent = buildGradleContent.substr(0, dependenciesNode - 1) + ' classpath "io.fabric.tools:gradle:1.25.1"\\n' + buildGradleContent.substr(dependenciesNode - 1);
3395+
}
3396+
}
3397+
33773398
let gradlePattern = /classpath ('|")com\\.android\\.tools\\.build:gradle:\\d+\\.\\d+\\.\\d+('|")/;
33783399
let googleServicesPattern = /classpath ('|")com\\.google\\.gms:google-services:\\d+\\.\\d+\\.\\d+('|")/;
3379-
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.1"';
3400+
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.2"';
33803401
if (googleServicesPattern.test(buildGradleContent)) {
33813402
buildGradleContent = buildGradleContent.replace(googleServicesPattern, latestGoogleServicesPlugin);
33823403
} else {
@@ -3387,6 +3408,29 @@ module.exports = function($logger, $projectData) {
33873408
33883409
fs.writeFileSync(projectBuildGradlePath, buildGradleContent);
33893410
}
3411+
3412+
let projectAppBuildGradlePath = path.join($projectData.platformsDir, "android", "app", "build.gradle");
3413+
if (fs.existsSync(projectAppBuildGradlePath)) {
3414+
let appBuildGradleContent = fs.readFileSync(projectAppBuildGradlePath).toString();
3415+
if (appBuildGradleContent.indexOf("buildMetadata.finalizedBy(copyMetadata)") === -1) {
3416+
appBuildGradleContent = appBuildGradleContent.replace("ensureMetadataOutDir.finalizedBy(buildMetadata)", "ensureMetadataOutDir.finalizedBy(buildMetadata)\\n\\t\\tbuildMetadata.finalizedBy(copyMetadata)");
3417+
appBuildGradleContent += \`
3418+
task copyMetadata {
3419+
doLast {
3420+
copy {
3421+
from "$projectDir/src/main/assets/metadata"
3422+
def toDir = new File("$projectDir/build/intermediates/assets").listFiles()[0].name
3423+
if (toDir != 'debug' && toDir != 'release') {
3424+
toDir = toDir + "/release"
3425+
}
3426+
into "$projectDir/build/intermediates/assets/" + toDir + "/metadata"
3427+
}
3428+
}
3429+
}\`;
3430+
fs.writeFileSync(projectAppBuildGradlePath, appBuildGradleContent);
3431+
}
3432+
}
3433+
33903434
resolve();
33913435
});
33923436
};

0 commit comments

Comments
 (0)