Xposed也要热更新

好久没写博客了。这次玩一点不一样的。

吐槽&起因

相信熟悉Xposed的小伙伴们都知道,每次写完Xposed都要重启啊!有木有!反射错了,写错了名字,改一个log,都要重启啊有木有!重启浪费时间啊有木有!一个字母导致一次重启!要命啊有木有!

所以就花时间想了想有木有不重启就立即生效的办法呢。

Xposed每次都需要重启才能生效,这里我并不知道具体原因,但是大题可以猜测为烧入系统的代码,所以如果代码有变动,必须经过重启。那么有没有只烧一次,就可以动态变动的方法呢?

原理

参考android里classloader实现,发现findClass()最后都是调用了DexFile来loadClass,那我们也可以偷懒,不包裹classloader,直接拿DexFile来动态加载我们的代码呀。

栗子

首先,准备hook一下我的zoomheader,在主界面onCreate()的时候,让他弹出一个Toast. (其实做什么都可以
,这里只是方便演示)

此时,打开我的app,就可以弹出“哈哈”这个Toast

可是,这时候我发现“哈哈”其实写错了,我想写成”呵呵”,这时候我不得不修改“哈哈”为“呵呵”,然后重启,为了这一个字等上个三五六分钟。。

代码热加载,热更新

所以,这个时候,我们把需要写的逻辑代码单独放到一个dex里,然后使用DexFile加载,这时候虽然只烧了一次代码,但是这个烧了以后的代码可以根据动态的dex的代码来进行逻辑变化。

原理很简单,代码也很简单,只需要几句话。

这里为了展示原理,写了最简单的,这里去读取/sdcard/classes.dex文件,直接导入Hotfix类,调用他的invoke方法,由于弹出toast需要用到activity参数,所以把这个Activity传入即可。

HotFix代码如图:把Toast逻辑写在了里面。

这个时候,只要把hotfix的apk打包,然后提取dex,通过adb push到sdcard, 就可以实现热更新了!

自动化热更新

不过这样未免还是有些麻烦。所以当然要用gradle帮我们了。

这里要感谢下 @小小张 @神来一巴掌 @pighead

观察application插件,有个叫做transformClassesWithDex的任务,他就是把class文件转化为dex的任务,这个时候,我们去hook这个任务,在他执行完毕以后,自动把dex push到/sdcard/中。

写一个任务,叫做pushDex ,并且依赖于transformClassesWithDex。

此时,只要修改完HotFix.java的代码,然后执行Task pushDex,代码就自动热更新完毕!

判断adb路径代码 使用了AppMethodOrder的代码,感谢原作者 @三斤-虹猫

项目示例代码已经上传至https://github.com/githubwing/HotXposed,觉得有用,能节省时间的话,欢迎star哈。

欢迎加入Android地方酒馆:425983695

20 thoughts on “Xposed也要热更新

  1. Sorry, you must have the wrong number per restarne molto colpiti vendita viagra in ch “I’ll always aim to make my recipes work in whatever the kitchen, but eventually I would like a kitchen with four gas rings, a proper oven and a dishwasher. The first thing I bought with my book advance was a mini-washing machine – taking my clothes to the launderette was such a pain. And my flat in London has a bed! After eight years rolling out a futon, I can now just climb into bed every night. It’s a little thing most people take for granted, but I feel like I’ve arrived.”

  2. Sorry, you must have the wrong number http://xxxnx.fun/ vlxx He hasn’t played a down of football since the 2011 preseason. Back then, he was an up-and-coming corner, a franchise building block for the Giants. Then he tore his right ACL in a preseason game against the Bears, instantly ending his season. A year later, after a furious rehab, he returned to training camp in Albany and promptly re-tore the ACL.

  3. We’ll need to take up references http://rockettube.fun/ roket tube
    The proposed Transatlantic Trade and Investment Partnershippact would be the world’s biggest free-trade deal, coveringabout 50 percent of global economic output, 30 percent of globaltrade and 20 percent of global foreign direct investment.

  4. I want to make a withdrawal http://kratom.in.net/ kratom extract Last month, Japanese newspaper Asahi Shimbun reported that North Korea had executed nine music performers in an apparent attempt to quash rumours that the wife of leader Kim Jong-un might once have led a “provocative” lifestyle.

  5. An accountancy practice http://planetsuzy.fun/ planet suzy Across the country, the U.S. Supreme Court ruling that gaymarried couples must receive the same federal benefits, such astax breaks, granted to heterosexual couples has emboldenedadvocates, who are pursuing lawsuits in state and federalcourts.

  6. Hello good day http://12yo.icu/ 11yo
    Among other things, he argued, it will require long-term follow-up to be sure the child is truly HIV-negative, even though it has not been possible to recover replication-competent virus and there is little evidence of infection.

  7. I never went to university http://ghettotube.in.net/ gettotube Ryanair's chief executive, Michael O'Leary, said the deal “proves how UK airports can flourish when released from the dead hand of the BAA monopoly and is the first dramatic initiative by MAG to reverse 7 years of decline, during which Stansted’s traffic fell from 23.8 million to 17.5 million”.

  8. Could I make an appointment to see ? http://yuvututube.fun/ yuvutu tube mobile Carlyle’s strategy of focusing on selling Arinc to industryplayers for a higher price tag is not without risk. It couldresult in private equity firms trying to lowball the auctionshould the strategic bidders decide to walk away.

  9. I’m doing a phd in chemistry http://boobs.pet/ bare boobs Washington rallied from 12 points down to beat the Huskers in 1991 and Colorado came back from 12 points down to win in 1990, but those games can’t compare to the magnitude of Nebraska’s meltdown Saturday.

  10. I’d like to transfer some money to this account http://keandra.in.net/ keandra porn Finland is a key export market for InterRao, and used toaccount for around 40 percent of its foreign sales. But suppliesto Finland dropped to around 20 percent of InterRao’s total lastyear to 3.8 billion kilowatt/hour as cheaper hydroelectric powertook market share.

  11. What sort of music do you listen to? http://madthumbs.fun/ mad thumb
    I sincerely hope I never have to type the words “Kentavious Caldwell-Pope” ever again because that’s simply a ridiculous name, but I guess I could always make lame, tired “Are you down with KCP? Yeah you know me” jokes to show the world that I’m an old white guy.

  12. What do you want to do when you’ve finished? http://efukt.fun/ efukt porn
    Ever since the Converse Chuck Taylor All Star sneakers left the basketball court and were first worn on the street they have been an enduring choice for stylish men and women alike. Beloved of hipsters, skaters, the fashion crowd and, well, just about everybody, a pair of Converse All Star hi tops are a shoe rack essential.

  13. Do you need a work permit? http://thumbzilla.fun/ thumb zilla Costa Crociere SpA, the Italian unit of Miami-based Carnival Corp., is picking up the tab for the parbuckling and its intricate preparation. The company puts the costs so far at 600 million euros ($800 million), though much of that will be passed onto its insurers.

  14. Is this a temporary or permanent position? http://efukt.fun/ efukt.com JD.com, or Jingdong, holds a near one-fifth share of China’s business-to-consumer market, and its courier services allow it to distribute its high-value products to customers in big cities within 24 hours – giving it an edge over Alibaba, which sells mainly lower-cost items, said Forrester’s Bryan Wang.

  15. How long are you planning to stay here? http://xnxx.photography/ xnxx When the rule was created this offseason, Westhoff said, it was because the players wanted to see it. The push play was a tactic Westhoff said he had employed for years, including last season with the Jets. And though he found it to be a useful technique, he always thought the rule should be changed for safety reasons.

  16. Cool site goodluck 🙂 http://ampland.fun/ ampland movies The “X-Factor” judge addressed the 12-hour ordeal she and a half-dozen companions faced when their charter boat captain became disoriented in choppy weather off the coast of Provincetown, Mass.

  17. I quite like cooking http://xvideos.doctor/ xx videos Lord Maginnis said it was “to the credit of the Orange Order’s leadership that the backlash has been kept to a minimum. But they cannot hope to maintain their influence unless greater honesty and realism is shown by their critics.”

  18. I’d like to open an account http://redtube.in.net/ redtube porn What comes next will be watched closely by city managers, labor unions and bond investors throughout the United States to see who wins and loses as the city tries to repay an estimated $18 billion in debt. Pensions are a relatively small part of the total, but a growing concern for its aging workforce.

发表评论

电子邮件地址不会被公开。