Android-->build.gradle-->productFlavors

来源:互联网 发布:什么是无约束优化问题 编辑:程序博客网 时间:2024/05/21 16:23

首先贴出官网:https://google.github.io/android-gradle-dsl/current/index.html

最外层的build.gradle是配置一些module共用的内容

// Top-level build file where you can add configuration options common to all sub-projects/modules.//构建脚本的配置buildscript {    //仓库    repositories {        jcenter()//        maven{//            url 'https://xxx/xxx'//        }    }    //依赖    dependencies {        classpath 'com.android.tools.build:gradle:2.3.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}//配置子模块的共用行为allprojects {    repositories {        jcenter()    }}//clear任务task clean(type: Delete) {    delete rootProject.buildDir}

settings.gradle是配置项目中引入了那几个模块,或者修改名称

//路径不能用/,只能用:include ':app', ':lib:library'//project(':app').buildFileName = 'abc.gradle'

gradle/wrapper/gradle-wrapper.properties

这个文件主要配置了我们要用的gradle的版本,如果新拷贝进来一个项目,本地没有对应的gradle的话,可以直接修改这个文件里的版本,就不用等待他下载了


本地路径在:C:\Users\Administrator.gradle\wrapper\dists,记的点击进去看看是否下载完成了

#Wed Sep 06 11:22:43 CST 2017distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https://services.gradle.org/distributions/gradle-3.3-all.zip

module里面的build.gradle

//引入插件apply plugin: 'com.android.application'//配置android插件android {    //编译使用的SDK版本    compileSdkVersion 25    //buildTools版本    buildToolsVersion "25.0.3"    //默认的产品风味    defaultConfig {        //包名,执行构建时会替换掉Manifest当中的package节点        //原来的节点会拼接给name节点以'.'开头的,比如:android:name=".MainActivity"        applicationId "com.pf.listener13"        //applicationId的后缀        applicationIdSuffix '.testsuffix'        minSdkVersion 15        targetSdkVersion 25        versionCode 1        versionName "1.0"        versionNameSuffix ".suffix"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        //支持java8的部分特性,如lamda        jackOptions {            enabled true        }        //namifest占位符,相当于传值        manifestPlaceholders = [name: '1']        //65K        multiDexEnabled true        //在文件中表示哪些类要打包到主dex中        //每一行表示要分到主dex中的类  com/a/b/c/C.class//        multiDexKeepFile project.file("multi-dex-keep-file.txt")        //使用Proguard规则来定义分到主dex的类        //语法和混淆差不多        //-keep class com.a.b.c        //-keep class com.a.b.**{*;}//        multiDexKeepProguard project.file("multi-dex-keep-proguard.txt")        //构建ndk的配置        externalNativeBuild {            //Android.mk//            ndkBuild {////            }            //cmake            cmake {                cppFlags "-frtti -fexceptions"            }        }        //定义打包的cpu架构支持        ndk {            abiFilters "armeabi-v7a", "x86"        }        //打包配置文件        proguardFiles 'proguard-rules.pro'        //svg配置        vectorDrawables {            useSupportLibrary true            generatedDensities = ["mdpi"]        }        //在BuildConfig里添加变量        buildConfigField('String', 'h', '"testBuildConfigField"')        //res里value里创建变量        resValue('string', 'hh', '"aaaaaaa"')    }    //指定ndk的构建文件//    externalNativeBuild {//        cmake {//            path "CMakeLists.txt"//        }//    }    //创建两个维度    flavorDimensions('product', 'abi')    //创建产品风味    productFlavors {        free {            dimension 'product'        }        pro {            dimension 'product'        }        armeabiV7a {            dimension 'abi'        }        x86 {            dimension 'abi'        }    }    //过滤变体//    variantFilter {//        variant ->//        variant.flavors.each {//            if(it.name.concat('pro')){//                //setIgnore(true)//            }//        }//    }    //构建类型    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}configurations {    freeArmeabiV7aDebugCompile {}}dependencies {    freeArmeabiV7aDebugCompile project(':lib:library')    compile fileTree(dir: 'libs', include: ['*.jar'])    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'}

manifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.pf.listener13">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <!--${name}接收build.gradle里传来的值-->        <meta-data            android:name="name"            android:value="${name}" />        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>