Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pre build event to copy files to assets folder in Android application

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.

like image 538
vlasentiy Avatar asked Dec 17 '15 12:12

vlasentiy


2 Answers

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)
like image 98
j2emanue Avatar answered Sep 20 '22 08:09

j2emanue


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}')
like image 31
Marc_Alx Avatar answered Sep 18 '22 08:09

Marc_Alx