From f2792b814f74d244929eab13abfd05ea8179ea3f Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Tue, 24 Apr 2018 22:44:25 +0200 Subject: [PATCH] Basic integration of Crashlytics for iOS... #549 (added Android as well) --- .gitignore | 2 +- demo/package.json | 6 ++-- publish/scripts/installer.js | 53 ++++++++++++++++++++++++--- src/package.json | 2 +- src/platforms/android/include.gradle | 6 ++++ src/scripts/postinstall.js | 54 +++++++++++++++++++++++++--- 6 files changed, 108 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 6c7c4412..e5bf0e71 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/demo/package.json b/demo/package.json index 6fe53ff7..6b6a6e8f 100644 --- a/demo/package.json +++ b/demo/package.json @@ -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": { diff --git a/publish/scripts/installer.js b/publish/scripts/installer.js index 764fdb14..17f8edf0 100755 --- a/publish/scripts/installer.js +++ b/publish/scripts/installer.js @@ -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', @@ -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" @@ -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) { @@ -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 { @@ -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(); }); }; diff --git a/src/package.json b/src/package.json index 5e1dd278..d8f68748 100644 --- a/src/package.json +++ b/src/package.json @@ -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", diff --git a/src/platforms/android/include.gradle b/src/platforms/android/include.gradle index a63bba72..8a2016fa 100644 --- a/src/platforms/android/include.gradle +++ b/src/platforms/android/include.gradle @@ -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" @@ -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" diff --git a/src/scripts/postinstall.js b/src/scripts/postinstall.js index 9e3523bb..3fd96073 100644 --- a/src/scripts/postinstall.js +++ b/src/scripts/postinstall.js @@ -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', @@ -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" @@ -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) { @@ -3363,6 +3369,7 @@ function writeGoogleServiceGradleHook(result) { try { var scriptContent = ` + var path = require("path"); var fs = require("fs"); @@ -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 { @@ -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(); }); };