美文网首页安卓学习
Android mac 环境下Gradle 自动打包上传到蒲公英

Android mac 环境下Gradle 自动打包上传到蒲公英

作者: d2f3569f311c | 来源:发表于2016-10-21 18:11 被阅读1000次

    1.使用方法

    1)命令:

    在AndroidStudio的Terminal下,工程目录下运行如下命令:
    ./gradlew myBuild

    2)功能:

    *apk自动打Release包
    apk重名为:AppName_Version_Date.apk
    apk备份到apkFile文件下
    apk上传到蒲公英账号下

    2.build.gradle配置(主Module)

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 24
        buildToolsVersion "24.0.0"
    
    defaultConfig {
        applicationId "com.example.helloworld"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 0
        versionName "1.0.0"
        // multiDexEnabled true
        buildConfigField "long","BUILD_TIMESTAMP",System.currentTimeMillis() + "L"
    }
    //签名
    signingConfigs {
        debug {
            storeFile file("keystore/test.jks")
            keyAlias      "test"
            storePassword "test"
            keyPassword   "test"
        }
        release {
            storeFile file("keystore/test.jks")
            keyAlias      "test"
            storePassword "test"
            keyPassword   "test"
        }
    }
    //多个版本的打包配置
    productFlavors {
        AppName{
            //applicationId "com.example.helloworld"
            //manifestPlaceholders = [GAO_DE_KEY: "your gaode key1", UMENG_KEY: "your umeng key1"]
        }
        // Version2 {
        //applicationId "com.example.helloworld"
        //manifestPlaceholders = [GAO_DE_KEY: "your gaode key2", UMENG_KEY: "your umeng key2"]
        //}
    }
    buildTypes {
        release {
            //debuggable true         //!!! remmeber to false
            minifyEnabled true    //混淆
            zipAlignEnabled true  //Zipalign优化
            shrinkResources true  // 移除无用的resource文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
    
            //将release版本的包名重命名,加上版本及日期
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('release.apk')) {
                        def fileName = "${variant.productFlavors[0].name}_${defaultConfig.versionName}_${releaseTime()}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
        debug{
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }
    lintOptions {
        abortOnError false
    }
    packagingOptions {
        exclude '.readme'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude '!META-INF/MANIFEST.MF'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/maven/com.nineoldandroids/library/pom.xml'
        exclude 'META-INF/maven/com.nineoldandroids/library/pom.properties'
    
        }
    }
    
    def releaseTime() {
        return new Date().format("yyyy-MM-dd",TimeZone.getTimeZone("UTC"))
    }
    def vCode() {
        return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
    }
    
    //上传到蒲公英task
    task myUploadApkTask(type: Exec) {
        commandLine 'sh','-c','./uploadApk.sh'
    
    //test
        executable "sh"
        args "-c", "echo 'shell say hello to you'"
    
    }
    
    //my define
    //运行命令./gradlew myBuild
    //将打包后的文件复制到build目录下,这样就不用深入到apk目录,同时还看不到unaligned的apk了
    task copyTask(type: Copy) {
        from 'build/outputs/apk/'
        into '/Users/***/Documents/Android/apkFiles/AppName'
        exclude '*-unaligned.apk'
    }
    
    //Android Gradle plugin to version 2.1.2 and Gradle to version 2.10
    //task myBuild(dependsOn: ['assembleDebug', 'assembleRelease', 'copyTask' , 'myUploadApkTask']){
    //    copyTask.mustRunAfter assembleRelease
    //    myUploadApkTask.mustRunAfter copyTask
    //}
    
    //Android Gradle plugin to version 2.2.0 and Gradle to version 2.14.1.
    task myBuild(){
        dependsOn 'assembleRelease'
        dependsOn 'copyTask'
        dependsOn 'myUploadApkTask'
    }
    
    dependencies {
         compile fileTree(dir: 'libs', include: ['*.jar'])
         testCompile 'junit:junit:4.12'
    }
    

    3.uploadApk.sh脚本(在主Module目录下)

    脚本内容:

    \#!/bin/sh
    \#MAIN_MODULE指AndroidStudio工程目录下面主module
    MAIN_MODULE="."
    \#蒲公英API账号
    
    PGYER_API_KEY="***"
    PGYER_USER_KEY="***"
    \#APK路径
    APK_PATH="${MAIN_MODULE}/build/outputs/apk"
    echo "current path: `pwd`"
    \#遍历apk,选出今天生成的apk,有多个的话,选出第一个
    TODAY=`date +%Y-%m-%d`
    echo "Today is $TODAY"
    for APK_FILE in ${APK_PATH}/*; do
        APK_NAME=`basename $APK_FILE`
        if [[ "$APK_NAME" =~ "$TODAY.apk" ]];then
            echo "Upload apk:$APK_NAME"
            break
        fi
    done
    \#curl上传至蒲公英,默认直接发布,不发布到广场
    curl -F "file=@${APK_PATH}/${APK_NAME}" -F "uKey=${PGYER_USER_KEY}" -F "_api_key=${PGYER_API_KEY}" http://www.pgyer.com/apiv1/app/upload
    

    更权限:

    chmod +x uploadApk.sh

    4.常见Error

    1)sh: ../uploadApk.sh: Permission denied

    增加执行权限chmod +x uploadApk.sh

    2)couldn't open file "app/build/outputs/apk/*"

    找不到apk文件,请确认uploadApk.sh 在工程目录下面主module

    5.链接

    1)蒲公英API接口
    2)Android studio的gradle配置

    相关文章

      网友评论

        本文标题:Android mac 环境下Gradle 自动打包上传到蒲公英

        本文链接:https://www.haomeiwen.com/subject/wqrcuttx.html