一、Build.gradle的配置
在工程的根build.gradle文件中添加
classpath 'cn.jiajixin.nuwa:gradle:1.2.2'
然后我们的build.gradle文件可能长这样:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'cn.jiajixin.nuwa:gradle:1.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在uliaobao的build.gradle中添加依赖:
apply plugin: "cn.jiajixin.nuwa"
nuwa {
//括号里是Application类以及要排除Application父类
excludeClass = ['comcom/demo//Bean/AllClassif/yBean.class'
,'com/demo/Bean/ReceiverBean.class'
,'com/umeng/socialize/PlatformConfig.class'
,'com/umeng/socialize/bean/SHARE_MEDIA.class'
,'com/umeng/socialize/PlatformConfig$QQZone.class'
,'com/umeng/socialize/PlatformConfig$Weixin.class'
,'com/umeng/socialize/PlatformConfig$Douban.class'
,'com/umeng/socialize/PlatformConfig$Laiwang.class'
,'com/umeng/socialize/PlatformConfig$Yixin.class'
,'com/umeng/socialize/PlatformConfig$SinaWeibo.class'
,'com/umeng/socialize/PlatformConfig$Alipay.class'
,'com/umeng/socialize/PlatformConfig$Renren.class'
,'com/umeng/socialize/PlatformConfig$Googleplus.class'
,'com/umeng/socialize/PlatformConfig$Facebook.class'
,'com/umeng/socialize/PlatformConfig$Twitter.class'
,'com/umeng/socialize/PlatformConfig$Tumblr.class'
,'com/umeng/socialize/PlatformConfig$Plnterest.class'
,'com/umeng/socialize/PlatformConfig$Pocket.class'
,'com/umeng/socialize/PlatformConfig$Whatsapp.class'
,'com/umeng/socialize/PlatformConfig$Email.class'
,'com/umeng/socialize/PlatformConfig$Sms.class'
,'com/umeng/socialize/PlatformConfig$Linkedin.class'
,'com/umeng/socialize/PlatformConfig$Line.class'
,'com/umeng/socialize/PlatformConfig$Flickr.class'
,'com/umeng/socialize/PlatformConfig$Evernote.class'
,'com/umeng/socialize/PlatformConfig$Foursquare.class'
,'com/umeng/socialize/PlatformConfig$Ynote.class'
,'com/umeng/socialize/PlatformConfig$Kakao.class'
,'com/umeng/socialize/PlatformConfig$Instagram.class'
,'com/umeng/socialize/PlatformConfig$GooglePlus.class'
,'com/umeng/socialize/PlatformConfig$CustomPlatform.class'
,'com/umeng/socialize/PlatformConfig$Pinterest.class'
]
}
然后我们的build.gradle文件可能长这样:
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':pulltorefresh')
compile 'com.android.support:recyclerview-v7:21.0.0'
compile files('libs/okhttp-2.7.0.jar')
compile files('libs/jpush-android-2.1.5.jar')
compile files('libs/gson-1.6.jar')
compile files('libs/aseo2.jar')
compile files('libs/robotium-solo-5.2.1.jar')
compile 'cn.jiajixin.nuwa:nuwa:1.0.0'
}
【备注】 这边有个坑,2.0版本的Android Studio为了支持Instant Run, 默认使用的Gradle Plugin是2.0.0-alpha2, 如果用此版本,会报错Error:Cannot get property ‘taskDependencies’ on null object,乖乖降低到1.2.3,木有此问题。
二、使用Nuwa
1.自定义Application,并且在Application类中添加如下代码:
@Override
public void onCreate() {
super.onCreate();
Nuwa.init(this);
Nuwa.loadPatch(this, Environment.getExternalStorageDirectory().getAbsolutePath().concat("/patch.jar"));
}
2.运行并安装apk,会在 项目名称/build/outputs目录下自动生成一个叫nuwa的目录:
拷贝此目录到一个指定的目录,比如:
D:/nuwa
此apk目前是有bug的apk, 然后我们修复bug, 修复完毕后进入步骤3
3.在修复完bug的c ode base下执行如下命令:
gradlew clean nuwa(打包的渠道名)DebugPatch -P NuwaDir=D:/nuwa(拷贝后的nuwa的路径)
gradlew clean nuwa BaiduSEM DebugPatch -P NuwaDir=D:/nuwa
注意:如果执行命令后出现下面的错误:
解决方式:配置AS的sdk的路径,如下图:
配置完后,再执行步骤3,即可。
然后在app/build/outputs/nuwa/debug/目录下会自动生成patch.jar文件:
此文件就是我们热部署的补丁文件
4.我们把补丁文件拷贝到sdcard根目录(这边只是为了测试整个hotfix流程, 自己的项目肯定是放在服务器上,我们通过代码自己去下载和存储)
5.代码混淆
———– Begin: 热补丁———-
-keep class cn.jiajixin.nuwa.* { ; }