How to run copy task with android studio into assets folder

后端 未结 7 961
温柔的废话
温柔的废话 2020-12-01 11:53

So far I have added the following to the end of my \"build.gradle\"

task copyFiles(type: Copy)

copyFiles {
    description = \'Copies html5 files from the c         


        
相关标签:
7条回答
  • 2020-12-01 12:31

    I had a very similar problem to yours and I was able to solve it as follows:

    android.buildTypes.all{ buildType ->    
        task "${buildType.name}CopyFiles" (type: Copy)
        "${buildType.name}CopyFiles" {
            description = 'Copies html5 files from the common library...'
            from '../../www'
            into 'assets/www'
            include('**/*')
        }
    
        tasks.getByPath(":${project.name}:assemble${buildType.name.capitalize()}").dependsOn "${buildType.name}CopyFiles"
    }
    

    The problem is, that Android Studio seems to call a specific assemble task like assembleDebug when you click on run, that's why you have to make sure to make all assemble tasks depend on your copy task.

    0 讨论(0)
  • 2020-12-01 12:33

    Try this below your build.gradle:

    tasks.whenTaskAdded { task ->
        if (task.name == 'assemble') {
            task.dependsOn copyFiles
        }
    }
    

    In my case I manipulate some 'token' values inside res/values/strings.xml, and then copy it into ${project_root}/build/filtered-resources due to project cleaning issue.

    To work correctly with this manipulated resource, android.sourceSets.res should be redefined to copied folder.

    For your case copy assets folder with your www resource into ${PROJECT_ROOT}/build/your/desired/location, and asign android.sourceSets.assets point to it.

    0 讨论(0)
  • 2020-12-01 12:34

    This way I do custom copying of file assets in my android-gradle build system

    preBuild.dependsOn copyFiles
    
    0 讨论(0)
  • 2020-12-01 12:37

    I use the following copy task in my project to copy strings into another directory:

    task copyStringsUniversal(type: Copy) {
        from 'src/main/res/values'
        include 'strings.xml'
        include 'array.xml'
        into 'src/universal/res/values'
    }
    build.dependsOn copyStringsUniversal
    
    0 讨论(0)
  • 2020-12-01 12:38

    Add this line to your build.gradle file:

    assemble.dependsOn copyFiles
    assemble.mustRunAfter copyFiles
    

    where assemble can be any task.

    Edit: I added the mustRunAfter bit to make sure the copyFiles task is run before any of the other assemble dependencies.

    0 讨论(0)
  • 2020-12-01 12:56
    task myCopyToAssets(type: Copy) {
        def toDir = rootProject.file('app/assets')
        from (rootProject.file('app/usb')) {
            include 'libhotplug.so'
        }
        into toDir
    }
    
    
    
    tasks.whenTaskAdded { task ->
        //println task.getName()
        if (task.name == 'assembleDebug' || task.name == 'assembleRelease') {
            println 'myCopy'
            myCopyToLibs.execute()
            myCopyToAssets.execute()
        }
    }
    
    0 讨论(0)
提交回复
热议问题