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

Commit

Permalink
Basic integration of Crashlytics for iOS... #549 (added Android as well)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Apr 24, 2018
1 parent c4519aa commit f2792b8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ src/**/*.d.ts
!src/platforms/web/typings/firebase-webapi.d.ts
!src/platforms/ios/typings/*.d.ts
!src/platforms/android/typings/**/*.d.ts
!src/scripts/*.js
!src/scripts/entitlements*.js
!demo/karma.conf.js
demo/*.d.ts
demo-ng/*.d.ts
Expand Down
6 changes: 3 additions & 3 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"nativescript": {
"id": "org.nativescript.firebasedemo",
"tns-android": {
"version": "4.0.0-rc-2018.4.2.1"
},
"tns-ios": {
"version": "4.0.1"
},
"tns-android": {
"version": "4.0.1"
}
},
"dependencies": {
Expand Down
53 changes: 48 additions & 5 deletions publish/scripts/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ function promptQuestions() {
default: 'n'
}, {
name: 'crashlytics',
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)',
description: 'Are you using Firebase Crashlytics (y/n)',
default: 'n'
}, {
name: 'crash_reporting',
description: 'Are you using Firebase Crash Reporting (y/n)',
description: 'Are you using Firebase Crash Reporting (answer "n" if you want to use Crashlytics instead) (y/n)',
default: 'n'
}, {
name: 'storage',
Expand Down Expand Up @@ -483,7 +483,10 @@ dependencies {
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:$firebaseVersion"
// Uncomment if you want to use 'Crash Reporting'
` + (isSelected(result.crash_reporting) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
// Uncomment if you want to use 'Crashlytics'
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.1"
// Uncomment if you want FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:$firebaseVersion"
Expand All @@ -505,6 +508,9 @@ dependencies {
}
apply plugin: "com.google.gms.google-services"
// Uncomment if you want to use 'Crashlytics'
` + (isSelected(result.crashlytics) ? `` : `//`) + `apply plugin: "io.fabric"
`);
console.log('Successfully created Android (include.gradle) file.');
} catch(e) {
Expand Down Expand Up @@ -575,10 +581,24 @@ module.exports = function($logger, $projectData) {
let projectBuildGradlePath = path.join($projectData.platformsDir, "android", "build.gradle");
if (fs.existsSync(projectBuildGradlePath)) {
let buildGradleContent = fs.readFileSync(projectBuildGradlePath).toString();
if (buildGradleContent.indexOf("fabric.io") === -1) {
let repositoriesNode = buildGradleContent.indexOf("repositories", 0);
if (repositoriesNode > -1) {
repositoriesNode = buildGradleContent.indexOf("}", repositoriesNode);
buildGradleContent = buildGradleContent.substr(0, repositoriesNode - 1) + ' maven { url "https://maven.fabric.io/public" }\\n' + buildGradleContent.substr(repositoriesNode - 1);
}
let dependenciesNode = buildGradleContent.indexOf("dependencies", 0);
if (dependenciesNode > -1) {
dependenciesNode = buildGradleContent.indexOf("}", dependenciesNode);
buildGradleContent = buildGradleContent.substr(0, dependenciesNode - 1) + ' classpath "io.fabric.tools:gradle:1.25.1"\\n' + buildGradleContent.substr(dependenciesNode - 1);
}
}
let gradlePattern = /classpath ('|")com\\.android\\.tools\\.build:gradle:\\d+\\.\\d+\\.\\d+('|")/;
let googleServicesPattern = /classpath ('|")com\\.google\\.gms:google-services:\\d+\\.\\d+\\.\\d+('|")/;
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.1"';
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.2"';
if (googleServicesPattern.test(buildGradleContent)) {
buildGradleContent = buildGradleContent.replace(googleServicesPattern, latestGoogleServicesPlugin);
} else {
Expand All @@ -589,6 +609,29 @@ module.exports = function($logger, $projectData) {
fs.writeFileSync(projectBuildGradlePath, buildGradleContent);
}
let projectAppBuildGradlePath = path.join($projectData.platformsDir, "android", "app", "build.gradle");
if (fs.existsSync(projectAppBuildGradlePath)) {
let appBuildGradleContent = fs.readFileSync(projectAppBuildGradlePath).toString();
if (appBuildGradleContent.indexOf("buildMetadata.finalizedBy(copyMetadata)") === -1) {
appBuildGradleContent = appBuildGradleContent.replace("ensureMetadataOutDir.finalizedBy(buildMetadata)", "ensureMetadataOutDir.finalizedBy(buildMetadata)\\n\\t\\tbuildMetadata.finalizedBy(copyMetadata)");
appBuildGradleContent += \`
task copyMetadata {
doLast {
copy {
from "$projectDir/src/main/assets/metadata"
def toDir = new File("$projectDir/build/intermediates/assets").listFiles()[0].name
if (toDir != 'debug' && toDir != 'release') {
toDir += "/release"
}
into "$projectDir/build/intermediates/assets/" + toDir + "/metadata"
}
}
}\`;
fs.writeFileSync(projectAppBuildGradlePath, appBuildGradleContent);
}
}
resolve();
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"package": "cd ../publish && rm -rf ./package && ./pack.sh",
"demo.ios": "npm run preparedemo && cd ../demo && tns platform remove ios && tns run ios",
"demo-ng.ios": "npm run preparedemo-ng && cd ../demo-ng && tns platform remove ios && tns run ios",
"demo.android": "npm run preparedemo && cd ../demo && tns platform remove android && tns run android --justlaunch",
"demo.android": "npm run preparedemo && cd ../demo && tns platform remove android && tns run android",
"demo-ng.android": "npm run preparedemo-ng && cd ../demo-ng && tns run android",
"test": "npm run tslint && npm run tslint.demo && cd ../demo && tns build ios && tns build android",
"test.ios": "cd ../demo && tns test ios --emulator",
Expand Down
6 changes: 6 additions & 0 deletions src/platforms/android/include.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
// Uncomment if you want to use 'Crash Reporting'
// compile "com.google.firebase:firebase-crash:$firebaseVersion"

// Uncomment if you want to enable 'Crashlytics' (1 / 2)
// compile 'com.crashlytics.sdk.android:crashlytics:2.9.1'

// Uncomment if you want FCM (Firebase Cloud Messaging)
// compile "com.google.firebase:firebase-messaging:$firebaseVersion"

Expand All @@ -66,3 +69,6 @@ dependencies {
}

apply plugin: "com.google.gms.google-services"

// Uncomment if you want to enable 'Crashlytics' (2 / 2)
//apply plugin: "io.fabric"
54 changes: 49 additions & 5 deletions src/scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2943,11 +2943,11 @@ function promptQuestions() {
default: 'n'
}, {
name: 'crashlytics',
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)',
description: 'Are you using Firebase Crashlytics (y/n)',
default: 'n'
}, {
name: 'crash_reporting',
description: 'Are you using Firebase Crash Reporting (y/n)',
description: 'Are you using Firebase Crash Reporting (answer "n" if you want to use Crashlytics instead) (y/n)',
default: 'n'
}, {
name: 'storage',
Expand Down Expand Up @@ -3281,7 +3281,10 @@ dependencies {
` + (isSelected(result.remote_config) ? `` : `//`) + ` compile "com.google.firebase:firebase-config:$firebaseVersion"
// Uncomment if you want to use 'Crash Reporting'
` + (isSelected(result.crash_reporting) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
` + (isSelected(result.crash_reporting) && !isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.google.firebase:firebase-crash:$firebaseVersion"
// Uncomment if you want to use 'Crashlytics'
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.1"
// Uncomment if you want FCM (Firebase Cloud Messaging)
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:$firebaseVersion"
Expand All @@ -3303,6 +3306,9 @@ dependencies {
}
apply plugin: "com.google.gms.google-services"
// Uncomment if you want to use 'Crashlytics'
` + (isSelected(result.crashlytics) ? `` : `//`) + `apply plugin: "io.fabric"
`);
console.log('Successfully created Android (include.gradle) file.');
} catch(e) {
Expand Down Expand Up @@ -3363,6 +3369,7 @@ function writeGoogleServiceGradleHook(result) {
try {
var scriptContent =
`
var path = require("path");
var fs = require("fs");
Expand All @@ -3373,10 +3380,24 @@ module.exports = function($logger, $projectData) {
let projectBuildGradlePath = path.join($projectData.platformsDir, "android", "build.gradle");
if (fs.existsSync(projectBuildGradlePath)) {
let buildGradleContent = fs.readFileSync(projectBuildGradlePath).toString();
if (buildGradleContent.indexOf("fabric.io") === -1) {
let repositoriesNode = buildGradleContent.indexOf("repositories", 0);
if (repositoriesNode > -1) {
repositoriesNode = buildGradleContent.indexOf("}", repositoriesNode);
buildGradleContent = buildGradleContent.substr(0, repositoriesNode - 1) + ' maven { url "https://maven.fabric.io/public" }\\n' + buildGradleContent.substr(repositoriesNode - 1);
}
let dependenciesNode = buildGradleContent.indexOf("dependencies", 0);
if (dependenciesNode > -1) {
dependenciesNode = buildGradleContent.indexOf("}", dependenciesNode);
buildGradleContent = buildGradleContent.substr(0, dependenciesNode - 1) + ' classpath "io.fabric.tools:gradle:1.25.1"\\n' + buildGradleContent.substr(dependenciesNode - 1);
}
}
let gradlePattern = /classpath ('|")com\\.android\\.tools\\.build:gradle:\\d+\\.\\d+\\.\\d+('|")/;
let googleServicesPattern = /classpath ('|")com\\.google\\.gms:google-services:\\d+\\.\\d+\\.\\d+('|")/;
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.1"';
let latestGoogleServicesPlugin = 'classpath "com.google.gms:google-services:3.1.2"';
if (googleServicesPattern.test(buildGradleContent)) {
buildGradleContent = buildGradleContent.replace(googleServicesPattern, latestGoogleServicesPlugin);
} else {
Expand All @@ -3387,6 +3408,29 @@ module.exports = function($logger, $projectData) {
fs.writeFileSync(projectBuildGradlePath, buildGradleContent);
}
let projectAppBuildGradlePath = path.join($projectData.platformsDir, "android", "app", "build.gradle");
if (fs.existsSync(projectAppBuildGradlePath)) {
let appBuildGradleContent = fs.readFileSync(projectAppBuildGradlePath).toString();
if (appBuildGradleContent.indexOf("buildMetadata.finalizedBy(copyMetadata)") === -1) {
appBuildGradleContent = appBuildGradleContent.replace("ensureMetadataOutDir.finalizedBy(buildMetadata)", "ensureMetadataOutDir.finalizedBy(buildMetadata)\\n\\t\\tbuildMetadata.finalizedBy(copyMetadata)");
appBuildGradleContent += \`
task copyMetadata {
doLast {
copy {
from "$projectDir/src/main/assets/metadata"
def toDir = new File("$projectDir/build/intermediates/assets").listFiles()[0].name
if (toDir != 'debug' && toDir != 'release') {
toDir = toDir + "/release"
}
into "$projectDir/build/intermediates/assets/" + toDir + "/metadata"
}
}
}\`;
fs.writeFileSync(projectAppBuildGradlePath, appBuildGradleContent);
}
}
resolve();
});
};
Expand Down

0 comments on commit f2792b8

Please sign in to comment.