2

I've got some assets for my Android project that aren't checked in with the source, but instead get downloaded by the build system. I've figured out how to download and extract them in gradle, but I seem to be doing it too late in the build process, because they aren't getting copied to the apk.

def assetsDir = new File(projectDir, 'src/main/assets')

task extractAssets(type: Exec) {

  def wd = assetsDir
  def extractDir = 'extractedStuff'

  outputs.dir "${wd}/${extractDir}"

  workingDir wd
  commandLine 'tar', 'xzf', 'assets-to-extract.tgz', extractDir
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn extractAssets
}

The first time I run this after a clean, it extracts the assets into the assets directory, but doesn't include them in my apk. After that, whenever I build, it includes the assets in the apk. My conclusion is that I should make my extractAssets task happen earlier in the build process (before mergeAssets maybe?). If so, where do I add the dependsOn?

Cypress Frankenfeld
  • 2,317
  • 2
  • 28
  • 40
  • I recently found a similar question which has many potential answers. I'll be trying them to see what works. http://stackoverflow.com/questions/17213236/how-to-run-copy-task-with-android-studio-into-assets-folder – Cypress Frankenfeld Jul 09 '14 at 14:25

1 Answers1

3

In your gradle file, you can specify that your there's a task to execute before the build :

preBuild.dependsOn(extractAssets)

That will execute your "extractAssets" before the build

Hope it helps!

Tr4X
  • 309
  • 1
  • 8
  • I'm about to try today! – Cypress Frankenfeld Jul 09 '14 at 13:46
  • That works, but when I clean, the assets do not get deleted properly. Is preBuild also running after a clean? – Cypress Frankenfeld Jul 09 '14 at 14:21
  • You can use clean.dependsOn(yourCleanTask) to delete some files/folders when Clean is called, by exemple : task yourCleanTask(type: Delete) { delete 'libs/whatever' } – Tr4X Jul 09 '14 at 14:55
  • I was already doing that, and it was working fine, however after I started using preBuild.dependsOn(extractAssets) the clean task seemed to do everything except delete the assets I extracted. I'm guessing this is because preBuild gets run on clean? – Cypress Frankenfeld Jul 09 '14 at 15:32
  • I don't think that preBuild gets run on clean, but be careful that Android Studio often triggers a build right after calling Clean. To see what is really done by the clean, you can run "gradlew clean" at the root of your project – Tr4X Jul 10 '14 at 07:34