美文网首页Android开发
gradle自定义变量

gradle自定义变量

作者: Mlxff | 来源:发表于2019-07-15 17:15 被阅读0次

    1. 定义变量

    可以把变量定义在一个文件里面

    gradle相关的配置文件要以 .gradle 为扩展名

    比如定义一个文件:moduleConfig.gradle
    文件内容:

    //buildType在release下的调试模式
    ext.releaseDebuggable = false
    
    //buildType在debug下的调试模式
    ext.debugDebuggable = true
    
    //是否只启用armeabi---用于解决tbs不支持64位的问题,只能使用armeabi
    ext.onlyArmabi = false
    
    //bugly编译打包调试模式开关
    ext.buglyDebug = false
    

    需要把变量到 ext 下面

    2. 定义数组

    下面是两个数组定义的例子

    //首先定义数组
    ext.deps = [:]
    def deps = [:]
    //然后给数组添加值
    deps.a = 'com.gethub.ccmagic:' + MODULE_A + ':' + MODULE_A_VERSION + '@aar'
    deps.b = 'com.gethub.ccmagic:' + MODULE_B + ':' + MODULE_B_VERSION + '@aar'
    deps.c = 'com.gethub.ccmagic:' + MODULE_C + ':' + MODULE_C_VERSION + '@aar'
    deps.d = 'com.gethub.ccmagic:' + MODULE_D + ':' + MODULE_D_VERSION + '@aar'
    //定义的数组赋值到ext上才能引用
    ext.deps = deps
    
    //配置变量控制每个module是作为APP各自运行还是作为lib以来
    def isApp = [:]
    isApp.MODULE_A = false
    isApp.MODULE_B = false
    isApp.MODULE_C = false
    isApp.MODULE_D = false
    ext.isApp = isApp
    

    注意字符串拼接字符串拼接

    这里提一下gradle.properties文件

    • gradle.properties天生就是给gradle配置变量的,其中定义的变量都是可以直接在gradle文件中直接引用的。上述代码中的MODULE_A、MODULE_A_VERSION等等这些都是在gradle中定义过的,所以可以直接引用。
    • groovy中单引号和双引号都能直接用于字符串。
    gradle.properties文件
    # Project-wide Gradle settings.
    # IDE (e.g. Android Studio) users:
    # Gradle settings configured through the IDE *will override*
    # any settings specified in this file.
    # For more details on how to configure your build environment visit
    # http://www.gradle.org/docs/current/userguide/build_environment.html
    # Specifies the JVM arguments used for the daemon process.
    # The mine_setting is particularly useful for tweaking memory settings.
    org.gradle.jvmargs=-Xmx1536m
    # When configured, Gradle will run in incubating parallel mode.
    # This option should only be used with decoupled projects. More details, visit
    # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
    # org.gradle.parallel=true
    # AndroidX package structure to make it clearer which packages are bundled with the
    # Android operating system, and which are packaged with your app's APK
    # https://developer.android.com/topic/libraries/support-library/androidx-rn
    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    #D8 dex
    android.enableR8=false
    
    # module的名称
    MODULE_A=search
    # module的版本
    MODULE_A_VERSION=1.5.7
    # 
    MODULE_B=preview
    MODULE_B_VERSION=1.2.6
    # 
    MODULE_C=share
    MODULE_C_VERSION=1.4.3
    # 
    MODULE_D=usercenter
    MODULE_D_VERSION=2.0.6
    

    相关文章

      网友评论

        本文标题:gradle自定义变量

        本文链接:https://www.haomeiwen.com/subject/lyqokctx.html