Gradle 进行 Android 多渠道多包名换图片全自动打包

转自: http://www.eoeandroid.com/thread-325377-1-1.html

在用过maven,ant等项目等工具后后,发现觉得Android最佳的项目管理以及打包实践:Git/SVN+Jenkins+Gradle
Gradle 脚本:
[mw_shl_code=java,true]import com.android.builder.DefaultManifestParser  

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.6'//依赖
    }
}

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')//添加android依赖libs
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"
        //签名
    signingConfigs {
        myConfig {
                        storeFile file('ext/test.keystore')
                        storePassword '123456'
                        keyAlias '123456'
                        keyPassword '123456'
        }
    }
    
    defaultConfig {
        versionCode 1
        versionName getVersionName()
        minSdkVersion 8
        targetSdkVersion 19
        debuggable=false;
    }

    //渠道
    productFlavors {
        google{
        
        }
        tantai{
        
        }
    }
    
    buildTypes{

        release {
            signingConfig signingConfigs.myConfig
            runProguard true
            proguardFile 'proguard.cfg'
        }
    }
        
    sourceSets {
        main {
            manifest {
                srcFile 'AndroidManifest.xml'
            }
            java {
                srcDir 'src'
            }
            res {
                srcDir 'res'
            }
            assets {
                srcDir 'assets'
            }
            resources {
                srcDir 'src'
            }
            aidl {
                srcDir 'src'
            }
                }
        }
}


tasks.withType(Compile) {
    //options.encoding = "UTF-8"
}
//替换AndroidManifest.xml的UMENG_CHANNEL_VALUE字符串为渠道名称
android.applicationVariants.all{ variant -> 
    variant.processManifest.doLast{ 
        copy{
            from("${buildDir}/manifests"){
                include "${variant.dirName}/AndroidManifest.xml"
            }
            into("${buildDir}/manifests/$variant.name")

            filter{
                String line -> line.replaceAll("UMENG_CHANNEL_VALUE", "$variant.name")
            }

            variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml")
        }    
   }
        def manifestParser = new DefaultManifestParser()
        def version =manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)

        //def versionName = android.defaultConfig.versionName 
    //def versionNameSuffix = variant.buildType.versionNameSuffix
    //if(versionNameSuffix.toString().equals("null")){
            //versionNameSuffix = ""
    //}
        //def version= versionName+versionNameSuffix;
    def apk = variant.packageApplication.outputFile;
    println version;
    def newName = version+"//"+"unaligned//"+apk.name.replace(".apk","-"+version+".apk");
    newName=newName.replace("release","release(proguard)");
    variant.packageApplication.outputFile = new File(apk.parentFile, newName);
    if (variant.zipAlign) {
            newName=newName.replace("unaligned//", "");
        variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
    }
    //delete  new File(apk.parentFile, "unaligned");
}[/mw_shl_code]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值