2017-10-09 142 views
0

我認爲這是對此主題提出的其他問題的變體。多個google-services.json每個產品口味

我有兩種產品口味。我在不同的環境中部署我的應用,每個環境都與不同的Firebase項目交談。因此,對於每種口味,我需要能夠針對特定的環境(開發,測試,生產等)

有沒有一種方法,我可以使構建變體的風味,選擇適當的google-services.json文件沒有引入新的產品口味?也許我在錯誤的方式處理這個問題......

回答

1

我能夠做到這一點的唯一辦法是繞過google-services.json使用和動態例如創建FirebaseApp實例

if (<is dev>) { 
     apiKey = <dev api key>; 
     databaseUrl = <dev database url>; 
    } else if (<is test> { 
     apiKey = <>; 
     databaseUrl = <>; 
    } else // production { 
     apiKey = <>; 
     databaseUrl = <>; 
    } 


    FirebaseOptions firebaseOptions = new FirebaseOptions.Builder() 
      .setApiKey(apiKey) 
      .setApplicationId(context.getString(R.string.google_app_id)) 
      .setDatabaseUrl(databaseUrl) 
      .build(); 

    return FirebaseApp.initializeApp(context, firebaseOptions, "MyApp"); 
+0

太好了。我會試試這個! – user3352488

+0

請注意,您需要從您的gradle文件中刪除'apply plugin:'com.google.gms.google-services''使用這種方法..這就是您的流程'google-services.json' –

+0

@ user3352488是你能夠得到那個工作? –

0

首先,將各google_services.json每個buildType在以下位置:

app/src/debug/google_services.json 
app/src/main/google_services.json 

現在,讓我們掀起了一些gradle這個任務你:應用程序的的build.gradle自動移動相應的google_services.json到應用程序/ google_services.json

task switchToDebug(type: Copy) { 
    description = 'Switches to DEBUG google-services.json' 
    from "src/debug" 
    include "google-services.json" 
    into "." 
} 

task switchToRelease(type: Copy) { 
    description = 'Switches to RELEASE google-services.json' 
    from "src/release" 
    include "google-services.json" 
    into "." 
} 

大 - 但有你建立你的應用程序很麻煩之前,需要手動運行這些任務。我們希望以前運行以上適當的複製任務:運行assembleDebug或:assembleRelease。讓我們看看會發生什麼情況:assembleRelease運行時:

Zaks-MBP:my_awesome_application zak$ ./gradlew assembleRelease 
Parallel execution is an incubating feature. 
.... (other tasks) 
:app:processReleaseGoogleServices 
.... 
:app:assembleRelease 

請注意:app:processReleaseGoogleServices任務。此任務負責處理根目錄google_services.json文件。我們希望處理正確的google_services.json,因此我們必須事先立即運行我們的複製任務。 將此添加到您的build.gradle。請注意afterEvaluate封閉。

afterEvaluate { 
    processDebugGoogleServices.dependsOn switchToDebug 
    processReleaseGoogleServices.dependsOn switchToRelease 
} 

現在,隨時:app:processReleaseGoogleServices被調用,我們新定義的:app:switchToRelease將事先調用。調試buildType的邏輯相同。您可以運行:app:assembleRelease,發佈版本google_services.json將自動複製到您的應用程序模塊的根文件夾。

https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e

+0

請加,禮貌去https://medium.com/google-cloud/automatic-per-variant-google-services-json-configurations-with-gradle-d3d3e40abc0e –

+0

謝謝您的答覆,但我正在嘗試配置多個JSON配置文件_per構建flavor_,例如開發,測試和生產(無需創建新口味)。 – user3352488