freeline 实现秒级编译

2017-07-04  本文已影响220人  陌上疏影凉

Android Studio使用freeline编译步骤:

配置gradle

在project的build.gradle加入:
classpath 'com.antfortune.freeline:gradle:0.8.7'

总体效果如下:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.antfortune.freeline:gradle:0.8.7'
    }
}

在module的build.gradle文件中添加:

apply plugin: 'com.antfortune.freeline'

效果如下:

apply plugin: 'com.android.application'
apply plugin: 'com.antfortune.freeline'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.momo.myapplication"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

下载安装python

mac直接在终端命令行输入

brew install python

安装python环境,如果查找不到brew命令那么先去下载包管理器homebrew。因为后面的打包等操作都是由python脚本来控制的,所以需要有python环境。

初始化freeline

在Android Studio终端输入

./gradlew initFreeline -Pmirror

如果有科学上网工具,可以直接输入

./gradlew initFreeline

初始化freeline。初始化完成后会将配置写入json中,并且在项目目录下会多出一个freeline文件夹和freeline.pyfreeline_project_description.json

下载插件

如果觉得每次都要在命令行输命令很麻烦,Android Studio有freeline的插件,在Settings或者Prefrences->Plugin -> Browse repositories ... -> 输入freeline,安装插件后重启AS,在工具栏里就会有一个freeline的按钮,下次运行的时候,直接点击按钮就可以了。

编译运行

第一次要进行全量编译
在终端输入:

python freeline.py -f

第一次执行全量编译的时间比较长,不过相比于gradle已经快出很多了。编译完成后,app也会在模拟器或者手机中打开了。

全量编译.png

之后就可以进行增量编译了,比如改动了几行代码之后,再次运行时在终端输入:

python freeline.py

这时候编译基本都会在几秒内完成,并且也会在模拟器或者手机中打开。比instant run的速度还要快上许多。

增量编译.png

说明

配置过程中可能会遇到一些问题,比如我就遇到了这几个问题:

[ERROR] --------------------------------------------------------
[ERROR] Freeline ERROR
[ERROR] --------------------------------------------------------
Please make sure your application is properly running in your device.
Check follow steps:
1. Make sure the versions python freeline.py -v, freeline-gradle and freeline-runtime are the same;
2. Make sure there is no network proxy.
3.
More about this can see: #152
[ERROR] --------------------------------------------------------
[ERROR] Freeline server in app “包名” not found. Please make sure your application is properly running in your device.
[ERROR] --------------------------------------------------------
[DEBUG] Prepare tasks time: 0.5s
[DEBUG] Task engine running time: 16.7s
[DEBUG] Total time: 17.1s
[DEBUG] --------------------------------------------------------

关于这个错可以参考这个链接https://github.com/alibaba/freeline/issues/152,很有可能是没有先全量编译就直接增量编译导致,或者使用Android Studio自带的instant run运行了,freeline的全量编译与android-studio自带的run会存在冲突。

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied

[ERROR] --------------------------------------------------------
[ERROR] unexpected exception within task

报错的原因是没有权限,需要先执行:

chmod +x gradlew 

给gradlew赋予执行的权限,再进行全量编译与增量编译就不会报错了。具体的也可以参考这个链接
https://github.com/alibaba/freeline/issues/65

参考资料

  1. Github-alibaba/freeline
  2. AndroidStudio Freeline实现工程秒级编译
  3. freeline使用入门
上一篇 下一篇

猜你喜欢

热点阅读