Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Move firebase keys from json to config file
Browse files Browse the repository at this point in the history
- LEARNER-7585.
- Move google-services.json content to configs.
- Generate google-services.json file using the
  configs at build time at the time of build
  before the keys validation task firebase.
  • Loading branch information
farhan-arshad-dev committed Mar 6, 2020
1 parent d34febf commit 4e5c31d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 22 deletions.
40 changes: 18 additions & 22 deletions OpenEdXMobile/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import groovy.json.JsonBuilder
import org.edx.builder.TaskHelper

buildscript {
Expand Down Expand Up @@ -34,18 +33,7 @@ edx {
apply plugin: 'newrelic'
apply plugin: 'com.facebook.testing.screenshot'
apply from: 'jacoco.gradle'

class AndroidHelper {
static def saveProcessedConfig(project, config) {
def path = project.file('assets/config')
path.mkdirs()
def jsonWriter = new FileWriter(path.absolutePath + '/config.json')
def builder = new JsonBuilder(config)
jsonWriter.withWriter {
builder.writeTo(it)
}
}
}
apply from: file('gradle_scripts/AndroidHelper.gradle')

/**
* Computes a semantic version string, e.g. "1.0" or "2.5.7" by parsing git branches or tags.
Expand Down Expand Up @@ -261,15 +249,15 @@ configurations {
}

def config = new TaskHelper().loadConfig(project)
def firebase = config.get('FIREBASE')
def firebaseEnabled = firebase?.get('ENABLED')
if (firebaseEnabled?: false) {
def firebaseConfig = config.get('FIREBASE')
def firebaseEnabled = firebaseConfig?.get('ENABLED')
if (firebaseEnabled ?: false) {
apply plugin: 'com.google.gms.google-services'
// Apply the Performance Monitoring plugin to enable instrumentation
apply plugin: 'com.google.firebase.firebase-perf'
}
// Variable to check if Firebase Cloud Messaging is enabled
def fcmEnabled = config.get('PUSH_NOTIFICATIONS') && firebaseEnabled && firebase?.get('CLOUD_MESSAGING_ENABLED')
def fcmEnabled = config.get('PUSH_NOTIFICATIONS') && firebaseEnabled && firebaseConfig?.get('CLOUD_MESSAGING_ENABLED')

android {
signingConfigs {
Expand All @@ -295,7 +283,7 @@ android {
flavorDimensions "default"

defaultConfig {
applicationId "org.edx.mobile"
applicationId APPLICATION_ID
// minimum version is Android 4.1
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
Expand Down Expand Up @@ -495,18 +483,26 @@ android {
}
}


android.applicationVariants.all { variant ->
def variantName = variant.name.capitalize()
def taskName = "applyConfig" + variantName
def configureTask = project.task(taskName) << {
def helper = new AndroidHelper()
helper.saveProcessedConfig(project, config)
AndroidHelper.saveProcessedConfig(project, config)
}
def generateTask = project.tasks.getByName("generate" + variantName + "Resources")
generateTask.dependsOn(configureTask)
// Task that generate google-services.json from the configs and execute before the
// google-services.json validation
if (firebaseEnabled) {
def serviceJsonTask = "generate$variantName" + "GoogleServicesJson"
project.task(serviceJsonTask).doFirst {
AndroidHelper.saveGoogleServicesJson(project, firebaseConfig)
}
def googleServicesJsonValidationTask = "process$variantName" + "GoogleServices"
tasks.getByName(googleServicesJsonValidationTask).dependsOn(serviceJsonTask)
}

tasks.all {task ->
tasks.all { task ->
if (task.name.startsWith("test")) {
task.mustRunAfter(configureTask)
}
Expand Down
104 changes: 104 additions & 0 deletions OpenEdXMobile/gradle_scripts/AndroidHelper.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import groovy.json.JsonBuilder

class AndroidHelper {

static def saveProcessedConfig(project, config) {
def path = project.file("assets/config")
path.mkdirs()
def jsonWriter = new FileWriter(path.absolutePath + "/config.json")
def builder = new JsonBuilder(config)
jsonWriter.withWriter {
builder.writeTo(it)
}
}

static def saveGoogleServicesJson(project, firebaseConfig) {
def googleServiceJsonPath = project.projectDir.toString() + "/google-services.json"
new FileWriter(googleServiceJsonPath).withWriter {
getGoogleServicesContent(project, firebaseConfig).writeTo(it)
}
}

static def getGoogleServicesContent(project, firebaseConfig) {
if (checkRequiredFields(firebaseConfig)) {
def jsonBuilder = new JsonBuilder()
def projectNumber = firebaseConfig.get("PROJECT_NUMBER")
def projectId = firebaseConfig.get("PROJECT_ID")
def oauthClientId = firebaseConfig.get("OAUTH_CLIENT_ID")
def currentKey = firebaseConfig.get("CURRENT_KEY")
def mobileSdkAppId = firebaseConfig.get("MOBILE_SDK_APP_ID")
// construct the google-services.json fields
jsonBuilder {
project_info {
project_number projectNumber
firebase_url "https://$projectId" + ".firebaseio.com"
project_id projectId
storage_bucket projectId + ".appspot.com"
}
client([{
client_info {
"mobilesdk_app_id" mobileSdkAppId
"android_client_info" {
"package_name" project.APPLICATION_ID
}
}
oauth_client([
{
"client_id" oauthClientId
"client_type" 3
}
])
api_key(
[{
"current_key" currentKey
}])
services {
appinvite_service {
other_platform_oauth_client([{
"client_id" oauthClientId
"client_type" 3
}
])
}
}
}])
configuration_version "1"
}
return jsonBuilder
}
}

static def checkRequiredFields(firebaseConfig) {
def available = true
def message = ""
if (!firebaseConfig.get("PROJECT_NUMBER")) {
message += 'FIREBASE:PROJECT_NUMBER is missing or empty\n'
available = false
}

if (!firebaseConfig.get("PROJECT_ID")) {
message += 'FIREBASE:PROJECT_ID is missing or empty\n'
available = false
}

if (!firebaseConfig.get("OAUTH_CLIENT_ID")) {
message += 'FIREBASE:OAUTH_CLIENT_ID is missing or empty\n'
available = false
}

if (!firebaseConfig.get("CURRENT_KEY")) {
message += 'FIREBASE:CURRENT_KEY is missing or empty\n'
available = false
}
if (!firebaseConfig.get("MOBILE_SDK_APP_ID")) {
message += 'FIREBASE:MOBILE_SDK_APP_ID is missing or empty\n'
available = false
}
if (!available) {
throw new GradleException(message)
}
return available
}
}

ext.AndroidHelper = AndroidHelper
1 change: 1 addition & 0 deletions constants.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
project.ext {
APPLICATION_ID = "org.edx.mobile"
KOTLIN_VERSION = "1.3.41"
GRADLE_PLUGIN_VERSION = "3.3.2"
BUILD_TOOLS_VERSION = "28.0.3"
Expand Down

0 comments on commit 4e5c31d

Please sign in to comment.