Gradle多渠道打包[umeng]

随声附和 提交于 2019-12-19 02:21:00

前言

国内Android应用市场品种太多,血统不纯,每次上线App都打包打到手软,上传上到吐血!好在我们有了Android studio和gradle,一条命令打包各种渠道版本App!

正文

Umeng多渠道

我们在项目中会使用统计SDK,用的最流行的当属umeng了吧。这里我们就用友盟来说明。
用了友盟的App的清单文件AndroidManifest.xml中都会写上meta-data,来区分不同的渠道:

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

1.将Channel_ID更改为${UMENG_CHANNEL_VALUE},如下:

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

2.在appbuild.gradle设置produceFlavors

  productFlavors {
    xiaomi {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
    }
    qihoo {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihoo"]
    }
    baidu {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
    }
    wandoujia {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
    }
    qq {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq"]
    }
  }

3.来看看我在项目中使用的build.gradle文件内容:

apply plugin: 'com.android.application'

def releaseTime() {
  return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.0"

  defaultConfig {
    // 这里的applicationId根据自己的替换
    applicationId "com.sunjiajia.xxxxx"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "0.0.1"
    multiDexEnabled true //dex突破65535限制
    manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
  }
  lintOptions {
    disable 'MissingTranslation', 'ExtraTranslation'
    abortOnError false
  }

  signingConfigs {
    // /home/monkey/Projects/AndroidStudioProjects/debug.jks是签名文件所在全路径

    // debug时打包用的签名
    debug {
      keyAlias 'debug'
      keyPassword '123456'
      storeFile file('/home/monkey/Projects/AndroidStudioProjects/debug.jks')
      storePassword '123456'
    }

     // release时打包用的签名
    release {
      keyAlias 'monkey'
      keyPassword '123123'
      storeFile file('/home/monkey/Projects/AndroidStudioProjects/monkey.jks')
      storePassword '123123'
    }
  }

  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名称为appname_v1.0_2015-09-15_09-57-58_wandoujia.apk
            // 下面的appname改为你自己的app name
            def fileName = "appname_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
            output.outputFile = new File(outputFile.parent, fileName)
          }
        }
      }
    }
  }

  packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
  }

  // 友盟多渠道打包
  productFlavors {
    xiaomi {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
    }
    qihoo {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qihoo"]
    }
    baidu {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
    }
    wandoujia {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
    }
    qq {
      manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq"]
    }
  }
}

dependencies {
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.0.1'
  compile 'com.android.support:design:23.0.1'
}

命令创建task

1.进入项目的根目录:

// 打包全部Debug版本
./gradlew assembleDebug
// 打包全部Release版本
./gradlew assembleRelease

2.assembleProduct Flavor 结合创建指定渠道的任务版本。例如:

// 打包wandoujia渠道的Release版本和Debug版本
./gradlew assembleWandoujia
// 打包wandoujia渠道的Release版本
./gradlew assembleWandoujiaRelease
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!