一. freeline簡介
Freeline的GitHub地址: https://github.com/alibaba/freeline
其官方介紹如下:
Freeline is a fast build and deployment tool for Android. Caching reusable class files and resource indices, it enables incrementally building Android apps, and optionally deploying the update to your device with hot swap.
Developed and used by Ant Fortune (about us) Android Team, Freeline has been significantly saving time in daily work. Inspiring by Buck and Instant Run but faster than ever, Freeline can finish an incremental build in just a few seconds. No more recompile and reinstall again and again before seeing your modifications, Freeline brings life-changing development experience for Android.
.
中文說明
下面是其原理說明
[Freeline原理] (https://yq.aliyun.com/articles/59122?spm=5176.8091938.0.0.1Bw3mU)
從中可以看出freeline加速的三駕馬車是: 緩存、并行支竹、Hot Swap (Instant Run原理)
二. 配置
- 配置根目錄下的build.gradle文件
在項(xiàng)目根目錄下的build.gradle文件中添加freeline插件:
classpath 'com.antfortune.freeline:gradle:0.8.1'
,
如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.antfortune.freeline:gradle:0.8.1'
}
}
allprojects {
repositories {
jcenter()
}
}
- 配置主module下的build.gradle文件
a. 應(yīng)用插件 (放在android插件之后)
apply plugin: 'com.antfortune.freeline'
b. 添加freeline配置
在android {}
中添加, 如下:
freeline {
hack true
//如果你定義了productFlavor, 記得把下面的注釋放開, 把{Your-productFlavor}替換成你相應(yīng)的productFlavor
//productFlavor '{Your-productFlavor}'
}
c. (**這一步可以和下面的"3"同時(shí)省略 --- [參考這里](https://github.com/alibaba/freeline/issues/280)**) 在dependencies塊中添加對(duì)freeline的依賴, 如下:
```
dependencies {
// others
// ....
// dependencies for freeline
debugCompile 'com.antfortune.freeline:runtime:0.8.1'
//如果release包不想依賴freeline, 可以改成`releaseProvided com.antfortune.freeline:runtime-no-op:0.8.1'`, 后面會(huì)進(jìn)行說明.
releaseCompile 'com.antfortune.freeline:runtime-no-op:0.8.1'
testCompile 'com.antfortune.freeline:runtime-no-op:0.8.1'
}
- (這一步可以和上面的"c"同時(shí)省略 --- 參考這里) 在代碼中初始化freeline
在你自定義的Application的onCreate方法里面加上下面這句:
FreelineCore.init(this);
三. 運(yùn)行
由于freeline依賴于python, 所以你首先要安裝python2.7+的運(yùn)行環(huán)境
-
始化你freeline (僅第一次), 在項(xiàng)目根目錄下執(zhí)行下列命令:
./gradlew initFreeline
這一步會(huì)下載freeline的相關(guān)依賴, 會(huì)在項(xiàng)目根目錄下生成freeline.py文件和freeline目錄
freeline依賴文件和目錄 -
現(xiàn)在可以開始運(yùn)行你的程序了, 執(zhí)行命令
python freeline.py
, 首次運(yùn)行 可以加上-f
選項(xiàng) (python freeline.py -f
) ,
-f
選項(xiàng)會(huì)強(qiáng)制執(zhí)行clean任務(wù), 然后再執(zhí)行build任務(wù)
當(dāng)然你也可以查看freeline的幫助, 執(zhí)行命令:python freeline.py -h
首次運(yùn)行會(huì)比較慢, 因?yàn)樾枰x取項(xiàng)目信息, 生成文件, 計(jì)算依賴 ......
首次使用freeline構(gòu)建項(xiàng)目
當(dāng)然如果你覺得每次都輸入命名來構(gòu)建項(xiàng)目不方便, 你也可以安裝freeline的插件, Preferences -> Plugin -> Browse repositories ... -> 輸入freeline -> 安裝插件, 操作如下圖:
安裝freeline插件
安裝之后會(huì)在AS的工具欄多了個(gè)freeline的按鈕, 如下, 以后運(yùn)行點(diǎn)擊這個(gè)按鈕就可以了
更改代碼后直接點(diǎn)擊此按鈕運(yùn)行項(xiàng)目, 不用再輸入命令運(yùn)行啦 !!
第一次運(yùn)行項(xiàng)目, 45秒, 輸出如下:
第一次運(yùn)行項(xiàng)目的log輸出.png
頁面如下:
初始頁面.png
然后我更改一下頁面, 再用freeline運(yùn)行只花了1.4秒!!!!! 輸出如下:
更改后運(yùn)行的log輸出.png
更改后的頁面, 如下:
更改后的頁面
四. release與debug區(qū)分 (不將freeline代碼打包進(jìn)release包中)
- 如果省略了上面"c"和"3"步驟, 那么代碼對(duì)freeline就沒有依賴了, 因此打debug可以使用freeline, 而release則用常規(guī)方法打包 (用常規(guī)的gradlew打包就行
./gradlew :app:assembleRelease
). 目前官方的使用教程里是沒有"c"和"3"步驟的, 只有你在build.gradle里面設(shè)置了applicationProxy false
, 才需要"c"和"3"步驟, 參考: FreelineCore.init()的作用是什么吁系,為什么調(diào)不調(diào)都可以给僵? - 如果用了"c"和"3"步驟那么, 那么可以將release依賴改成下面這樣:
releaseProvided 'com.antfortune.freeline:runtime-no-op:0.8.1'
這樣打包的時(shí)候, freeline的庫代碼不會(huì)被打包進(jìn)apk中.
.
代碼初始化的地方改成這樣:
if(BuildConfig.DEBUG) FreelineCore.init(this);
這這樣打releae包時(shí), BuildConfig.DEBUG是false, 編譯器會(huì)優(yōu)化掉這段代碼, 因此去掉了代碼中對(duì)freeline的依賴.
這樣打出來的apk包也不會(huì)包含freeline相關(guān)的代碼 !!
五. 總結(jié)
- 遇到一個(gè)坑, 請(qǐng)看這里
- 如果你不能忍受打包的蝸牛速度, 那就趕緊使用freeline吧 -- .