Android uses Gradle for multi-channel Packaging

Source: Internet
Author: User

Android uses Gradle for multi-channel Packaging

Android development is complete. For an open application, we need to publish it to different application markets. At the same time, we also need to count the downloads of users in different markets. (Obtain different values of apk in different markets after starting the application to differentiate them)

Here is a specific example:
1. Add the meta-data tag to the application of AndroidManifest. xml.

    
              
                           
            
    
       
  

2. Modify the build. gradle file and add it to android {}.

Android {// package channel List productFlavors {wandoujia {channels = [APP_CHANNEL_VALUE: "pods"]} cn360 {channels = [APP_CHANNEL_VALUE: "360"]} baidu {channels = [APP_CHANNEL_VALUE: "Baidu"]} tencent {manifestPlaceholders = [APP_CHANNEL_VALUE: ""]} sougou {manifestPlaceholders = [APP_CHANNEL_VALUE: "sogou market"]}

Or use the following method to directly use the name of flavor as $ {APP_CHANNEL_VALUE}

Android {// package channel List productFlavors {wandoujia {} cn360 {} baidu {} tencent {} sougou {} // batch processing, directly use the name of flavor as the value of APP_CHANNEL_VALUE productFlavors. all {flavor-> flavor. manifestPlaceholders = [APP_CHANNEL_VALUE: name]}

This completes. Execute gradle assumerelease to have a cup of tea and wait.
After that, you can view the channel packages in build/outputs/apk.

Available commands for assemble include:

Gradle assembleDebug // All Debug Versions gradle javaserelease // All Release versions gradle assembleBaidu // The Debug and Release versions of the specified channel gradle assembleBaiduDebug // The Debug version of the specified channel gradle Release // The specified channel the Release version of gradle build // Debug and Release versions of all channels

Add my MainActivity and build. gradle.

Package com. example. myandroid; import android. app. activity; import android. content. pm. applicationInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. OS. bundle; import android. widget. toast;/*** application portal ** @ author SHANHY ([email protected]) * @ date December 30, 2015 */public class MainActivity extends Activity {@ Override protected void onC Reate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String channel = getAppMetaData ("APP_CHANNEL"); if (channel! = Null) Toast. makeText (this, channel, Toast. LENGTH_SHORT ). show ();}/*** get metaData under the Application ** @ param name * @ return * @ author SHANHY * @ date January 1, December 30, 2015 */public String getAppMetaData (String meta_name) {try {ApplicationInfo appInfo = this. getPackageManager (). getApplicationInfo (getPackageName (), PackageManager. GET_META_DATA); return appInfo. metaData. getString (meta_name);} catch (NameNotFoundException e) {e. printStackTrace ();} return null ;}}

Complete build. gradle script

Buildscript {repositories {jcenter ()} dependencies {classpath 'com. android. tools. build: gradle: 2.0.0-alpha3 '} apply plugin: 'com. android. application 'dependencies {compile fileTree (dir: 'libs', include :'*. jar ')} android {compileSdkVersion 14 buildToolsVersion "23.0.2" defaultConfig {// You can manually modify the following configurations to generate the corresponding apk applicationId "com. example. myandroid. aaa "minSdkVersion 8 targetSdkVersion 21 versionCode 200 versionName" 2.0.0 "// dex exceeds 65535 limit multiDexEnabled true // default packaging channel (official) manifestPlaceholders = [APP_CHANNEL_VALUE: "official"]} // List of packaging channels productFlavors {myself {manifestPlaceholders = [APP_CHANNEL_VALUE: "official"]} wandoujia {manifestPlaceholders = [APP_CHANNEL_VALUE: "pea pod"]} cn360 {manifestPlaceholders = [APP_CHANNEL_VALUE: "360"]} baidu {token = [APP_CHANNEL_VALUE: "baidu"]} tencent {manifestPlaceholders = [APP_CHANNEL_VALUE: "app Bao"]} sougou {manifestPlaceholders = [APP_CHANNEL_VALUE: "sogou market"] }}// package channel List // productFlavors {// myself {}// wandoujia {}// cn360 {}// baidu {}// tencent {} // sougou {}/// batch processing, directly use the name of flavor as the value of APP_CHANNEL_VALUE (you can also configure it one by one in productFlavors without using this method) // productFlavors. all {flavor-> // flavor. manifestPlaceholders = [APP_CHANNEL_VALUE: name] //} lintOptions {abortOnError false} sourceSets {main {manifest. srcFile 'androidmanifest. xml 'java. srcDirs = ['src'] resources. srcDirs = ['src'] aidl. srcDirs = ['src'] renderscript. srcDirs = ['src'] res. srcDirs = ['res'] assets. srcDirs = ['assets']} // Move the tests to tests/java, tests/res, etc... instrumentTest. setRoot ('tests') // Move the build types to build-types/
  
   
// For instance, build-types/debug/java, build-types/debug/AndroidManifest. xml,... // This moves them out of them default location under src/
   
    
/... Which wocould // conflict with src/being used by the main source set. // Adding new build types or product flavors shoshould be accompanied // by a similar customization. debug. setRoot ('Build-types/debug') release. setRoot ('Build-types/release')} // signature information signingConfigs {debug {// No Debug Config} release {storeFile file ("xxxxx. key ") storePassword" xxxxx "keyAlias" xxxxx "keyPassword" xxxxx "}} BuildTypes {// Debug mode debug {// display LOG buildConfigField "boolean", "LOG_DEBUG ", "true" versionNameSuffix "-debug" // do not enable obfuscation minifyEnabled false // do not need ZIP optimization zipAlignEnabled false // do not need resource compression shrinkResources false/signingConfig signingConfigs. debug} // Release mode release {// do not display LOG buildConfigField "boolean", "LOG_DEBUG", "true" minifyEnabled true zipAlignEnabled true // resource compression, Remove useless Resources File shrinkResources true // configure proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' // configure the signature information (if defaultConfig is configured above, you do not need to specify signingConfig) signingConfig signingConfigs. release applicationVariants. all {variant-> variant. outputs. each {output-> def outputFile = output. outputFile if (outputFile! = Null & outputFile.name.endsWith('.apk ') {// The output apkname is myandroid_v1.0.0_2015-12-30_baidu.apk def fileName = "plugin" output. outputFile = new File (outputFile. parent, fileName) }}}}// declare a method to obtain the package time def releaseTime () {return new Date (). format ("yyyy-MM-dd", TimeZone. getTimeZone ("UTC "))}
   
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.