【问题标题】:Define a custom method in build.gradle and call it from same block and from other blocks在 build.gradle 中定义一个自定义方法并从同一个块和其他块调用它
【发布时间】:2018-06-14 15:50:15
【问题描述】:

我需要在 build.gradle 中定义一个方法来定义 BuildConfigFields 。我尝试过的如下。

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

但它不工作给出错误。

在 DefaultConfig_Decorated{name=main, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode 上找不到参数 [] 的方法 initializeBuildConfig() =null,versionName=null,applicationId=null,testApplicationId=null,testInstrumentationRunner=null,testInstrumentationRunnerArguments={},testHandleProfiling=null,testFunctionalTest=null,signingConfig=null,resConfig=null,mBuildConfigFields={},mResValues={}, com.android.build.gradle.internal.dsl.DefaultConfig 类型的 mProguardFiles=[]、mConsumerProguardFiles=[]、mManifestPlaceholders={appname=invite}、mWearAppUnbundled=null}。

谁能帮帮我?我想知道如何在同一个块和其他块中定义一个方法并调用它。另外我如何从方法中返回值 ?
我已经签出How to define and call custom methods in build.gradle。那里的答案似乎是正确的,但我无法从上面链接中给出的答案中抓住这个概念。我已经尝试实现相同但得到上述错误。

【问题讨论】:

    标签: android android-gradle-plugin build.gradle


    【解决方案1】:

    一种方法

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

    请注意,这是为项目范围创建的,即。全局可用于项目,可以在构建脚本中的任何地方使用 myCustomMethod(p1, p2) 调用,相当于 project.myCustomMethod(p1, p2)

    该方法也可以在不同的范围内定义,例如在任务中:

    task myTask {
        ext.myCustomMethod = { param1, param2 ->
            // Method body here
        }
    
        doLast {
            myCustomMethod(p1, p2) // This will resolve 'myCustomMethod' defined in task
        }
    }
    

    点赞:

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

    当我们使用 ext 时,作用域被限制在它被定义的地方,即。如果它是在任务下定义的,则它是任务本地的。它的工作方式是通过实现ExtensionAware 的实体(如项目、任务等)。这会添加一个通过 ext 指令配置的 ExtraPropertiesExtension

    【讨论】:

    猜你喜欢
    • 2015-03-02
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多