Kenneth
Kenneth

Reputation: 4065

Android gradle define method not found

Im trying to create a custom method to be called in the product flavors.

current build script:

   buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

def defineHost(String val) {
    buildConfig "public static final String host = \"" + val + "\";";
}

android {

    compileSdkVersion 17
    buildToolsVersion "18.1.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 18
    }

    productFlavors {

        flavor1 {
            packageName "flavor1"
            defineHost("test")
        }
        flavor2 {
            packageName "flavor2"
            defineHost("test2")
        }

    }

}
dependencies {
    compile 'com.android.support:appcompat-v7:+'
}

Im getting the following error:

Gradle: A problem occurred evaluating project ':Test'.

Could not find method defineHost() for arguments [test] on GroupableProductFlavorDsl_Decorated{name=flav1, minSdkVersion=-1, targetSdkVersion=-1, renderscriptTargetApi=-1, versionCode=-1, versionName=null, packageName=flavor1, testPackageName=null, testInstrumentationRunner=null, signingConfig=null}.

Im trying to add a couple of variables to BuildConfig for earch product variant. I COULD write the whole thing in defineHost in each variant, but i want it clean. Open for other suggestions on how to achieve variant-configured constants in BuildConfig.

Upvotes: 3

Views: 1854

Answers (2)

Kenneth
Kenneth

Reputation: 4065

Ended up just using the new 'buildConfigField "boolean", "PRE_VERSION", "false";'

Upvotes: 1

marczych
marczych

Reputation: 967

I believe that this will work:

def defineHost(String val) {
    return "public static final String host = \"" + val + "\";"
}


buildConfig defineHost("test")

I have not tested this, although I do something similar in one of my apps.

Upvotes: 0

Related Questions