2014-10-10 49 views
12

我目前正在實施API密鑰切換腳本建議here,除了使用生成類型而不使用flavor。我的build.gradle看起來是這樣的:Crashlytics在運行時無法在crashlytics.properties中找到API密鑰

... 
buildTypes { 
    debug { 
     ... 
     set("crashlyticsApiKey", "API_KEY_1") 
     set("crashlyticsApiSecret", "API_SECRET_1") 
    } 
    release { 
     ... 
     set("crashlyticsApiKey", "API_KEY_2") 
     set("crashlyticsApiSecret", "API_SECRET_2") 
    } 
} 
... 
productFlavors{...} 
... 
File crashlyticsProperties = new File("${project.projectDir.absolutePath}/crashlytics.properties") 

applicationVariants.all { variant -> 
    variant.productFlavors.each { flavor -> 
     def variantSuffix = variant.name.capitalize() 
     def generateResourcesTask = project.tasks.getByName("crashlyticsGenerateResources${variantSuffix}") 
     def generatePropertiesTask = task("crashlyticsGenerateProperties${variantSuffix}") << { 
      Properties properties = new Properties() 
      println "...copying apiKey for ${variant.name}" 
      properties.put("apiKey", variant.buildType.crashlyticsApiKey) 
      println "...copying apiSecret for ${variant.name}" 
      properties.put("apiSecret", variant.buildType.crashlyticsApiSecret) 
      properties.store(new FileWriter(crashlyticsProperties), "") 
     } 
     generateResourcesTask.dependsOn generatePropertiesTask 
     def cleanResourcesTask = project.tasks.getByName("crashlyticsCleanupResourcesAfterUpload${variantSuffix}") 
     cleanResourcesTask.doLast { 
      println "...removing crashlytics.properties" 
      crashlyticsProperties.delete() 
     } 
    } 
} 
... 

的gradle這個文件成功生成,並與根據生成類型的正確信息crashlytics.properties更新。建議使用這種使用crashlytics.properties的方法here,並且似乎除了在gradle文件中包含依賴關係之外沒有任何其他更新。然而,當Crashlytics.start(this)從主要活動叫,我得到一個運行時異常:

java.lang.RuntimeException: Unable to create application com.lookout.LookoutApplication: java.lang.IllegalArgumentException: Crashlytics could not be initialized, API key missing from AndroidManifest.xml. Add the following tag to your Application element 
<meta-data android:name="com.crashlytics.ApiKey" android:value="YOUR_API_KEY"/> 

剝離下來到一個靜態crashlytics.properties文件(即在gradle這個文件中刪除動態腳本,只是有一個apiKey和在crashlytics.properties中的apiSecret)會產生相同的錯誤,即使它成功構建。

是否對AndroidManifest或build.gradle文件進行了一些更改,我應該將它指向crashlytics.properties?

回答

1

雖然這不是原始問題的答案(因爲即時運行在2014年不存在),但您可能會發現即時運行可能會導致問題。我的過程是:

  • 安裝插件面料
  • 生成Crashlytics代碼(包括在清單中的API密鑰)
  • 切換到fabric.properties文件
  • 花一個小時試圖弄清楚爲什麼它不工作
  • 禁用即時運行 - >重建 - >安裝 - >成功

我對Android的工作室2.0.0-beta6。這可能會在未來得到解決,但這是我在網上找到的唯一資源,因此希望我可以在那個小時內拯救別人。

+1

禁用InstantR我解決了這個問題。希望我在嘗試預言之前花了一個小時才找到這篇文章。 – Soumya 2017-11-01 00:07:26

8

工作正常:

# Fabric properties file: app/fabric.properties 
apiSecret=xx68f6074dxxxxxc11dxxx97c172e8ebf0 
apiKey=xxxe76c4xxxx97e8cxxxx0135e9d46f5a2xxx 

添加上的.gitignore(開源項目)

REMOVE項上的AndroidManifest.xml:

<meta-data 
    android:name="io.fabric.ApiKey" 
    android:value="xxx6c41xxx6ec601xxxd4xxxa2" /> 

公報文檔:https://docs.fabric.io/android/fabric/settings/working-in-teams.html#android-projects

+0

無法使用fabric.properties工作。由於缺少清單條目,仍然收到織物無法初始化的消息 – CyberpunkDreams 2018-01-16 21:55:38