Skip to content

Commit

Permalink
Merge pull request openedx#233 from raccoongang/feat/google_config
Browse files Browse the repository at this point in the history
feat: Firebase config
  • Loading branch information
volodymyr-chekyrta authored Feb 26, 2024
2 parents 92d697f + b09a621 commit b543768
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
/google-services.json
37 changes: 27 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-parcelize'
id 'kotlin-kapt'
id 'com.google.firebase.crashlytics'
}

def config = configHelper.fetchConfig()
def appId = config.getOrDefault("APPLICATION_ID", "org.openedx.app")
def platformName = config.getOrDefault("PLATFORM_NAME", "OpenEdx").toLowerCase()
def firebaseConfig = config.get('FIREBASE')
def firebaseEnabled = firebaseConfig?.getOrDefault('ENABLED', false)

apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
if (firebaseEnabled) {
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

tasks.register('generateGoogleServicesJson') {
configHelper.generateGoogleServicesJson(appId)
}

preBuild.dependsOn(generateGoogleServicesJson)
} else {
tasks.register('removeGoogleServicesJson') {
configHelper.removeGoogleServicesJson()
}

preBuild.dependsOn(removeGoogleServicesJson)
}

android {
compileSdk 34
Expand Down Expand Up @@ -57,8 +72,10 @@ android {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

firebaseCrashlytics {
mappingFileUploadEnabled false
if (firebaseEnabled) {
firebaseCrashlytics {
mappingFileUploadEnabled false
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/openedx/app/AnalyticsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnalyticsManager(

init {
// Initialise all the analytics libraries here
if (config.getFirebaseConfig().projectId.isNotBlank()) {
if (config.getFirebaseConfig().enabled) {
addAnalyticsTracker(FirebaseAnalytics(context = context))
}
val segmentConfig = config.getSegmentConfig()
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/org/openedx/app/OpenEdXApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,8 @@ class OpenEdXApp : Application() {
screenModule
)
}
val firebaseConfig = config.getFirebaseConfig()
if (firebaseConfig.enabled) {
val options = FirebaseOptions.Builder()
.setProjectId(firebaseConfig.projectId)
.setApplicationId(firebaseConfig.applicationId)
.setApiKey(firebaseConfig.apiKey)
.setGcmSenderId(firebaseConfig.gcmSenderId)
.build()
Firebase.initialize(this, options)
if (config.getFirebaseConfig().enabled) {
Firebase.initialize(this)
}
}

Expand Down
53 changes: 53 additions & 0 deletions buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,57 @@ class ConfigHelper {
it.write(new JsonBuilder(configJson).toPrettyString())
}
}

def generateGoogleServicesJson(applicationId) {
def config = fetchConfig()
def firebase = config.get("FIREBASE")
if (!firebase) {
return
}
if (!firebase.getOrDefault("ENABLED", false)) {
return
}

def googleServicesJsonPath = projectDir.path + "/app/"
new File(googleServicesJsonPath).mkdirs()

def projectInfo = [
project_number: firebase.getOrDefault("PROJECT_NUMBER", ""),
project_id : firebase.getOrDefault("PROJECT_ID", ""),
storage_bucket: "${firebase.getOrDefault("PROJECT_ID", "")}.appspot.com"
]
def clientInfo = [
mobilesdk_app_id : firebase.getOrDefault("APPLICATION_ID", ""),
android_client_info: [
package_name: applicationId
]
]
def client = [
client_info : clientInfo,
oauth_client: [],
api_key : [[current_key: firebase.getOrDefault("API_KEY", "")]],
services : [
appinvite_service: [
other_platform_oauth_client: []
]
]
]
def configJson = [
project_info : projectInfo,
client : [client],
configuration_version: "1"
]

new FileWriter(googleServicesJsonPath + "/google-services.json").withWriter {
it.write(new JsonBuilder(configJson).toPrettyString())
}
}

def removeGoogleServicesJson() {
def googleServicesJsonPath = projectDir.path + "/app/google-services.json"
def file = new File(googleServicesJsonPath)
if (file.exists()) {
file.delete()
}
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/org/openedx/core/config/FirebaseConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ data class FirebaseConfig(
@SerializedName("ANALYTICS_SOURCE")
val analyticsSource: AnalyticsSource = AnalyticsSource.NONE,

@SerializedName("PROJECT_NUMBER")
val projectNumber: String = "",

@SerializedName("PROJECT_ID")
val projectId: String = "",

Expand All @@ -17,9 +20,6 @@ data class FirebaseConfig(

@SerializedName("API_KEY")
val apiKey: String = "",

@SerializedName("GCM_SENDER_ID")
val gcmSenderId: String = "",
) {
fun isSegmentAnalyticsSource(): Boolean {
return enabled && analyticsSource == AnalyticsSource.SEGMENT
Expand Down
8 changes: 4 additions & 4 deletions default_config/dev/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ FIREBASE:
ENABLED: false
ANALYTICS_SOURCE: '' # segment | none
CLOUD_MESSAGING_ENABLED: false
PROJECT_ID: ""
APPLICATION_ID: ""
API_KEY: ""
GCM_SENDER_ID: ""
PROJECT_NUMBER: ''
PROJECT_ID: ''
APPLICATION_ID: '' #App ID field from the Firebase console or mobilesdk_app_id from the google-services.json file.
API_KEY: ''

SEGMENT_IO:
ENABLED: false
Expand Down
8 changes: 4 additions & 4 deletions default_config/prod/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ FIREBASE:
ENABLED: false
ANALYTICS_SOURCE: '' # segment | none
CLOUD_MESSAGING_ENABLED: false
PROJECT_ID: ""
APPLICATION_ID: ""
API_KEY: ""
GCM_SENDER_ID: ""
PROJECT_NUMBER: ''
PROJECT_ID: ''
APPLICATION_ID: '' #App ID field from the Firebase console or mobilesdk_app_id from the google-services.json file.
API_KEY: ''

SEGMENT_IO:
ENABLED: false
Expand Down
8 changes: 4 additions & 4 deletions default_config/stage/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ FIREBASE:
ENABLED: false
ANALYTICS_SOURCE: '' # segment | none
CLOUD_MESSAGING_ENABLED: false
PROJECT_ID: ""
APPLICATION_ID: ""
API_KEY: ""
GCM_SENDER_ID: ""
PROJECT_NUMBER: ''
PROJECT_ID: ''
APPLICATION_ID: '' #App ID field from the Firebase console or mobilesdk_app_id from the google-services.json file.
API_KEY: ''

SEGMENT_IO:
ENABLED: false
Expand Down

0 comments on commit b543768

Please sign in to comment.