Gradle动态配置项目

作者: 黑泥卡 | 来源:发表于2016-11-08 23:06 被阅读632次

可以使用三种方式来通过gradle改变项目信息。

编译时动态改变Manifest

例如,在manifest下配置友盟的渠道:

<meta-data
    android:name="UMENG_CHANNEL"
    android:value="${UMENG_CHANNEL_VALUE}"/>

然后在gradle的 product flavor 中写上:

manifestPlaceholders = [UMENG_CHANNEL_VALUE: "GooglePlay"]

在编译后value的值就会变为GooglePlay了。

编译时动态改变resValue

在你的 gradle 内容 buildTypes 或者 productFlavors 下面,如 release 体内写上类似:

resValue "string", "AppName", "imag"

这样就可以修改,strings.xml

编译时动态改变BuildConfig

要想通过编译生成代码中可以获取的数据,那我们需要通过一个介质。那就是BuildConfig类。
他是编译的时候动态生成的。
在相同的地方添加:

buildConfigField "String", "name", "\"value\""

在项目中可以这样使用build.gradle

通过读取一个配置文件,来改变java代码中属性的值。

文件相对路径为“../script/build.properties”,内容如下:

appName=test
versionCode=5.0.0
mapApiKey=CeFoDLBAGKbbvpa4EFTiFuugpPjdxT1f

那么就可以通过读取这个文件来进行配置了:

//声明文件路径
def pFile = file("../script/build.properties")

def Properties p = new Properties()
//读取文件内容
def loadProperties = {
    pFile.withInputStream { stream->
        InputStreamReader read = new InputStreamReader(stream, "utf-8");
        BufferedReader bf = new BufferedReader(read);
        p.load(bf)
    }
}

android {
    buildTypes {
        //正式打包
        release {
          loadProperties()
          //动态创建res-values值
          resValue "string", "app_name", p.appName
          //动态创建常量
          buildConfigField 'String', 'versionCode', '"' + p.versionCode + '"'
          //动态创建manifest中meta的值
          manifestPlaceholders = [MAP_API_KEY:p.mapApiKey]
    }
}

读取文件夹下的文件名称,动态加载arr库文件

def loadPluginNames = {
    String pluginNames = "";
    def pluginFiles = file('../plugins').listFiles().sort()
    pluginFiles.each { File file ->
        if (file.isFile()) {
            String baseName = file.name.subSequence(0, file.name.indexOf('.'));
            String pluginName = baseName.replaceAll('(Plugin)$', '');
            pluginNames = pluginNames + pluginName + ","
        }
    }
    return pluginNames;
}

//引入aar插件
fileTree(dir: 'plugins', include: '**/*.aar').each { File file ->
    dependencies.add("compile", [
        name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name },
        ext: 'aar'
    ])
}

android{
  defaultConfig {
        //动态创建plugins的name
        buildConfigField "String", "PLUGIN_NAMES", "\"" + loadPluginNames() + "\""
    }
}

相关文章

网友评论

  • b3596b7679a3:请问,在读取配置文件时,提示这个错误,是什么原因?Error:(11, 0) Could not get unknown property 'p' for project ':app' of type org.gradle.api.Project.
    b3596b7679a3:@黑泥卡 使用def Properties properties = new Properties()也是一样的错误。
    b3596b7679a3:@黑泥卡
    //读取配置文件的方法,用来管理所有版本的打包参数
    Properties properties = new Properties()
    //读取文件内容
    def loadProperties(path) {
    File pFile = file(path)

    pFile.withInputStream { stream ->
    InputStreamReader read = new InputStreamReader(stream, "utf-8");
    BufferedReader bf = new BufferedReader(read);
    properties.load(bf);
    }
    }

    android {
    compileSdkVersion 26
    defaultConfig {
    applicationId "cn.forestar.myapplication"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    loadProperties("../buildConfig/debug.properties")
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }

    是定义了的,如果函数不调用,现在不提示错误,但是一调用loadProperties就会报这个错误
    No signature of method: java.util.HashMap.load() is applicable for argument types: (java.io.BufferedReader) values: [java.io.BufferedReader@571bd513]
    Possible solutions: clear(), clear(), clone(), find(), sort(), find(groovy.lang.Closure)

    错误定位是定位到 properties.load(bf);这一句

    麻烦帮忙看一下。
    黑泥卡:def Properties p = new Properties()
    这个定义了吗?
  • littleDad:鼓励鼓励 多
    黑泥卡: @Little_Dad 加油😛

本文标题:Gradle动态配置项目

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