weex 打包编译 android app

更新日期: 2017-05-31 阅读次数: 15897 分类: weex

vue project 与 weexpack project 的区别

执行

$ weex

可以看到 weex 的命令行参数

  init                                   create a vue project
  debug                                  start weex debugger
  compile                                compile we/vue file
  create                                 create a weexpack project
  platform <add|remove> <ios|android>    add/remove ios/android platform
  plugin <add|remove> <pluginName>       add/remove weexplugin
  run <ios|android>                      build your ios/android app and run

注意,init 与 create 对应不同的项目结构。一个是 vue project, 一个是 weexpack project

例如,一个用 weex init 初始化的 vue Hello world 项目,如果添加 ios/android platform 会报错

$ weex platform add android
Error: Current working directory is not a weexpack project.

打包成 android

$ weex run android
module.js:471
    throw err;
    ^

Error: Cannot find module '../src/run/Web'

日了,这居然是 weexpack 的一个 bug, 需要手动源码

vim ~/.xtoolkit/node_modules/weexpack/bin/weexpack-run

将 const runWeb = require('../src/run/Web') 中的 /Web 修改成 /web

再次执行 weex run android

Environment variable $ANDROID_HOME not found !

看来是需要下载安装 Android Studio 了。

启动 Android Studio 报错

unable to access android sdk add-on list

解决方法参考 Android Studio 无法启动, 卡在 fetching android sdk component information - Android - 大象笔记

也可以设置本地代理访问。配置好之后

在 ~/.bashrc 加入以下配置

export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

注意,不要忘了加上最后一行,否则会报错

adb: not found

把 android 手机开启开发者模式,编译,继续报错

File /home/zhongwei/.android/repositories.cfg could not be loaded.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> You have not accepted the license agreements of the following SDK components:
  [Android SDK Platform 23, Android SDK Build-Tools 23.0.2].
  Before building your project, you need to accept the license agreements and complete the installation of the missing components using the Android Studio SDK Manager.
  Alternatively, to learn how to transfer the license agreements from one workstation to another, go to http://d.android.com/r/studio-ui/export-licenses.html

如果是 Android Studio 2.3 可以执行命令解决

$ANDROID_HOME/tools/bin/sdkmanager --licenses

重新编译,继续报错

:appframework:processReleaseResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':appframework:processReleaseResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt

解决方法

sudo apt-get install -y lib32gcc1 libc6-i386 lib32z1 lib32stdc++6
sudo apt-get install -y lib32ncurses5 lib32gomp1 lib32z1-dev

继续编译,还是报错

:appframework:compileReleaseJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':appframework:compileReleaseJavaWithJavac'.
> Could not find tools.jar. Please check that /usr/lib/jvm/java-8-openjdk-amd64 contains a valid JDK installation.

重新安装 openjdk 即可

sudo apt-get install openjdk-8-jdk

哈哈,终于编译通过啦。

weex android app

weex pack

weexpack 是基于 Weex 快速搭建应用原型的利器。它能够帮助开发者通过命令行创建 Weex 工程,添加相应平台的 Weex app 模版,并基于模版从本地,GitHub 或者 Weex 应用市场安装插件,快速打包 Weex 应用并安装到手机运行,对于具有分享精神的开发者而言还能够创建 Weex 插件模版并发布插件到 Weex 应用市场。

weex toolkit 的新版本中支持了 weex pack 的命令

例如,weex create 就可以创建 weexpack 的项目初始化目录。

weex toolkit 与 weex-pack 的区别

weex-toolkit 初始化的项目是针对开发单个 Weex 页面而设计的,也就是说这样的项目只包括单个页面开发需要的东西,比如前端页面源文件、webpack 配置、npm 脚本等。项目产生的输出就是一个 JS Bundle 文件,可以自由的进行部署。

weex-pack 是初始化一个完整的 App 工程,包括 Android 和 iOS 的整个 App 起步,前端页面只是其中的一部分。这样的项目最终产出是一个 Android App 和一个 iOS App。

从 Github 的 commit 历史来看,weex-pack 比 weex-toolkit 更活跃啊。

参考

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式