简体   繁体   中英

Create gradle file programmatically from build.gradle file

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

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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM