├── .gitignore ├── README.md ├── android_release.gradle ├── build.gradle ├── buildSrc ├── .gitignore ├── build.gradle ├── gradle.properties └── src │ └── main │ ├── groovy │ └── com │ │ └── lizhangqu │ │ └── multichannel │ │ ├── MultiChannelExtension.groovy │ │ ├── MultiChannelPlugin.groovy │ │ └── TimeListener.groovy │ └── resources │ └── META-INF │ └── gradle-plugins │ └── com.lizhangqu.multichannel.properties ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── sign │ └── release.jks └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── lizhangqu │ │ └── sample │ │ ├── App.java │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/libraries 4 | /.idea 5 | .DS_Store 6 | build 7 | gen 8 | /captures 9 | repo 10 | *.iml 11 | bak -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 多渠道打包Gradle插件 2 | 3 | 配置渠道 4 | 5 | ``` 6 | apply plugin: 'com.lizhangqu.multichannel' 7 | multiChannel { 8 | channels = ["baidu", "qihu", "google", "huawei", "xiaomi", "wandoujia", "anzhi", "uc", "tencent", "wangyi", "youmeng"] 9 | } 10 | ``` 11 | 12 | 之后会往META-INF/channel.data下写入渠道信息 13 | 14 | 开启v2签名后会扔异常!!禁用即可 15 | 16 | 生成渠道包 17 | 18 | ``` 19 | gradle clean multiChannelDebug 20 | gradle clean multiChannelRelease 21 | ``` 22 | 23 | 产物位于 24 | ${project}/build/outputs/apk/channel下 -------------------------------------------------------------------------------- /android_release.gradle: -------------------------------------------------------------------------------- 1 | // ./gradlew clean build generateRelease 2 | apply plugin: 'maven' 3 | 4 | def groupId = project.PUBLISH_GROUP_ID 5 | def artifactId = project.PUBLISH_ARTIFACT_ID 6 | def version = project.PUBLISH_VERSION 7 | 8 | def localReleaseDest = "${buildDir}/release/${version}" 9 | 10 | 11 | uploadArchives { 12 | repositories.mavenDeployer { 13 | pom.groupId = groupId 14 | pom.artifactId = artifactId 15 | pom.version = version 16 | repository(url: mavenDeployReleaseUrl) { 17 | authentication(userName: mavenReleaseUser, password:mavenReleasePassword) 18 | } 19 | snapshotRepository(url: mavenDeployUrl) { 20 | authentication(userName: mavenUser, password:mavenPassword) 21 | } 22 | } 23 | } 24 | 25 | task zipRelease(type: Zip) { 26 | from localReleaseDest 27 | destinationDir buildDir 28 | archiveName "release-${version}.zip" 29 | } 30 | 31 | task generateRelease << { 32 | println "Release ${version} can be found at ${localReleaseDest}/" 33 | println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip" 34 | } 35 | 36 | generateRelease.dependsOn(uploadArchives) 37 | generateRelease.dependsOn(zipRelease) 38 | 39 | artifacts { 40 | //archives androidSourcesJar 41 | //archives androidJavadocsJar 42 | } 43 | 44 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:2.2.3' 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | jcenter() 16 | mavenCentral() 17 | } 18 | } 19 | 20 | task clean(type: Delete) { 21 | delete rootProject.buildDir 22 | } 23 | configurations.all { 24 | // check for updates every build 25 | resolutionStrategy.cacheChangingModulesFor 0, 'seconds' 26 | } -------------------------------------------------------------------------------- /buildSrc/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | /build 3 | *.iml -------------------------------------------------------------------------------- /buildSrc/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'groovy' 2 | apply plugin: 'idea' 3 | apply plugin: 'maven' 4 | sourceCompatibility = 1.6 5 | repositories { 6 | jcenter() 7 | mavenCentral() 8 | } 9 | configurations { 10 | provided 11 | } 12 | 13 | idea { 14 | module { 15 | scopes.PROVIDED.plus += [configurations.provided] 16 | } 17 | } 18 | 19 | sourceSets { 20 | main { 21 | compileClasspath += configurations.provided 22 | } 23 | } 24 | 25 | dependencies { 26 | compile gradleApi() 27 | compile localGroovy() 28 | compile "commons-io:commons-io:1.4" 29 | compile 'commons-codec:commons-codec:1.6' 30 | compile "com.android.tools.build:gradle:2.2.3" 31 | } 32 | 33 | //ext { 34 | // PUBLISH_GROUP_ID = 'com.lizhangqu.multichannel' 35 | // PUBLISH_ARTIFACT_ID = 'multichannel' 36 | // PUBLISH_VERSION = '0.0.1-SNAPSHOT' 37 | //} 38 | // 39 | //apply from: '../android_release.gradle' 40 | -------------------------------------------------------------------------------- /buildSrc/gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Thu Dec 03 11:07:19 CST 2015 16 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/com/lizhangqu/multichannel/MultiChannelExtension.groovy: -------------------------------------------------------------------------------- 1 | package com.lizhangqu.multichannel 2 | 3 | import org.gradle.api.Project 4 | 5 | class MultiChannelExtension { 6 | public static final PLUGIN_EXTENTION = "multiChannel" 7 | HashSet channels = [] 8 | MultiChannelExtension(Project project) { 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/com/lizhangqu/multichannel/MultiChannelPlugin.groovy: -------------------------------------------------------------------------------- 1 | package com.lizhangqu.multichannel 2 | 3 | import com.android.build.gradle.internal.dsl.SigningConfig 4 | import com.android.builder.core.AndroidBuilder 5 | import com.android.sdklib.BuildToolInfo 6 | import org.apache.commons.io.FileUtils 7 | import org.gradle.api.Action 8 | import org.gradle.api.Plugin 9 | import org.gradle.api.Project 10 | import org.gradle.api.logging.Logger 11 | import org.gradle.process.ExecSpec 12 | 13 | /** 14 | * 多渠道打包插件 15 | * author:lizhangqu(区长) 16 | * 17 | */ 18 | class MultiChannelPlugin implements Plugin { 19 | HashSet channels = [] 20 | private Logger logger 21 | 22 | @Override 23 | void apply(Project project) { 24 | project.gradle.addListener(new TimeListener()) 25 | logger = project.logger; 26 | project.extensions.create(MultiChannelExtension.PLUGIN_EXTENTION, MultiChannelExtension, project) 27 | project.afterEvaluate { 28 | def extension = project.extensions.findByName(MultiChannelExtension.PLUGIN_EXTENTION) as MultiChannelExtension 29 | extension.channels.each { 30 | channel -> 31 | channels.add(channel) 32 | } 33 | if (channels == null || channels.size() <= 0) { 34 | return 35 | } 36 | project.android.applicationVariants.each { variant -> 37 | //got variantName 38 | def variantName = variant.name.capitalize() 39 | 40 | AndroidBuilder androidBuilder = variant.androidBuilder 41 | def mTargetInfo = androidBuilder.mTargetInfo 42 | def mBuildToolInfo = mTargetInfo.mBuildToolInfo 43 | Map mPaths = mBuildToolInfo.mPaths 44 | def aaptPath = mPaths.get(BuildToolInfo.PathId.AAPT) 45 | //got aapt file 46 | def aaptFile = new File(aaptPath); 47 | 48 | //got assemble Task 49 | def assembleTask = variant.getAssemble(); 50 | // def assembleTask = project.tasks.findByName("assemble${variantName}") 51 | 52 | //got package Task 53 | def packageTask = project.tasks.findByName("package${variantName}") 54 | //got signed apk 55 | File outApk = packageTask.outputFile 56 | //got sign config 57 | SigningConfig signingConfig = packageTask.signingConfig; 58 | 59 | //got v2 sign ..... 60 | def v2Enabled = false 61 | try { 62 | v2Enabled = signingConfig.isV2SigningEnabled(); 63 | } catch (Throwable e) { 64 | e.printStackTrace() 65 | v2Enabled = false 66 | } 67 | if (v2Enabled) { 68 | throw new RuntimeException("please disable v2SigningEnabled.\n" + "signingConfigs {\n" + 69 | " ${variant.name} {\n" + 70 | " v2SigningEnabled false\n" + 71 | " }\n" + 72 | "}" + "\n see:https://developer.android.com/about/versions/nougat/android-7.0.html#apk_signature_v2") 73 | } 74 | String multiChannelName = "multiChannel${variantName}" 75 | project.task(multiChannelName) { 76 | doLast { 77 | def channelDataFileName = "META-INF/channel.data" 78 | //1. generate an empty channel.data 79 | File operateFile = new File(outApk.getParentFile(), "channelOrignal.apk"); 80 | FileUtils.copyFile(outApk, operateFile) 81 | 82 | File channelDataFile = new File(outApk.parentFile, channelDataFileName) 83 | FileUtils.deleteQuietly(channelDataFile) 84 | FileUtils.writeStringToFile(channelDataFile, ""); 85 | try { 86 | //2. add an empty channel.data to apk 87 | project.exec(new Action() { 88 | @Override 89 | public void execute(ExecSpec execSpec) { 90 | execSpec.workingDir(operateFile.getParent()) 91 | execSpec.executable(aaptFile); 92 | execSpec.args("add", operateFile.getName(), channelDataFileName); 93 | } 94 | }); 95 | } catch (Throwable throwable) { 96 | throwable.printStackTrace() 97 | //ignore,maybe META-INF/channel.data exist 98 | } 99 | 100 | //iterate all channels, and exec aapt remove and add 101 | channels.each { 102 | channel -> 103 | if (channel == null || channel.length() <= 0) { 104 | return 105 | } 106 | logger.warn("current channel:${channel}") 107 | //write channel 108 | FileUtils.deleteQuietly(channelDataFile) 109 | FileUtils.writeStringToFile(channelDataFile, channel); 110 | 111 | //remove 112 | project.exec(new Action() { 113 | @Override 114 | public void execute(ExecSpec execSpec) { 115 | execSpec.workingDir(operateFile.getParent()) 116 | execSpec.executable(aaptFile); 117 | execSpec.args("remove", operateFile.getName(), channelDataFileName); 118 | } 119 | }); 120 | //add 121 | project.exec(new Action() { 122 | @Override 123 | public void execute(ExecSpec execSpec) { 124 | execSpec.workingDir(operateFile.getParent()) 125 | execSpec.executable(aaptFile); 126 | execSpec.args("add", operateFile.getName(), channelDataFileName); 127 | } 128 | }); 129 | 130 | //sign 131 | // File channelResignFile = new File("${operateFile.parent}/channel", "resign_${channel}.apk") 132 | // FileUtils.forceMkdir(channelResignFile.getParentFile()) 133 | // androidBuilder.signApk(operateFile, signingConfig, channelResignFile) 134 | //copy 135 | File channelFile = new File("${operateFile.parent}/channel", "${project.name}-${variant.name}-${channel}.apk") 136 | FileUtils.copyFile(operateFile, channelFile) 137 | } 138 | 139 | //delete 140 | FileUtils.deleteQuietly(operateFile) 141 | FileUtils.deleteQuietly(channelDataFile) 142 | FileUtils.deleteDirectory(channelDataFile.getParentFile()) 143 | 144 | } 145 | } 146 | 147 | //task 148 | def multiChannelTask = project.tasks[multiChannelName] 149 | multiChannelTask.setGroup("multiChannel") 150 | multiChannelTask.setDescription("多渠道打包task") 151 | 152 | //depend on package Task 153 | if (packageTask) { 154 | multiChannelTask.dependsOn(packageTask) 155 | } 156 | 157 | 158 | } 159 | 160 | } 161 | } 162 | } 163 | 164 | 165 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/com/lizhangqu/multichannel/TimeListener.groovy: -------------------------------------------------------------------------------- 1 | package com.lizhangqu.multichannel 2 | 3 | import org.gradle.BuildListener 4 | import org.gradle.BuildResult 5 | import org.gradle.api.Task 6 | import org.gradle.api.execution.TaskExecutionListener 7 | import org.gradle.api.initialization.Settings 8 | import org.gradle.api.invocation.Gradle 9 | import org.gradle.api.tasks.TaskState 10 | import org.gradle.util.Clock 11 | 12 | class TimeListener implements TaskExecutionListener, BuildListener { 13 | private Clock clock 14 | private times = [] 15 | 16 | @Override 17 | void beforeExecute(Task task) { 18 | clock = new Clock() 19 | } 20 | 21 | @Override 22 | void afterExecute(Task task, TaskState taskState) { 23 | def ms = clock.timeInMs 24 | times.add([ms, task.path]) 25 | } 26 | 27 | @Override 28 | void buildFinished(BuildResult result) { 29 | println "Task spend time:" 30 | for (time in times) { 31 | printf "%7sms %s\n", time 32 | } 33 | } 34 | 35 | @Override 36 | void buildStarted(Gradle gradle) { 37 | 38 | } 39 | 40 | @Override 41 | void projectsEvaluated(Gradle gradle) { 42 | 43 | } 44 | 45 | @Override 46 | void projectsLoaded(Gradle gradle) { 47 | 48 | } 49 | 50 | @Override 51 | void settingsEvaluated(Settings settings) { 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /buildSrc/src/main/resources/META-INF/gradle-plugins/com.lizhangqu.multichannel.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.lizhangqu.multichannel.MultiChannelPlugin -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Thu Dec 03 11:07:19 CST 2015 16 | 17 | 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizhangqu/MultiChannel/967372f177ed94229684cbfbba9d32996919aa5e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Mar 09 17:13:55 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | *.iml -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "25.0.1" 6 | signingConfigs { 7 | release { 8 | keyAlias 'lizhangqu' 9 | keyPassword 'lizhangqu' 10 | storeFile file('./sign/release.jks') 11 | storePassword 'lizhangqu' 12 | v2SigningEnabled false 13 | } 14 | } 15 | defaultConfig { 16 | applicationId "com.lizhangqu.sample" 17 | minSdkVersion 14 18 | targetSdkVersion 23 19 | versionCode 1 20 | versionName "1.0" 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled true 25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 26 | signingConfig signingConfigs.release 27 | } 28 | 29 | debug { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 32 | signingConfig signingConfigs.release 33 | } 34 | } 35 | 36 | dexOptions { 37 | preDexLibraries true 38 | threadCount 4 39 | javaMaxHeapSize '2g' 40 | } 41 | 42 | } 43 | 44 | dependencies { 45 | compile fileTree(dir: 'libs', include: ['*.jar']) 46 | compile 'com.android.support:appcompat-v7:23.1.0' 47 | 48 | } 49 | 50 | apply plugin: 'com.lizhangqu.multichannel' 51 | multiChannel { 52 | channels = ["baidu", "qihu", "google", "huawei", "xiaomi", "wandoujia", "anzhi", "uc", "tencent", "wangyi", "youmeng"] 53 | } -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | -optimizationpasses 5 2 | -dontusemixedcaseclassnames 3 | -dontskipnonpubliclibraryclasses 4 | -dontpreverify 5 | -verbose 6 | -dontoptimize 7 | -dontwarn 8 | -dontskipnonpubliclibraryclassmembers 9 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 10 | 11 | 12 | -keep public class * extends android.app.Activity 13 | -keep public class * extends android.app.Application 14 | -keep public class * extends android.app.Service 15 | -keep public class * extends android.content.BroadcastReceiver 16 | -keep public class * extends android.content.ContentProvider 17 | -keep public class * extends android.app.backup.BackupAgentHelper 18 | -keep public class * extends android.preference.Preference 19 | -keep public class com.android.vending.licensing.ILicensingService 20 | -keep public class * extends android.widget.LinearLayout 21 | -keep public class * extends android.widget.TextView 22 | -keep public class * extends android.widget.GridView 23 | -keep public class * extends android.support.v4.app.Fragment 24 | -keep public class * extends android.widget.ListView 25 | -keep public class * extends android.widget.RelativeLayout 26 | -keep public class * extends android.widget.FrameLayout 27 | 28 | 29 | -keep class **.R$* { *; } 30 | -keepclassmembers class * {public (org.json.JSONObject);} 31 | 32 | -keepattributes SourceFile,LineNumberTable 33 | 34 | 35 | -keepclassmembers class * extends android.content.Context { 36 | public void *(android.view.View); 37 | public void *(android.view.MenuItem); 38 | } 39 | 40 | 41 | -keepclassmembers class * { 42 | public (org.json.JSONObject); 43 | } 44 | 45 | -keepclasseswithmembers class * { 46 | native ; 47 | } 48 | 49 | -keepclasseswithmembers class * { 50 | public (android.content.Context, android.util.AttributeSet); 51 | } 52 | 53 | 54 | -keepclasseswithmembers class * { 55 | public (android.content.Context, android.util.AttributeSet, int); 56 | } 57 | 58 | -keepclassmembers enum * { 59 | public static **[] values(); 60 | public static ** valueOf(java.lang.String); 61 | } 62 | 63 | -keep class * implements android.os.Parcelable { 64 | public static final android.os.Parcelable$Creator *; 65 | } 66 | 67 | 68 | -assumenosideeffects class android.util.Log { *; } 69 | 70 | -------------------------------------------------------------------------------- /sample/sign/release.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizhangqu/MultiChannel/967372f177ed94229684cbfbba9d32996919aa5e/sample/sign/release.jks -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /sample/src/main/java/com/lizhangqu/sample/App.java: -------------------------------------------------------------------------------- 1 | package com.lizhangqu.sample; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | import android.util.Log; 6 | 7 | 8 | 9 | public class App extends Application { 10 | 11 | @Override 12 | protected void attachBaseContext(Context base) { 13 | super.attachBaseContext(base); 14 | Log.e("TAG", "real attachBaseContext"); 15 | 16 | } 17 | 18 | @Override 19 | public void onCreate() { 20 | super.onCreate(); 21 | Log.e("TAG", "real onCreate"); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/src/main/java/com/lizhangqu/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.lizhangqu.sample; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.Toast; 10 | 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.InputStream; 13 | import java.util.jar.JarFile; 14 | import java.util.zip.ZipEntry; 15 | 16 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 17 | private Button mButton; 18 | private String mCacheChannel = null; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | mButton = (Button) findViewById(R.id.channel); 25 | mButton.setOnClickListener(this); 26 | 27 | } 28 | 29 | @Override 30 | public void onClick(View v) { 31 | if (v.getId() == R.id.channel) { 32 | if (mCacheChannel == null) { 33 | mCacheChannel = getChannel(this); 34 | } 35 | Toast.makeText(this, "channel:" + mCacheChannel, Toast.LENGTH_LONG).show(); 36 | } 37 | } 38 | 39 | public static final String DEFAULT = "default"; 40 | 41 | public static String getChannel(Context context) { 42 | if (context == null) { 43 | return null; 44 | } 45 | ByteArrayOutputStream bos = null; 46 | InputStream inputStream = null; 47 | JarFile jarFile = null; 48 | try { 49 | jarFile = new JarFile(context.getPackageCodePath()); 50 | ZipEntry entry = jarFile.getEntry("META-INF/channel.data"); 51 | if (entry == null) { 52 | return DEFAULT; 53 | } 54 | inputStream = jarFile.getInputStream(entry); 55 | 56 | if (inputStream == null) { 57 | return DEFAULT; 58 | } 59 | 60 | bos = new ByteArrayOutputStream(); 61 | byte[] data = new byte[1024]; 62 | int read = -1; 63 | 64 | while ((read = inputStream.read(data)) != -1) { 65 | bos.write(data, 0, read); 66 | } 67 | 68 | String channel = new String(bos.toByteArray(), "UTF-8"); 69 | return channel; 70 | } catch (Exception e) { 71 | e.printStackTrace(); 72 | } finally { 73 | try { 74 | if (inputStream != null) { 75 | inputStream.close(); 76 | } 77 | if (bos != null) { 78 | bos.close(); 79 | } 80 | if (jarFile != null) { 81 | jarFile.close(); 82 | } 83 | } catch (Exception e) { 84 | e.printStackTrace(); 85 | } 86 | } 87 | return DEFAULT; 88 | } 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 |