logo
当前位置:首 页 > 移动开发 >android > 查看文章

android studio 打包

android, 编程技术 你是第4194个围观者 0条评论 供稿者: 标签:, ,

用软件打包每次的名称是不是都是app-release.apk这个名字的,low倒是无所谓但是每次在同一个路径下,就覆盖了旧的,没有对比啊有没有,手动命名太累了,要怎么办呢?

使用Gradle 配置文件修改默认命名格式以更方便的管理

 

我现在格式是这样的
包名_版本名称_生成时间的哈希-release|debug.apk
例如 com.ziaostudio.www_1.0.0.0_980550381-release.apk

 

实现的方法是在app的module里的build.gradle文件中,
先在android { ...}节点外面加上下面的函数 作用是返回时间的哈希值

def releaseTime() {
    return new Date().time.hashCode()
}

 

 

之后在android { ...}里面加上下面一段代码,即可修改生成类似上面的apk的文件名。

 

android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                //这里修改apk文件名
                def fileName = outputFile.name.replace("app","${defaultConfig.applicationId }_${defaultConfig.versionName}_${releaseTime() }")
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

 

 

这样包名就不会单调而且重复了,是不是不那么low了呢;

 

包名没问题了,现在还有个问题是如果app有加入友盟统计的话,那么,累人的事又来了

 

在AndroidManifest.xml里面会有这么一段:

<meta-data
    android:name="UMENG_CHANNEL"
    android:value="Channel_ID" />

里面的Channel_ID就是渠道标示。我们的目标就是在编译的时候这个值能够自动变化

 

第一步 在AndroidManifest.xml里配置PlaceHolder

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

 

 

第二步 在build.gradle设置productFlavors

 

android {  
    productFlavors {
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        _360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }
    }  
}

 

 

或者批量修改

 

android {  
    productFlavors {
        xiaomi {}
        _360 {}
        baidu {}
        wandoujia {}
    }  
 
    productFlavors.all { 
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] 
    }
}

 

 

很简单清晰有没有?直接执行 ./gradlew assembleRelease , 然后就可以静静的喝杯咖啡等待打包完成吧。

 

 

assemble结合Build Variants来创建task

 

  • ./gradlew assembleDebug
  • ./gradlew assembleRelease

除此之外 assemble 还能和 Product Flavor 结合创建新的任务,其实 assemble 是和 Build Variants 一起结合使用的,而 Build Variants = Build Type + Product Flavor , 举个例子大家就明白了:

如果我们想打包wandoujia渠道的release版本,执行如下命令就好了:

  • ./gradlew assembleWandoujiaRelease

如果我们只打wandoujia渠道版本,则:

  • ./gradlew assembleWandoujia

此命令会生成wandoujia渠道的Release和Debug版本

同理我想打全部Release版本:

  • ./gradlew assembleRelease

这条命令会把Product Flavor下的所有渠道的Release版本都打出来。

总之,assemble 命令创建task有如下用法:

  • **assemble**: 允许直接构建一个Variant版本,例如assembleFlavor1Debug。
  • **assemble**: 允许构建指定Build Type的所有APK,例如assembleDebug将会构建Flavor1Debug和Flavor2Debug两个Variant版本。
  • **assemble**: 允许构建指定flavor的所有APK,例如assembleFlavor1将会构建Flavor1Debug和Flavor1Release两个Variant版本。

完整的gradle脚本

最后福利大放送,来一份我在项目中使用的完整的gradle文件配置:

 

apply plugin: 'com.android.application'
 
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
 
android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
 
    defaultConfig {
        applicationId "com.boohee.*"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
 
        // dex突破65535的限制
        multiDexEnabled true
        // 默认是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }
 
    lintOptions {
        abortOnError false
    }
 
    signingConfigs {
        debug {
            // No debug config
        }
 
        release {
            storeFile file("../yourapp.keystore")
            storePassword "your password"
            keyAlias "your alias"
            keyPassword "your password"
        }
    }
 
    buildTypes {
        debug {
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
 
            versionNameSuffix "-debug"
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }
 
        release {
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
 
            minifyEnabled true
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
 
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    	// 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }
 
    // 友盟多渠道打包
    productFlavors {
        wandoujia {}
        _360 {}
        baidu {}
        xiaomi {}
        tencent {}
        taobao {}
        ...
    }
 
    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}
 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.jakewharton:butterknife:6.0.0'
    ...
}

 

 

 

说说梦想,谈谈感悟 ,聊聊技术,有啥要说的来github留言吧 https://github.com/cjx2328

—— 陈 建鑫

陈建鑫
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容。站长联系:cjx2328#126.com(修改#为@)
Copyright ©ziao Studio All Rights Reserved. E-mail:cjx2328#126.com(#号改成@) 沪ICP备14052271号-3