2

I need to define a method in build.gradle to define BuildConfigFields . What i have tried is below .

defaultConfig {
    initializeBuildConfig()
    applicationId com.terser
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    ext.initializeBuildConfig = {->
        Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(file('config.properties')))
        def properties_appName = versionProps['APP_NAME']
        manifestPlaceholders = [appname:properties_appName]

        def properties_authkey=versionProps['AUTH_KEY']
        buildConfigField "String", "AUTH_KEY", "\"$properties_authkey\""
    }
}

But its not working giving the error .

Could not find method initializeBuildConfig() for arguments [] on DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={appname=invite}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.DefaultConfig.

Can anyone help me ? I would like to know how can i define a method in same block and also in other blocks and call it. Also how can i return a value from a method ?
I have already checked out How to define and call custom methods in build.gradle. The answer there is seems correct but i could not grab the concept from the answers given in the above link . I have tried to implement the same but getting the above error.

Diaz diaz
  • 284
  • 1
  • 7
  • 21

1 Answers1

0

One way to

ext.myCustomMethod = { param1, param2 ->
    // Method body here
}

Note that this gets created for the project scope, ie. globally available for the project, which can be invoked as follows anywhere in the build script using myCustomMethod(p1, p2) which is equivalent to project.myCustomMethod(p1, p2)

The method can be defined under different scopes as well, such as within tasks:

task myTask {
    ext.myCustomMethod = { param1, param2 ->
        // Method body here
    }

    doLast {
        myCustomMethod(p1, p2) // This will resolve 'myCustomMethod' defined in task
    }
}

Like :

task ndkBuild(type: Exec, description: 'Compile JNI source with NDK') {
        Properties properties = new Properties()
        properties.load(project.rootProject.file('local.properties').newDataInputStream())
        def ndkDir = properties.getProperty('ndk.dir')
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine "$ndkDir/ndk-build.cmd", '-C', file('src/main/jni').absolutePath
        } else {
            commandLine "$ndkDir/ndk-build", '-C', file('src/main/jni').absolutePath
        }
    }

When we use ext, the scope is restricted to where it is defined, ie. if it is defined under a task, it is local to the task. The way it works is through entities (like project, task etc.) that implement ExtensionAware. This adds an ExtraPropertiesExtension which is configured through the ext directive.

Mayur Patel
  • 2,300
  • 11
  • 30
  • This seems came from https://stackoverflow.com/questions/27777591/how-to-define-and-call-custom-methods-in-build-gradle. I have already implemented it but i get error . Can you tell me why ? – Diaz diaz Jun 14 '18 at 05:30
  • @Diazdiaz you may check this https://stackoverflow.com/a/37100291/6238866 – Mayur Patel Jun 14 '18 at 05:32
  • @Diazdiaz you have right this came from https://stackoverflow.com/a/27778202/6238866 here and i have also impliemented this in my project and its working. Check : http://prntscr.com/jurbjs – Mayur Patel Jun 14 '18 at 05:36