LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

TensorFlow for Android 勉強会@浜松

Posted at

一時的な資料なのでイベント終わったらこの記事は消します(すべて整理してまた投稿し直します)

UbuntuのDockerコンテナがおすすめ

docker pull ubuntu:latest
docker run -it --rm ubuntu

セットアップ

# バージョンは適当に変えてください
SDK_VERSION=25.2.3
NDK_VERSION=r13b
SDK_API_VERSION=25
BUILD_TOOLS_VERSION=25.0.2
TENSORFLOW_TAG=v0.12.0

# ディレクトリは適当に変えてください
WORK_DIR=/home
LIB_DIR=/usr/local/lib

SDK_URL=https://dl.google.com/android/repository/tools_r${SDK_VERSION}-linux.zip
NDK_URL=https://dl.google.com/android/repository/android-ndk-${NDK_VERSION}-linux-x86_64.zip

cat >> /etc/profile << EOS
export ANDROID_HOME=${LIB_DIR}/android-sdk
export ANDROID_NDK_HOME=${LIB_DIR}/android-ndk-${NDK_VERSION}
EOS
cat >> /etc/profile << 'EOS'
export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
export PATH=${PATH}:${ANDROID_NDK_HOME}
EOS
source /etc/profile

# 好きなリポジトリにしてください
sed -i.bak -e "s%http://archive.ubuntu.com/ubuntu/%http://ftp.jaist.ac.jp/pub/Linux/ubuntu/%g" /etc/apt/sources.list

apt-get update
apt-get upgrade -y
apt-get install -y git curl unzip

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | apt-key add -

apt-get update && apt-get install -y bazel &

# 適当にハッシュチェックとかしてください
curl ${SDK_URL} -sSL -o /tmp/sdk.zip && unzip -q /tmp/sdk.zip -d ${LIB_DIR}/android-sdk &
curl ${NDK_URL} -sSL -o /tmp/ndk.zip && unzip -q /tmp/ndk.zip -d ${LIB_DIR} &

wait

apt-get clean

cd $WORK_DIR
git clone https://github.com/ornew/MNIST-for-Android.git
pushd MNIST-for-Android
pushd lib
cat > WORKSPACE << EOS
workspace(name = "mnist_android")

android_sdk_repository(
    name = "androidsdk",
    api_level = ${SDK_VERSION},
    build_tools_version = "${BUILD_TOOLS_VERSION}",
    path = "${ANDROID_HOME}",
)

android_ndk_repository(
    name = "androidndk",
    path = "${ANDROID_NDK_HOME}",
    api_level = 21)

git_repository(
    name = "org_tensorflow",
    remote = "https://github.com/tensorflow/tensorflow.git",
    init_submodules = 1,
    tag = "${TENSORFLOW_TAG}"
)

load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace")
tf_workspace()
EOS

soのビルド

cd $WORK_DIR/MNIST-for-Android/lib
bazel build libmnist_android.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

gradlewでコンソールからビルドする場合

cd $WORK_DIR/MNIST-for-Android
cp lib/bazel-bin/libmnist_android.so android/app/src/main/jniLibs/armeabi-v7a/
android list sdk -a | grep -E "SDK Platform Android .* API ${SDK_VERSION}" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
android list sdk -a | grep "Android SDK Build-tools, revision ${BUILD_TOOLS_VERSION}" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
android list sdk -a | grep "Android Support Repository" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
cd android
bash ./gradlew build
# build/outputs/apk/app-debug.apk
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up