您的位置:首页 > 移动开发 > Android开发

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

2014-09-30 14:11 363 查看
转自: 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]

Android图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: