19

I have this project structure: ProjectFolder/IosFolder,AndroidFolder,CommonFolder Now android app uses files from it's assets folder. But we decide to make Common folder for the same files.

Could you help me to make function witch will copy files from Common folder(this folder is under my project, so in Android Studio I don't see it) to android assets folder before app will be built?

In Common folder will be some .json files and font files.

As I understand, i need to write this function in my build.gradle file something like that:

task copyFiles(type: Copy)

    copyFiles {
        description = 'Copy files'
        from 'Common/'
        into 'Android/{projectName}/app/src/main/assets'
    }

Here is my file:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "amc.amc_mobile_promo2"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        //For Flurry
        multiDexEnabled = true
    }
    //For Flurry
    /*compileOptions {
        //noinspection GroovyAssignabilityCheck
        sourceCompatibility JavaVersion.VERSION_1_7
        //noinspection GroovyAssignabilityCheck
        targetCompatibility JavaVersion.VERSION_1_7
    }*/
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'

    compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'joda-time:joda-time:2.8.2'
    compile 'com.github.orangegangsters:swipy:1.2.0@aar'

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.okhttp:okhttp:2.6.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.6.0'

    /*compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-identity:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'*/
}

And could you tell me where can i see results of executed methods in Gradle Console?

What path i need to use and where in build.gradle file situate this method?

Hope you will help me.

vlasentiy
  • 342
  • 1
  • 6
  • 12

2 Answers2

17

Can you try this configuration:

gradle.projectsEvaluated {
     preBuild.dependsOn(copyFiles)
}

update: there are many commands the copy task can do for you. from the docs here are examples:

task anotherCopyTask(type: Copy) {
// Copy everything under src/main/webapp
from 'src/main/webapp'
// Copy a single file
from 'src/staging/index.html'
// Copy the output of a task
from copyTask
// Copy the output of a task using Task outputs explicitly.
from copyTaskWithPatterns.outputs
// Copy the contents of a Zip file
from zipTree('src/main/assets.zip')
// Determine the destination directory later
into { getDestDir() }

}

if you just want to copy from one source directory to another you can do this :

task copyFiles(type: Copy) {
    from 'pathToMyAssets'
    into 'AndroidStudioAssetsFolderPath'
}

UPDATE do this in your app's build.gradle at the very bottom:

task copyFiles(type: Copy) {
    from 'Users/kostya/repo_amc_mobile_promo/Common/'
    into 'Users/kostya/repo_amc_mobile_promo/Android/AMC_Mobile_Promo2/app/src/main/assets'
}

preBuild.dependsOn(copyFiles)
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Could you help with path of folders what i need in method? task copyFiles(type: Copy) copyFiles { description = 'Copy files' from 'Common/' into '{project}/app/src/main/assets/' include('**/*') } gradle.projectsEvaluated { preBuild.dependsOn(copyFiles) } – vlasentiy Dec 17 '15 at 12:37
  • i updated my answer. are you asking me to get the paths for you ? or is this enough to get you going ? – j2emanue Dec 17 '15 at 14:04
  • Yes, please, could you help me with the paths of this files because my Common folder situates not in project folder. i tried your last variant with paths: from '{projectRepository}/Common' into '{projectRepository}/Android/{projectName}/app/src/main/assets' but it does not copy files. And one more question: when i build and run app, can i see this copied files in assets folder in Android Studio? – vlasentiy Dec 17 '15 at 14:29
  • can you give the path to the commons folder ? the exact path ? – j2emanue Dec 17 '15 at 14:58
  • Yes, of course, I'm on Mac, so: Macintosh HD ▸ Users ▸ kostya ▸ repo_amc_mobile_promo - there Common folder; Path to the project assets folder: Macintosh HD ▸ Users ▸ kostya ▸ repo_amc_mobile_promo ▸ Android ▸ AMC_Mobile_Promo2 ▸ app ▸ src ▸ main – vlasentiy Dec 17 '15 at 15:04
  • i updated my answer. For some reason preBuild would not run the copy command but it did execute. Very strange. I used a shell script to get the task done – j2emanue Dec 17 '15 at 22:58
  • Thank you very, very, very much ! It works. Just ' / ' to the beginning of the paths and it works ! Thank you for you help ! I appreciate you ! – vlasentiy Dec 18 '15 at 12:42
  • One more question: i want to make relative path for 'from' : String $projectPath= rootProject.projectDir.absolutePath String $path='/Common' from $projectPath+$path into '../app/src/main/assets' But it does not understand my part of '/Common' – vlasentiy Dec 18 '15 at 15:01
  • Find answer for my last question: for relative path i used '../../../Common'. Good luck to all ! – vlasentiy Dec 21 '15 at 08:55
3

According to documentation here's how to copy a file via gradle:

task myCopyTask(type: Copy) {
    from file("${buildDir}/reports/my-report.pdf")
    into file("${buildDir}/toArchive")
}

To do it before build you must add the following line:

preBuild.dependsOn(myCopyTask)

You can use absolute or relative path that may use some of the project properties. To ensure path of project properties do not hesitate to print it using :

println file('${buildDir}')
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
Marc_Alx
  • 1,285
  • 17
  • 25