2017-06-21 22 views
-1

我有一些.txt文件(只有.txt文件不是android lib)在Bitbucket回購,我想添加到我的android項目時通過項目建設通過Android工作室(Gradle)。Android的 - 如何下載和添加文本文件從bitbucket在gradle構建

目標實現:隨時修改遠程文件內容,並在構建項目時添加更新的文件。

我研究了很多,但找不到任何解決方案。請幫忙。

+0

的可能的複製[如何下載gradle這個外部文件?(https://stackoverflow.com/questions/17123606/how-to-download-external-文件在gradle) –

回答

1

您可以使用preBuild任務在構建之前下載文件,並使用this method執行下載。下面將下載文件到assets目錄中app模塊的

android { 

    preBuild << { 
     def url = "https://bitbucket.org/HellGate/jquery-slider/raw/5ab0c31aaa57fb7d321076194f462b472f5f031e/index.html" 
     def file = new File('app/src/main/assets/index.html') 
     new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }} 
    } 
} 

如果使用私有倉庫,把你的證書與基本身份驗證方案username:password

android { 

    preBuild << { 
     def url = "https://username:[email protected]/HellGate/jquery-slider/raw/5ab0c31aaa57fb7d321076194f462b472f5f031e/index.html" 
     def file = new File('app/src/main/assets/index.html') 
     new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }} 
    } 
} 

在這種情況下,你可以把他們在local.properties文件中(未提交您的憑據):

file_path=app/src/main/assets/index.html 
ext_url=https://username:[email protected]/bertrandmartel/test/raw/c489ae46c3de9ad7089f53660a8de616af08265d/youtube.html 

preBuild任務讀取性能:

preBuild << { 

    Properties properties = new Properties() 
    properties.load(project.rootProject.file('local.properties').newDataInputStream()) 

    if (properties.containsKey("file_path") && properties.containsKey("ext_url")) { 
     def file = new File(properties.getProperty("file_path")) 
     def url = properties.getProperty("ext_url") 
     new URL(url).withInputStream{ i -> file.withOutputStream{ it << i }} 
    } 
    else{ 
     println("no properties found") 
    } 
} 
+0

我有私人bitbucket存儲庫,如何通過bitbucket憑據? –

+0

請參閱我更新的帖子,瞭解使用憑據並從本地屬性中讀取值 –

+0

我已經在「ext_url」中添加了用戶名和密碼,但您總是將其重定向到登錄頁面,並且還下載登錄頁面的html內容。請記住這是私人回購。 –

相關問題