Home > Blockchain >  Create gradle file programmatically from build.gradle file
Create gradle file programmatically from build.gradle file

Time:07-22

I have an assignment task so I have downloaded the project to implement the task but found in readme file

"you still need to register for an API key and add it to app/local.gradle - see local.gradle.example for details."

and found inside

local.gradle.example

android {
    defaultConfig {
        buildConfigField "String", "API_KEY", "\"api key\""
    }
}

and the

app/local.gradle

was not there

and inside build.gradle file I have found

if (!file("local.gradle").exists()) {
exec {
    commandLine "sh"
    args = ["-c", "cp local.gradle.example local.gradle"]
    }
}
apply from: "local.gradle"

when I tried to run the project it gives an error says

java.io.IOException: Cannot run program "sh" (in directory "E:\android-assignment-main\app"): CreateProcess error=2, The system cannot find the file specified

so how to first create local.gradle file? from programmatically from build.gradle

CodePudding user response:

It seems the issue is that you are on Windows and try to run a shell script, which will not work like this.

I would suggest to use Gradle's Copy task to do this:

task copyLocalGradle(type: Copy) {
    if (!file("app/local.gradle").exists()) {
        from 'local.gradle.example'
        into 'app'
        rename 'local.gradle.example', "local.gradle"
    }
}
  • Related