diff --git a/app/.gitignore b/app/.gitignore index 42afabfd2..2abde4aab 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -1 +1,2 @@ -/build \ No newline at end of file +/build +/google-services.json diff --git a/app/build.gradle b/app/build.gradle index 45b8abb92..645059efb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 @@ -57,8 +72,10 @@ android { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - firebaseCrashlytics { - mappingFileUploadEnabled false + if (firebaseEnabled) { + firebaseCrashlytics { + mappingFileUploadEnabled false + } } } } diff --git a/app/src/main/java/org/openedx/app/AnalyticsManager.kt b/app/src/main/java/org/openedx/app/AnalyticsManager.kt index d2604ca1d..e474416dd 100644 --- a/app/src/main/java/org/openedx/app/AnalyticsManager.kt +++ b/app/src/main/java/org/openedx/app/AnalyticsManager.kt @@ -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() diff --git a/app/src/main/java/org/openedx/app/OpenEdXApp.kt b/app/src/main/java/org/openedx/app/OpenEdXApp.kt index 9f1f95977..865290940 100644 --- a/app/src/main/java/org/openedx/app/OpenEdXApp.kt +++ b/app/src/main/java/org/openedx/app/OpenEdXApp.kt @@ -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) } } diff --git a/buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy b/buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy index ed78319bc..10ca3e8db 100644 --- a/buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy +++ b/buildSrc/src/main/groovy/org/edx/builder/ConfigHelper.groovy @@ -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() + } + } } diff --git a/core/src/main/java/org/openedx/core/config/FirebaseConfig.kt b/core/src/main/java/org/openedx/core/config/FirebaseConfig.kt index e659ef2ce..b8257d002 100644 --- a/core/src/main/java/org/openedx/core/config/FirebaseConfig.kt +++ b/core/src/main/java/org/openedx/core/config/FirebaseConfig.kt @@ -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 = "", @@ -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 diff --git a/default_config/dev/config.yaml b/default_config/dev/config.yaml index 7de39893a..09c746827 100644 --- a/default_config/dev/config.yaml +++ b/default_config/dev/config.yaml @@ -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 diff --git a/default_config/prod/config.yaml b/default_config/prod/config.yaml index ef70aeae7..3cc0b6592 100644 --- a/default_config/prod/config.yaml +++ b/default_config/prod/config.yaml @@ -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 diff --git a/default_config/stage/config.yaml b/default_config/stage/config.yaml index ef70aeae7..3cc0b6592 100644 --- a/default_config/stage/config.yaml +++ b/default_config/stage/config.yaml @@ -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