Gradle配置打包成不同的app

Gradle作为android studio 的自动构建工具,G是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。

Groovy是一种基于JVM(Java虚拟机)的敏捷开发语言,它结合了Python、Ruby和Smalltalk的许多强大的特性,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码。由于其运行在 JVM 上的特性,Groovy 可以使用其他 Java 语言编写的库。

最近在研究热修复和插件化的过程中, 原理明白,但是gradle的配置过程,自己云里雾里.
所以自己想研究一下gradle,构建工具.Gradle的强大功能,这里只用到了一角~

是啊,gradle 的学习曲线比较陡峭.自己啃了好久也没弄个所以然,大家会用就好.


这里推荐几篇博文,避免大家再走弯路,
1**.热修复nvwa的创建人贾吉鑫的博客**
http://www.csdn.net/article/2015-08-10/2825420/1

2.(需要翻墙,其实官方的才是最干货的!~),as 的官方技术指导
http://tools.android.com/tech-docs/new-build-system/user-guide

3.google搜索 (大家还是别用百度了,如果你用过google,你会感觉百度真心难用)


先抛砖引玉,从打包开始我们的gradle之旅~
要想实现同一个app程序打包成不同包名,只需要五步
1.在AndroidManifest.xml中的添加渠道包

 <application
        android:name=".base.BaseApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_lancher"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <!--多渠道打包-->
        <meta-data android:name="TD_CHANNEL_ID"
            android:value="${ONEAPM_TEST_CHANNEL}" />
    <!--其他-->         
 </application>

2.在module的build.gradle中的android{}中,添加如下代码:

 //产品渠道,这里一份源码有两个产品,
    productFlavors{
        //产品1,meeting(同多渠道打包)
        meeting{
            //重写meeting下的包名如下,当心百度地图等的使用
             applicationId"com.efly.meeting"
            //注册文件MainFest的渠道占位符,多渠道打包用
            manifestPlaceholders=[ONEAPM_TEST_CHANNEL: "meeting"]
           //生成res资源文件值,还可以通过添加文件夹的方式替换
           // resValue "string", "app_name", "青岛建管通"
        }

        report{
            applicationId"com.efly.meeting_report"
            manifestPlaceholders=[ONEAPM_TEST_CHANNEL: "report"]
            //resValue "string", "app_name", "工作汇报"
        }
    }

3.在module的build.gradle中的android{}中,添加如下代码:**

//发布前改名打包  遍历改名(grooxy语言)
    applicationVariants.all{ variant->
        variant.outputs.each { output->
            def oldFile = output.outputFile
            def newName = '';
            if(variant.buildType.name.equals('release')){
                println(variant.productFlavors[0].name)
                def releaseApkName = 'app-v' + defaultConfig.versionName + '-' + variant.productFlavors[0].name + '-release-signed.apk'
                output.outputFile = new File(oldFile.parent, releaseApkName)
            }

            if(variant.buildType.name.equals('debug')){
                newName = oldFile.name.replace(".apk", "-v" + defaultConfig.versionName + "-build" + realeaseTime() + ".apk")
                output.outputFile = new File(oldFile.parent, newName)
            }

        }
    }

4.在module的build.gradle中的android{}中,添加如下代码:**

//构建app的版本
    buildTypes {
        release {
            // minifyEnabled 混淆处理
            // shrinkResources 去除无用资源
          // signingConfig 签名
          // proguardFiles 混淆配置
          // applicationIdSuffix 增加APP ID的后缀
          // debuggable 是否保留调试信息
            //zipAlignEnabled true
            //minifyEnabled true
            //useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //signingConfig signingConfigs.release

        }
        debug {
            manifestPlaceholders = [app_label: "@string/app_name_report"]
        }
    }

5.根据渠道的不同添加不同的依赖库,在module的dependencies中

 //渠道不同,依赖库不同
    provided"io.reactivex:rxjava:1.1.0"
    flavor1Compile"io.reactivex:rxjava:1.1.0"

6.反编译查看包名是否被改

meeting反编译结果
meeting反编译结果
反编译结果
report反编译结果


关于gradle的构建,可以进行代码分dex进行插件化,热修复的开发,代码混淆,自建任务,自建仓库等操作,我会在后期的blog中,一一进行研究分享.
在这里大家如果有兴趣可以看下我的关于mvp架构的博客
http://blog.csdn.net/ccj659/article/details/51889713
~谢谢大家,thanks!

附build.gradle的代码:

apply plugin: 'com.android.application'


def realeaseTime(){
    return new Date().format("yyMMdd",TimeZone.getTimeZone("UTC"))
}

android {

   /* sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }*/

    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.efly.meeting"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0.7" //测试版本更新需要更改
       // resValue "string", "app_name", "青岛建管通"

    }
    //不安全
    /*signingConfigs {
        releaseConfig {
            keyAlias ' '
            keyPassword ' '
            storeFile file(' .jks')
            storePassword ' '
        }
    }*/

//发布前改名打包  遍历改名
    applicationVariants.all{ variant->
        variant.outputs.each { output->
            def oldFile = output.outputFile
            def newName = '';
            if(variant.buildType.name.equals('release')){
                println(variant.productFlavors[0].name)
                def releaseApkName = 'app-v' + defaultConfig.versionName + '-' + variant.productFlavors[0].name + '-release-signed.apk'
                output.outputFile = new File(oldFile.parent, releaseApkName)
            }

            if(variant.buildType.name.equals('debug')){
                newName = oldFile.name.replace(".apk", "-v" + defaultConfig.versionName + "-build" + realeaseTime() + ".apk")
                output.outputFile = new File(oldFile.parent, newName)
            }

        }
    }


//构建app的版本
    buildTypes {
        release {
            // minifyEnabled 混淆处理
            // shrinkResources 去除无用资源
          // signingConfig 签名
          // proguardFiles 混淆配置
          // applicationIdSuffix 增加APP ID的后缀
          // debuggable 是否保留调试信息
            //zipAlignEnabled true
            //minifyEnabled true
            //useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //signingConfig signingConfigs.release

        }
        debug {

            manifestPlaceholders = [app_label: "@string/app_name_report"]
        }


    }

    //产品渠道
    productFlavors{
        meeting{
            // applicationId"com.efly.meeting"
            manifestPlaceholders=[ONEAPM_TEST_CHANNEL: "meeting"]
           // resValue "string", "app_name", "青岛建管通"
        }
        report{
            //applicationId"com.efly.meeting_report"
            manifestPlaceholders=[ONEAPM_TEST_CHANNEL: "report"]
            //resValue "string", "app_name", "工作汇报"
        }
    }

}

dependencies {
    //渠道不同,依赖库不同
   /* provided"io.reactivex:rxjava:1.1.0"
    flavor1Compile"io.reactivex:rxjava:1.1.0"
*/


    compile fileTree(include: ['*.jar'], dir: 'libs')
    /*testCompile 'junit:junit:4.12'*/
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile project(':pulltorefresh')
    compile files('libs/locSDK_6.13.jar')
    //compile files('libs/json-lib-2.4-jdk15.jar')
    compile files('libs/jmessage-android-1.2.5.jar')
    /* compile files('libs/jmessage-android-1.2.0.jar')*/
    compile files('libs/eventbus.jar')
    // compile 'io.reactivex:rxandroid:1.1.0'
    // compile 'io.reactivex:rxjava:1.1.0'
    // compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'im.fir:fir-sdk:latest.integration@aar'
    compile 'com.android.support:design:23.3.0'
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值