ReDex使ってみた(けど動かなかった記録)

Facebookが公開した[ReDex](http://fbredex.com/)を使ってみた。

・・・結論としては私のアプリ(TwitPane)のAPKで試したところ多少小さくはなったけど実機でインストール後に起動すらしなかったのでなんの知見もありませんw

が、せっかく苦労して環境作ったのでメモを残しておきます。


[Facebook、Androidアプリのバイトコードを最適化する「ReDex」をオープンソース化：CodeZine（コードジン）](http://codezine.jp/article/detail/9381)

# Ubuntu 14.04 LTS を用意する

軟弱者なので「Ubuntu 14.04 LTS 日本語 Remix」を使った。```ubuntu-ja-14.04-desktop-amd64.iso``` をゲットして Windows10 上の VMware 上に構築。

直接関係ないけど愛用のTeraTermからsshできるようにopenssh-serverを入れといた。

```bash
$ sudo apt-get install openssh-server
```


# ReDex をビルド

[Redex](http://fbredex.com/) に書いてあるとおりに。

## 必要パッケージのapt-get

```bash
$ sudo apt-get install \
    g++ \
    automake \
    autoconf \
    autoconf-archive \
    libtool \
    libboost-all-dev \
    libevent-dev \
    libdouble-conversion-dev \
    libgoogle-glog-dev \
    libgflags-dev \
    liblz4-dev \
    liblzma-dev \
    libsnappy-dev \
    make \
    zlib1g-dev \
    binutils-dev \
    libjemalloc-dev \
    libssl-dev \
    libiberty-dev
```

## Redex をゲットする

```bash
$ sudo apt-get install git
...
$ git clone https://github.com/facebook/redex.git
$ cd redex/
```

## ReDex が依存している folly を submodule としてゲットする

```bash
takke@ubuntu:~/redex-work/redex$ git submodule update --init
Submodule 'third-party/folly' (https://github.com/facebook/folly.git) registered for path 'third-party/folly'
Cloning into 'third-party/folly'...
remote: Counting objects: 20167, done.
remote: Total 20167 (delta 0), reused 0 (delta 0), pack-reused 20167
Receiving objects: 100% (20167/20167), 8.72 MiB | 2.78 MiB/s, done.
Resolving deltas: 100% (15965/15965), done.
Checking connectivity... done.
Submodule path 'third-party/folly': checked out 'f549d777735917759dc85580ada13240acdfb3c6'
```

## ReDex のビルド

```bash
takke@ubuntu:~/redex-work/redex$ autoreconf -ivf && ./configure && make && make install
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
autoreconf: configure.ac: tracing
autoreconf: configure.ac: adding subdirectory third-party/folly/folly to autoreconf
autoreconf: Entering directory `third-party/folly/folly'
autoreconf: running: aclocal --force -I m4
autoreconf: configure.ac: creating directory build-aux
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'.
libtoolize: copying file `build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
libtoolize: copying file `m4/libtool.m4'
libtoolize: copying file `m4/ltoptions.m4'
libtoolize: copying file `m4/ltsugar.m4'
libtoolize: copying file `m4/ltversion.m4'
libtoolize: copying file `m4/lt~obsolete.m4'
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
configure.ac:32: installing 'build-aux/compile'
configure.ac:32: installing 'build-aux/config.guess'
configure.ac:32: installing 'build-aux/config.sub'
configure.ac:20: installing 'build-aux/install-sh'
configure.ac:20: installing 'build-aux/missing'
Makefile.am: installing 'build-aux/depcomp'
parallel-tests: installing 'build-aux/test-driver'
autoreconf: Leaving directory `third-party/folly/folly'
autoreconf: configure.ac: not using Libtool
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:13: installing './compile'
configure.ac:24: installing './config.guess'
configure.ac:24: installing './config.sub'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'
autoreconf: Leaving directory `.'
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for g++... g++
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for ranlib... ranlib
checking dependency style of gcc... (cached) gcc3
checking dependency style of g++... (cached) gcc3
checking for a Python interpreter with version >= 3.0... python3
checking for python3... /usr/bin/python3
checking for python3 version... 3.4
checking for python3 platform... linux
checking for python3 script directory... ${prefix}/lib/python3.4/site-packages
checking for python3 extension module directory... ${exec_prefix}/lib/python3.4/site-packages
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for boostlib >= 1.54.0... yes
checking whether the Boost::Filesystem library is available... yes
checking for exit in -lboost_filesystem... yes
checking whether the Boost::System library is available... yes
checking for exit in -lboost_system... yes
checking for adler32 in -lz... yes
checking for ceil in -ldouble-conversion... no
configure: error: Please install double-conversion library
takke@ubuntu:~/redex-work/redex$
```

double-conversion がないとかで怒られた。

## double-conversionの誤検知対処

follyを自前でビルドする必要があるのかとか、自力でdouble-conversion入れる必要があるのかとかごにょごにょしてみたけど結局解決せず、、

issueを見てみると既知の不具合のようで [double-conversion has installed, but configure error · Issue #15 · facebook/redex](https://github.com/facebook/redex/issues/15) とか [configure error: Please Install double-conversion library · Issue #29 · facebook/redex](https://github.com/facebook/redex/issues/29) とかが上がってた。

おそらく double-conversion 自体は問題なくインストールできてるんだけど configure で誤検知してる風、というわけでとりあえず #15 のほうのコメントを参考に```configure.ac```の

```
AC_CHECK_LIB([double-conversion],[ceil],[],[AC_MSG_ERROR(
```

を

```
AC_CHECK_LIB([double-conversion],[main],[],[AC_MSG_ERROR(
```

に変えて通した。


## ReDexを使ってみる

```bash
takke@ubuntu:~/redex-work$ redex TwitPane_free_881-SNAPSHOT_20160412_1357_armeabi-v7a.apk -o output-sign.apk -s xxxx.keystore -a xxxx -p xxxx --sign
Optimization warning: Pure abstract class passed to encode: 1 occurrences
jar signed.

Warning:
No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2110-03-18) or after any future revocation date.
Couldn't find zipalign.  Your APK may not be fully optimized.
```

いろいろWarning出てるので解決しないといけないんだけどとりあえずこのまま```output-sign.apk```を実機に持って行くとインストールできたけど起動しませんでした。

また時間があるときに試します。