gradle多渠道打包

实战场景两个:
1.使用Umeng打多个渠道包。
2.应用生成 正式站、测试站两个版本,一下分别介绍。

成功打包的几个注意事项:
1.仔细看gradle-wapper的版本:文件位置:根目录\gradle\wrapper\gradle-wrapper.properties
仔细看gradle插件的版本:文件位置:根目录\build.gradle
不同的gradle版本和gradle插件版本,相应api会发生变化
我使用的gradle:gradle-3.3 plugin:'com.android.tools.build:gradle:2.3.3'
2.Manifest.xml文件中,渠道号的写法。
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
3.keystore文件。位置:根目录\app\xxx.keystore
4.app moudle下build.gradle的配置。
a.配置签名信息。

        release {
            v1SigningEnabled true
            v2SigningEnabled false
            storeFile file("easaa_useinterface.keystore")
            storePassword "easaa.com"
            keyAlias "easaa.keystore"
            keyPassword "easaa.com"
        }
    }```

b.指定生成的apk文件名。
```buildTypes {
        debug {
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled false
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


            applicationVariants.all { variant ->
                if (variant.buildType.name.equals('release')) {
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith('.apk')) {
                            // 输出apk名称为test_v1.0_2015-01-15_wandoujia.apk
                            def fileName = "DM_${variant.productFlavors[0].name}_${defaultConfig.versionName}.apk"
                            output.outputFile = new File(outputFile.parent, fileName)
                        }
                    }
                }
            }
        }
    } ```

c.指定多少种渠道。
productFlavors {
        YstenJS {
            //这是生成正式站、测试站两种app包用的
           /* manifestPlaceholders = [app_name:"xxxx" ,icon: "@mipmap/ic_launcher_xxxx"]
            resValue("string" , "baseUrl","xxxxxxx")
            android:label="${app_name}"
            android:icon="${icon}"
            value=context.getResource().getString(R.string.key)*/
        }
        dangbei {}
        damai {}

        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
    }
4.运行:gradle clean 
            gradlew assembleRelease
5.完整的代码。

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.onairm.dmgame4tv"
minSdkVersion 15
targetSdkVersion 22
versionCode 200
versionName "2.0.0"
// flavorDimensions "versionCode"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
signingConfigs {
release {
v1SigningEnabled true
v2SigningEnabled false
storeFile file("easaa_useinterface.keystore")
storePassword "easaa.com"
keyAlias "easaa.keystore"
keyPassword "easaa.com"
}
}

buildTypes {
    debug {
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled false
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


        applicationVariants.all { variant ->
            if (variant.buildType.name.equals('release')) {
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为test_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "DM_${variant.productFlavors[0].name}_${defaultConfig.versionName}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }
}

productFlavors {
    YstenJS {
        //这是生成正式站、测试站两种app包用的
       /* manifestPlaceholders = [app_name:"xxxx" ,icon: "@mipmap/ic_launcher_xxxx"]
        resValue("string" , "baseUrl","xxxxxxx")
        android:label="${app_name}"
        android:icon="${icon}"
        value=context.getResource().getString(R.string.key)*/
    }
    dangbei {}
    damai {}

    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.gongwen:marqueelibrary:1.1.1'
compile project(path: ':baselibrary')
compile files('libs/billing.jar')
compile files('libs/glide-3.6.0.jar')

compile files('libs/umeng-analytics-7.4.0.jar')
compile files('libs/umeng-common-1.4.0.jar')
compile files('libs/umerror_android_1.0.0.jar')

}


// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            mavenCentral()
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
源码地址:https://github.com/zhuzhanpeng/GradlePackage

你可能感兴趣的:(gradle多渠道打包)