Android项目build.gradle多渠道&完整版

(一)Android配置多渠道,使用的是友盟多渠道

1. 配置多渠道,指定渠道处理相关的逻辑

build.gradle配置渠道,指定通用渠道

    productFlavors {
        lonline {}
        ltest {}
        ldevelop {}
        lperform {}
//        xiaomi {}
//        huawei {}
//        meizu {}
//        baidu {}
//        wandoujia {}
//        c360 {}
//        yingyongbao {}
//        tianyi {}
//        anzhuo {}
//        anzhi {}
//        sougou {}
//        c91 {}
//        sougou {}
    }
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }

2. 清单文件配置,AndroidManifest.xml

AndroidManifest.xml文件配置

配置在application目录下

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

3. Application动态配置不同环境

基类Application初始化渠道相关逻辑

渠道主要用来打多个环境的包,配置开发、测试,线上三个地址的环境

//配置环境
    private void initHostAddress(){
        try {
            ApplicationInfo info = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            String value = info.metaData.getString("UMENG_CHANNEL");
            switch (value) {
                case "lonline":
                    environment = HostAddress.ONLINE;
                    break;
                case "ltest ":
                    environment = HostAddress.TEST;
                    break;

                default:
                    environment = HostAddress.getHostAddress(BuildConfig.HOST_ADDRESS);
                    break;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

4. build构建apk指定名称

build构建apk指定名称

gradle文件配置

android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def file = output.outputFile
            def today = new Date().getTime()
            output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName +"-"+today+ ".apk"))
//            def file = variant.outputs[0].outputFile;
//            variant.outputs[0].outputFile = new File(file.parent, file.name.replace(".apk", "-"+today+"-"+ + defaultConfig.versionName + ".apk"))
        }
    }

(二)Android项目build.gradle完整版

apply plugin: 'com.android.application'
//引用配置 dependencies.gradle
def cfg = rootProject.ext.configuration;
def presentationDependencies = rootProject.ext.presentationDependencies
android {
    compileSdkVersion cfg.compileVersion
    buildToolsVersion cfg.buildToolsVersion
    defaultConfig {
        //语言资源,只支持中文
        resConfigs "zh"
        applicationId cfg.applicationId
        minSdkVersion cfg.minSdk
        targetSdkVersion cfg.targetSdk
        versionCode cfg.versionCode
        versionName cfg.versionName
        //支持多dex
        multiDexEnabled true
    }
    //配置友盟多渠道包
    productFlavors {
     lonline {}
        ltest {}
        ldevelop {}
        lperform {}
          xiaomi {}
//        huawei {}
//        meizu {}
//        baidu {}
//        wandoujia {}
//        c360 {}
//        yingyongbao {}
//        tianyi {}
//        anzhuo {}
//        anzhi {}
//        sougou {}
//        c91 {}
//        sougou {}
    }
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
    //去除lint代码检测
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
    signingConfigs {
//        debug {
//            storeFile file("xxxx.keystore")
//            storePassword 'xxxx'
//            keyAlias 'xxxx'
//            keyPassword 'xxxx'
//        }
        release {
            storeFile file("xxxx.keystore")
            storePassword 'xxxx'
            keyAlias 'xxxx'
            keyPassword 'xxxx'
        }
    }
    buildTypes {
        debug {
            //指定签名的配置
//            signingConfig signingConfigs.debug
            //启用代码混淆
            minifyEnabled true
            // 移除无用的resource文件
            shrinkResources true
            //so兼容多cpu,目前只包armeabi-v7a
            ndk {
                abiFilters "x86", 'x86_64', "arm64-v8a", "armeabi-v7a", "armeabi"
            }
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
        release {
            //指定签名的配置
//            signingConfig signingConfigs.release
            //启用代码混淆
            minifyEnabled true
            // 移除无用的resource文件
            shrinkResources true
            //so兼容多cpu
            ndk {
                abiFilters "x86", 'x86_64', "arm64-v8a", "armeabi-v7a", "armeabi"
            }
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
    //指定jni目录so库
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
}


dependencies {
    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'
    //    })
    //    testCompile 'junit:junit:4.12'
//    compile presentationDependencies.appcompatV7
    compile presentationDependencies.constraintLayout

    //butterKnife
    compile presentationDependencies.butterKnife
    annotationProcessor presentationDependencies.butterknifeCompiler

}

主项目 build.gradle 指定引用 dependencies.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
//配置dependencies.gradle文件引用
apply from: 'dependencies.gradle'
buildscript {
    repositories {
        jcenter()
//        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}

allprojects {
    repositories {
        jcenter()
//        maven {
//            url 'http://maven.aliyun.com/nexus/content/groups/public'
//        }
//        mavenCentral();
    }
}

dependencies.gradle配置


allprojects {
    repositories {
        jcenter()
//        mavenCentral()
//        maven { url "https://jitpack.io" }
    }
}

ext {
    configuration = [
            applicationId    : "com.doublechina.automatic",
            buildToolsVersion: "25.0.0",
            compileVersion   : 25,
            minSdk           : 14,
            targetSdk        : 25,
            versionCode      : 1,
            versionName      : "1.0"
    ]
    libraries = [
//            appcompatV4                  : "23.4.0",
            //google官方V7公开库
            appcompatV7                 : "25.3.0",
            appcompatV4                 : "25.3.0",
            constraintLayout            : "1.0.2",
            //butterknife依赖注入框架
            // annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'可以不引用
            butterknifeVersion          : '8.5.1',
            daggerVersion               : '2.10',
            retrofitVersion : '2.1.0',
            rxAndroidVersion : '1.2.1',
            picassoVersion : '2.5.2'
    ]

    presentationDependencies = [
            //google官方依赖包
            appcompatV4 : "com.android.support:support-v4:${libraries.appcompatV4}",
            appcompatV7 : "com.android.support:appcompat-v7:${libraries.appcompatV7}",
            constraintLayout : "com.android.support.constraint:constraint-layout:${libraries.constraintLayout}",
            recyclerView: "com.android.support:recyclerview-v7:${libraries.appcompatV7}",
            cardView: "com.android.support:cardview-v7:${libraries.appcompatV7}",


            //butterknife依赖注入框架
            butterknifeCompiler : "com.jakewharton:butterknife-compiler:${libraries.butterknifeVersion}",
            butterKnife : "com.jakewharton:butterknife:${libraries.butterknifeVersion}",

            //dagger依赖注入框架
            dagger : "com.google.dagger:dagger:${libraries.daggerVersion}",
            daggerCompiler : "com.google.dagger:dagger-compiler:${libraries.daggerVersion}",

            //rxAndroid 观察者事件绑定
            rxAndroid : "io.reactivex:rxandroid:${libraries.rxAndroidVersion}",

            //retrofit网络请求,须和okhttp一起使用
            retrofit : "com.squareup.retrofit2:retrofit:${libraries.retrofitVersion}",
            //retrofit Gson转换工具
            retrofitGson : "com.squareup.retrofit2:converter-gson:${libraries.retrofitVersion}",
            //retrofit Adapter
            retrofitAdapterRxJava: "com.squareup.retrofit2:adapter-rxjava:${libraries.retrofitVersion}",

            //图片下载
            picasso: "com.squareup.picasso:picasso:${libraries.picassoVersion}",

    ]
}

欢迎大家star demo https://github.com/doublechina/Call

你可能感兴趣的:(android,android-studio)