2017-06-21 19 views
-1

プロジェクトをビルドするときに、アンドロイドプロジェクトに追加したいBitbucketリポジトリに.txtファイル(アンドロイドライブラリではない.txtファイルのみ) Androidスタジオ(Gradle)。Android - gradleビルドでbitbucketからプロジェクトにテキストファイルをダウンロードして追加する方法

達成目標:リモートファイルの内容をいつでも変更し、プロジェクトをビルドするときに更新ファイルを追加します。

私は多くのことを研究していますが、解決策を見つけることができませんでした。助けてください。

+0

可能な複製を(https://stackoverflow.com/questions/17123606/how-to-download-external- files-in-gradle) –

答えて

1

preBuildタスクを使用して、ビルド前にファイルをダウンロードし、this methodでダウンロードを実行できます。以下は、プライベートリポジトリを使用している場合は、あなたの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 }} 
    } 
} 

assetsディレクトリにファイルをダウンロードし、基本的な認証スキーム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タスクのプロパティ読む:[?のGradleで外部ファイルをダウンロードする方法]の

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コンテンツもダウンロードされます。これはプライベートレポですのでご注意ください。 –

関連する問題