From 014208e62ad9c0b22dfe9251eee2e47dbe72f999 Mon Sep 17 00:00:00 2001 From: Kirill Snezhko <4477094+argrento@users.noreply.github.com> Date: Wed, 25 May 2022 11:18:06 +0300 Subject: [PATCH] [Android] Update gradle version and other changes in android apps, CI modification to auto-build Android apps and upload artifacts (#11241) * Update gradle version in android_rpc app * Support latest gradle, bump versions, replace ndk build script with gradle tasks * [android_rpc] Fix linter errors, disable weird ones * [android_deploy] Support latest gradle, bump versions, fix linter errors, disable some of them * [android_camera] Support latest gradle, bump versions, rewrite readme * [android_camera] Fix linter errors * Fix sanity check errors * Add Android jobs for Github Actions * Add python requirements for TVM and android_camera, use preinstalled NDK * Revert to build with make * Add minrpc include (PR #11232) * Remove relative paths --- .github/workflows/main.yml | 169 ++++++++++++------ apps/android_camera/README.md | 129 ++++++++++--- apps/android_camera/app/build.gradle | 52 ++++-- .../app/src/main/AndroidManifest.xml | 2 +- .../Camera2BasicFragment.java | 2 +- .../androidcamerademo/MainActivity.java | 1 + .../app/src/main/jni/Android.mk | 3 + .../app/src/main/jni/tvm_runtime.h | 5 + .../app/src/main/res/layout/listview_row.xml | 2 +- apps/android_camera/build.gradle | 13 +- apps/android_camera/gradle.properties | 1 - apps/android_camera/models/prepare_model.py | 2 +- apps/android_camera/models/requirements.txt | 4 + apps/android_deploy/app/build.gradle | 57 ++++-- .../android_deploy/app/download-models.gradle | 2 +- .../app/src/main/AndroidManifest.xml | 9 +- .../apache/tvm/android/demo/MainActivity.java | 10 +- .../app/src/main/res/layout/activity_main.xml | 37 ++-- .../app/src/main/res/layout/content_main.xml | 7 +- .../app/src/main/res/values/strings.xml | 2 + apps/android_deploy/build.gradle | 8 +- apps/android_deploy/gradle.properties | 2 + apps/android_rpc/README.md | 14 +- apps/android_rpc/app/build.gradle | 58 ++++-- .../app/src/main/AndroidManifest.xml | 8 +- .../org/apache/tvm/tvmrpc/MainActivity.java | 18 +- .../org/apache/tvm/tvmrpc/RPCActivity.java | 2 +- apps/android_rpc/app/src/main/jni/build.sh | 26 --- .../app/src/main/jni/tvm_runtime.h | 1 + .../app/src/main/res/layout/activity_main.xml | 10 +- .../app/src/main/res/layout/activity_rpc.xml | 10 +- .../app/src/main/res/layout/content_main.xml | 23 ++- .../app/src/main/res/values/strings.xml | 5 + apps/android_rpc/build.gradle | 6 +- .../gradle.properties} | 19 +- .../scripts/task_config_build_jvm.sh | 29 +-- 36 files changed, 497 insertions(+), 251 deletions(-) create mode 100644 apps/android_camera/models/requirements.txt delete mode 100755 apps/android_rpc/app/src/main/jni/build.sh rename apps/{android_deploy/app/src/main/jni/build.sh => android_rpc/gradle.properties} (54%) rename apps/android_camera/app/src/main/jni/build.sh => tests/scripts/task_config_build_jvm.sh (58%) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 48b9d62bb9b7..313c440cbd21 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,59 +38,124 @@ jobs: MacOS: runs-on: macOS-latest steps: - - uses: actions/checkout@v2 - with: - submodules: 'recursive' - - name: Set up environment - uses: ./.github/actions/setup - - name: Conda Build - shell: bash -l {0} - run: >- - conda build --output-folder=conda/pkg conda/recipe && - conda install tvm -c ./conda/pkg - - name: Build iOS RPC - run: | - IOS_VERSION="14.0" - CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_SYSTEM_NAME=iOS \ - -DCMAKE_SYSTEM_VERSION=${IOS_VERSION} \ - -DCMAKE_OSX_SYSROOT=iphonesimulator \ - -DCMAKE_OSX_ARCHITECTURES=x86_64 \ - -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \ - -DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON \ - -DUSE_IOS_RPC=ON" - - mkdir build-ios-simulator - cd build-ios-simulator - cmake .. ${CMAKE_FLAGS} - cmake --build . --target ios_rpc - - name: Test - shell: bash -l {0} - run: >- - python -m pytest -v tests/python/all-platform-minimal-test - - name: Test iOS RPC - shell: bash -l {0} - run: >- - python -m pip install tornado psutil cloudpickle && - export PYTHONPATH=tests/python/contrib:${PYTHONPATH} && - export BUNDLE_ID=org.apache.tvmrpc && - export BUNDLE_PATH=build-ios-simulator/apps/ios_rpc/ios_rpc/src/ios_rpc-build/Release-iphonesimulator/tvmrpc.app && - python -m pytest -v tests/python/contrib/test_rpc_server_device.py + - uses: actions/checkout@v2 + with: + submodules: 'recursive' + - name: Set up environment + uses: ./.github/actions/setup + - name: Conda Build + shell: bash -l {0} + run: >- + conda build --output-folder=conda/pkg conda/recipe && + conda install tvm -c ./conda/pkg + - name: Build iOS RPC + run: | + IOS_VERSION="14.0" + CMAKE_FLAGS="-DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_SYSTEM_NAME=iOS \ + -DCMAKE_SYSTEM_VERSION=${IOS_VERSION} \ + -DCMAKE_OSX_SYSROOT=iphonesimulator \ + -DCMAKE_OSX_ARCHITECTURES=x86_64 \ + -DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \ + -DCMAKE_BUILD_WITH_INSTALL_NAME_DIR=ON \ + -DUSE_IOS_RPC=ON" + + mkdir build-ios-simulator + cd build-ios-simulator + cmake .. ${CMAKE_FLAGS} + cmake --build . --target ios_rpc + - name: Test + shell: bash -l {0} + run: >- + python -m pytest -v tests/python/all-platform-minimal-test + - name: Test iOS RPC + shell: bash -l {0} + run: >- + python -m pip install tornado psutil cloudpickle && + export PYTHONPATH=tests/python/contrib:${PYTHONPATH} && + export BUNDLE_ID=org.apache.tvmrpc && + export BUNDLE_PATH=build-ios-simulator/apps/ios_rpc/ios_rpc/src/ios_rpc-build/Release-iphonesimulator/tvmrpc.app && + python -m pytest -v tests/python/contrib/test_rpc_server_device.py Windows: runs-on: windows-2019 steps: - - uses: actions/checkout@v2 - with: - submodules: 'recursive' - - name: Set up environment - uses: ./.github/actions/setup - - name: Conda Build - shell: cmd /C call {0} - run: >- - conda build --output-folder=conda/pkg conda/recipe && - conda install tvm -c ./conda/pkg - - name: Test - shell: cmd /C call {0} - run: >- - python -m pytest -v tests/python/all-platform-minimal-test + - uses: actions/checkout@v2 + with: + submodules: 'recursive' + - name: Set up environment + uses: ./.github/actions/setup + - name: Conda Build + shell: cmd /C call {0} + run: >- + conda build --output-folder=conda/pkg conda/recipe && + conda install tvm -c ./conda/pkg + - name: Test + shell: cmd /C call {0} + run: >- + python -m pytest -v tests/python/all-platform-minimal-test + + Android: + runs-on: Ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + with: + submodules: 'recursive' + - name: Set up environment + uses: ./.github/actions/setup + - name: Set up java + uses: actions/setup-java@v3 + with: + distribution: 'zulu' + java-version: '11' + - name: Build TVM + shell: bash -l {0} + run: | + mkdir build + cd build + ../tests/scripts/task_config_build_jvm.sh . + cmake .. + make + - name: Build TVM4J + run: | + make jvmpkg + - name: Build android_rpc + working-directory: apps/android_rpc + run: | + export PATH="${ANDROID_NDK_HOME}:$PATH" + gradle clean build + - name: Upload android_rpc APK + uses: actions/upload-artifact@v2 + with: + name: android_rpc-debug.apk + path: ./apps/android_rpc/app/build/outputs/apk/debug/app-debug.apk + - name: Build android_deploy + working-directory: apps/android_deploy + run: | + export PATH="${ANDROID_NDK_HOME}:$PATH" + gradle clean build + - name: Upload android_deploy APK + uses: actions/upload-artifact@v2 + with: + name: android_deploy-debug.apk + path: ./apps/android_deploy/app/build/outputs/apk/debug/app-debug.apk + - name: Build android_camera + working-directory: apps/android_camera + run: | + mkdir -p app/src/main/assets/models/ + export TVM_NDK_CC=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang++ + export TVM_HOME=~/work/tvm/tvm + export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH} + python3 ${TVM_HOME}/python/gen_requirements.py + pip3 install -r ${TVM_HOME}/python/requirements/core.txt + cd models + pip3 install -r requirements.txt + python3 prepare_model.py + cd .. + export PATH="${ANDROID_NDK_HOME}:$PATH" + gradle clean build + - name: Upload android_camera APK + uses: actions/upload-artifact@v2 + with: + name: android_camera-debug.apk + path: ./apps/android_camera/app/build/outputs/apk/debug/app-debug.apk \ No newline at end of file diff --git a/apps/android_camera/README.md b/apps/android_camera/README.md index c292ce40c582..f659e905f281 100644 --- a/apps/android_camera/README.md +++ b/apps/android_camera/README.md @@ -1,28 +1,107 @@ -[//]: # Licensed to the Apache Software Foundation (ASF) under one -[//]: # or more contributor license agreements. See the NOTICE file -[//]: # distributed with this work for additional information -[//]: # regarding copyright ownership. The ASF licenses this file -[//]: # to you under the Apache License, Version 2.0 (the -[//]: # "License"); you may not use this file except in compliance -[//]: # with the License. You may obtain a copy of the License at -[//]: # -[//]: # http://www.apache.org/licenses/LICENSE-2.0 -[//]: # -[//]: # Unless required by applicable law or agreed to in writing, -[//]: # software distributed under the License is distributed on an -[//]: # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -[//]: # KIND, either express or implied. See the License for the -[//]: # specific language governing permissions and limitations -[//]: # under the License. - -Android Camera Demo Sample App -============================== - -The Android Camera Demo Sample App provides a basic implementation of an Android -app that uses the tvm runtime to perform image classification in real time. - -Converting Models ------------------ + + + + + + + + + + + + + + + + + + +# Android Camera Demo Sample App + +The Android Camera Demo Sample App provides a basic implementation of an Android app that uses the tvm runtime to perform image classification in real time. + +You will need JDK, [Android NDK](https://developer.android.com/ndk) and an Android device to use this. + +## Build and Installation + +### Prepare Models The `models/prepare_models.py` script provides a example flow for dumping model parameter files for use by the app. + +1. Set path to the NDK CC: `export TVM_NDK_CC=[Path to CC, e.g. /opt/android-toolchain-arm64/bin/aarch64-linux-android-g++]` +2. Switch to the script directory: `cd models` +3. Run script: `python3 prepare_model.py` + +#### Sample output +``` +mobilenet_v2 +getting model... +building... +dumping lib... +dumping graph... +dumping params... +dumping labels... +resnet18_v1 +getting model... +building... +dumping lib... +dumping graph... +dumping params... +dumping labels... +``` + +### Build APK + +We use [Gradle](https://gradle.org) to build. Please follow [the installation instruction](https://gradle.org/install) for your operating system. + +Before you build the Android application, please refer to [TVM4J Installation Guide](https://github.com/apache/tvm/blob/main/jvm/README.md) and install tvm4j-core to your local maven repository. You can find tvm4j dependency declare in `app/build.gradle`. Modify it if it is necessary. + +``` +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + androidTestImplementation('androidx.test.espresso:espresso-core:3.2.0', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + implementation 'androidx.appcompat:appcompat:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'com.google.android.material:material:1.5.0' + implementation 'org.apache.tvm:tvm4j-core:0.0.1-SNAPSHOT' + testImplementation 'junit:junit:4.13.2' + + implementation "androidx.concurrent:concurrent-futures:1.0.0" + implementation "androidx.camera:camera-core:1.0.0-beta01" + implementation "androidx.camera:camera-camera2:1.0.0-beta01" + implementation "androidx.camera:camera-view:1.0.0-alpha08" + implementation "androidx.camera:camera-extensions:1.0.0-alpha08" + implementation "androidx.camera:camera-lifecycle:1.0.0-beta01" +} +``` + +Now use Gradle to compile JNI, resolve Java dependencies and build the Android application together with tvm4j. Run following script to generate the apk file. + +```bash +export ANDROID_HOME=[Path to your Android SDK, e.g., ~/Android/sdk] +cd apps/android_camera +gradle clean build +``` + +In `app/build/outputs/apk` you'll find `app-release-unsigned.apk`, use `dev_tools/gen_keystore.sh` to generate a signature and use `dev_tools/sign_apk.sh` to get the signed apk file `app/build/outputs/apk/release/tv8mdemo-release.apk`. + +Upload `tv8mdemo-release.apk` to your Android device and install it: + +```bash +$ANDROID_HOME/platform-tools/adb install app/build/outputs/apk/release/tv8mdemo-release.apk +``` + +If you see error: + + adb: failed to install app/build/outputs/apk/release/tv8mdemo-release.apk: + Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: + Package ml.apache.tvm.android.androidcamerademo signatures do not match the previously installed version; ignoring!] + +Run uninstall first: + +```bash +$ANDROID_HOME/platform-tools/adb uninstall ml.apache.tvm.android.androidcamerademo +``` diff --git a/apps/android_camera/app/build.gradle b/apps/android_camera/app/build.gradle index 8a772a3d29f3..c7767559e4df 100644 --- a/apps/android_camera/app/build.gradle +++ b/apps/android_camera/app/build.gradle @@ -17,20 +17,47 @@ apply plugin: 'com.android.application' +task generateJniHeaders(type: Exec, description: 'Generate JNI Headers') { + def headerPath = "${project.projectDir}/src/main/jni" + def classPath = "${project.projectDir}/../../../jvm/core/target/*" + def filePath = "${project.projectDir}/../../../jvm/core/src/main/java/org/apache/tvm/LibInfo.java" + commandLine "javac", "-h", headerPath, "-classpath", classPath, filePath + doLast { + file("${headerPath}/org_apache_tvm_LibInfo.h").renameTo(file("${headerPath}/org_apache_tvm_native_c_api.h")) + } +} + +task copyFiles(type: Copy, description: 'Copy Sources for ndk-build') { + dependsOn "generateJniHeaders" + def ndkFilesPath = "${project.projectDir}/../../../jvm/native/src/main/native" + def srcPath = "${project.projectDir}/src/main/jni/" + + from "${ndkFilesPath}/org_apache_tvm_native_c_api.cc", "${ndkFilesPath}/jni_helper_func.h" + into srcPath +} + +task deleteLibs(type: Delete, description: "Delete Compiled Libraries") { + dependsOn "copyFiles" + def libsPath = "${project.projectDir}/src/main/libs" + delete libsPath +} + task buildJni(type: Exec, description: 'Build JNI libs') { - commandLine 'sh', 'src/main/jni/build.sh' + dependsOn "deleteLibs" + def buildPath = "${project.projectDir}/src/main/jni" + commandLine "ndk-build", "--directory", buildPath } tasks.withType(JavaCompile) { - //compileTask -> compileTask.dependsOn buildJni + compileTask -> compileTask.dependsOn buildJni } android { - compileSdkVersion 29 + compileSdkVersion 31 defaultConfig { applicationId "ml.apache.tvm.android.androidcamerademo" minSdkVersion 24 - targetSdkVersion 29 + targetSdkVersion 26 renderscriptTargetApi 18 renderscriptSupportModeEnabled true versionCode 1 @@ -53,7 +80,10 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - buildToolsVersion = '29.0.3' + + lintOptions { + disable "Instantiatable" // MainActivity and RPCActivity must extend android.app.Activity + } } dependencies { @@ -61,13 +91,13 @@ dependencies { androidTestImplementation('androidx.test.espresso:espresso-core:3.2.0', { exclude group: 'com.android.support', module: 'support-annotations' }) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'com.google.android.material:material:1.1.0' - implementation 'org.apache.tvm:tvm4j-core:0.0.1-SNAPSHOT' - testImplementation 'junit:junit:4.13' + implementation 'androidx.appcompat:appcompat:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'com.google.android.material:material:1.5.0' + implementation files('../../../jvm/core/target/tvm4j-core-0.0.1-SNAPSHOT.jar') + testImplementation 'junit:junit:4.13.2' - implementation("androidx.concurrent:concurrent-futures:1.0.0") + implementation "androidx.concurrent:concurrent-futures:1.0.0" implementation "androidx.camera:camera-core:1.0.0-beta01" implementation "androidx.camera:camera-camera2:1.0.0-beta01" // If you want to use the CameraX View class diff --git a/apps/android_camera/app/src/main/AndroidManifest.xml b/apps/android_camera/app/src/main/AndroidManifest.xml index 0821286d5543..e5b6465c5874 100644 --- a/apps/android_camera/app/src/main/AndroidManifest.xml +++ b/apps/android_camera/app/src/main/AndroidManifest.xml @@ -28,7 +28,7 @@ tools:ignore="AllowBackup,MissingApplicationIcon"> diff --git a/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/Camera2BasicFragment.java b/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/Camera2BasicFragment.java index 8a5f54a3e399..3a55a62d739c 100644 --- a/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/Camera2BasicFragment.java +++ b/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/Camera2BasicFragment.java @@ -382,7 +382,7 @@ private Bitmap YUV_420_888_toRGB(Image image, int width, int height) { } private float[] getFrame(ImageProxy imageProxy) { - @SuppressLint("UnsafeExperimentalUsageError") + @SuppressLint("UnsafeOptInUsageError") Image image = imageProxy.getImage(); // extract the jpeg content if (image == null) { diff --git a/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/MainActivity.java b/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/MainActivity.java index f9c573a5d1fe..06b1c9730d05 100644 --- a/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/MainActivity.java +++ b/apps/android_camera/app/src/main/java/org/apache/tvm/android/androidcamerademo/MainActivity.java @@ -78,6 +78,7 @@ private void startFragment() { @Override public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (allPermissionsGranted()) { startFragment(); } else { diff --git a/apps/android_camera/app/src/main/jni/Android.mk b/apps/android_camera/app/src/main/jni/Android.mk index a5eacb0c0c2d..4ff3da8f3327 100644 --- a/apps/android_camera/app/src/main/jni/Android.mk +++ b/apps/android_camera/app/src/main/jni/Android.mk @@ -34,11 +34,14 @@ endif include $(config) LOCAL_SRC_FILES := org_apache_tvm_native_c_api.cc + LOCAL_LDFLAGS := -L$(SYSROOT)/usr/lib/ -llog LOCAL_C_INCLUDES := $(ROOT_PATH)/include \ + $(ROOT_PATH)/src/runtime/rpc \ $(ROOT_PATH)/3rdparty/dlpack/include \ $(ROOT_PATH)/3rdparty/dmlc-core/include \ + $(MY_PATH) LOCAL_MODULE = tvm4j_runtime_packed diff --git a/apps/android_camera/app/src/main/jni/tvm_runtime.h b/apps/android_camera/app/src/main/jni/tvm_runtime.h index bed3bc82d5a3..b20227b34db4 100644 --- a/apps/android_camera/app/src/main/jni/tvm_runtime.h +++ b/apps/android_camera/app/src/main/jni/tvm_runtime.h @@ -40,11 +40,16 @@ #include "../src/runtime/graph_executor/graph_executor.cc" #include "../src/runtime/library_module.cc" #include "../src/runtime/logging.cc" +#include "../src/runtime/minrpc/minrpc_logger.cc" #include "../src/runtime/module.cc" #include "../src/runtime/ndarray.cc" #include "../src/runtime/object.cc" +#include "../src/runtime/profiling.cc" #include "../src/runtime/registry.cc" +#include "../src/runtime/rpc/rpc_channel.cc" +#include "../src/runtime/rpc/rpc_endpoint.cc" #include "../src/runtime/rpc/rpc_event_impl.cc" +#include "../src/runtime/rpc/rpc_local_session.cc" #include "../src/runtime/rpc/rpc_module.cc" #include "../src/runtime/rpc/rpc_server_env.cc" #include "../src/runtime/rpc/rpc_session.cc" diff --git a/apps/android_camera/app/src/main/res/layout/listview_row.xml b/apps/android_camera/app/src/main/res/layout/listview_row.xml index 4c233dc40379..5038a27557e9 100644 --- a/apps/android_camera/app/src/main/res/layout/listview_row.xml +++ b/apps/android_camera/app/src/main/res/layout/listview_row.xml @@ -20,7 +20,7 @@ android:id="@+id/listview_row_text" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_marginRight="2dp" + android:layout_marginEnd="2dp" android:background="@drawable/item_selector" android:padding="10dp" android:textSize="18sp" diff --git a/apps/android_camera/build.gradle b/apps/android_camera/build.gradle index a58bc631dac5..1cd5ac9b656f 100644 --- a/apps/android_camera/build.gradle +++ b/apps/android_camera/build.gradle @@ -19,14 +19,14 @@ buildscript { repositories { - jcenter() + gradlePluginPortal() maven { url 'https://maven.google.com' } - google() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.1' + classpath 'com.android.tools.build:gradle:7.1.2' + // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } @@ -34,16 +34,15 @@ buildscript { allprojects { repositories { - jcenter() + gradlePluginPortal() maven { - url 'https://maven.google.com' + url 'https://maven.google.com' } mavenLocal() mavenCentral() - google() } } task clean(type: Delete) { delete rootProject.buildDir -} \ No newline at end of file +} diff --git a/apps/android_camera/gradle.properties b/apps/android_camera/gradle.properties index f1328bf5fd43..1add1b540993 100644 --- a/apps/android_camera/gradle.properties +++ b/apps/android_camera/gradle.properties @@ -1,4 +1,3 @@ - # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information diff --git a/apps/android_camera/models/prepare_model.py b/apps/android_camera/models/prepare_model.py index 2ea1b0a120af..959e93f8b47b 100644 --- a/apps/android_camera/models/prepare_model.py +++ b/apps/android_camera/models/prepare_model.py @@ -106,7 +106,7 @@ def main(model_str, output_path): f.write(graph) print("dumping params...") with open(output_path_str + "/" + "deploy_param.params", "wb") as f: - f.write(runtime.save_param_dict(params)) + f.write(tvm.runtime.save_param_dict(params)) print("dumping labels...") synset_url = "".join( [ diff --git a/apps/android_camera/models/requirements.txt b/apps/android_camera/models/requirements.txt new file mode 100644 index 000000000000..98aa53def46f --- /dev/null +++ b/apps/android_camera/models/requirements.txt @@ -0,0 +1,4 @@ +keras +mxnet +scipy +tensorflow \ No newline at end of file diff --git a/apps/android_deploy/app/build.gradle b/apps/android_deploy/app/build.gradle index c00528ba49fd..2949775349bb 100644 --- a/apps/android_deploy/app/build.gradle +++ b/apps/android_deploy/app/build.gradle @@ -26,8 +26,35 @@ apply from: "download-models.gradle" apply plugin: 'com.android.application' +task generateJniHeaders(type: Exec, description: 'Generate JNI Headers') { + def headerPath = "${project.projectDir}/src/main/jni" + def classPath = "${project.projectDir}/../../../jvm/core/target/*" + def filePath = "${project.projectDir}/../../../jvm/core/src/main/java/org/apache/tvm/LibInfo.java" + commandLine "javac", "-h", headerPath, "-classpath", classPath, filePath + doLast { + file("${headerPath}/org_apache_tvm_LibInfo.h").renameTo(file("${headerPath}/org_apache_tvm_native_c_api.h")) + } +} + +task copyFiles(type: Copy, description: 'Copy Sources for ndk-build') { + dependsOn "generateJniHeaders" + def ndkFilesPath = "${project.projectDir}/../../../jvm/native/src/main/native" + def srcPath = "${project.projectDir}/src/main/jni/" + + from "${ndkFilesPath}/org_apache_tvm_native_c_api.cc", "${ndkFilesPath}/jni_helper_func.h" + into srcPath +} + +task deleteLibs(type: Delete, description: "Delete Compiled Libraries") { + dependsOn "copyFiles" + def libsPath = "${project.projectDir}/src/main/libs" + delete libsPath +} + task buildJni(type: Exec, description: 'Build JNI libs') { - commandLine 'sh', 'src/main/jni/build.sh' + dependsOn "deleteLibs" + def buildPath = "${project.projectDir}/src/main/jni" + commandLine "ndk-build", "--directory", buildPath } tasks.withType(JavaCompile) { @@ -35,11 +62,10 @@ tasks.withType(JavaCompile) { } android { - compileSdkVersion 26 - buildToolsVersion "26.0.1" + compileSdkVersion 31 defaultConfig { applicationId "org.apache.tvm.android.demo" - minSdkVersion 17 + minSdkVersion 24 targetSdkVersion 26 versionCode 1 versionName "1.0" @@ -55,19 +81,24 @@ android { main { jni.srcDirs = [] jniLibs.srcDirs = ['src/main/libs'] - assets.srcDirs = [project.ext.ASSET_DIR] } } + + lintOptions { + disable "Instantiatable" // MainActivity and RPCActivity must extend android.app.Activity + disable "MissingApplicationIcon" // Should explicitly set android:icon, there is no default + disable "UnsafeNativeCodeLocation" // Shared libraries should not be placed in the res or assets directories. + } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + implementation fileTree(dir: 'libs', include: ['*.jar']) + androidTestImplementation('com.android.support.test.espresso:espresso-core:3.4.0', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:26.0.1' - compile 'com.android.support.constraint:constraint-layout:1.0.2' - compile 'com.android.support:design:26.0.1' - compile 'org.apache.tvm:tvm4j-core:0.0.1-SNAPSHOT' - testCompile 'junit:junit:4.12' -} + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.android.support.constraint:constraint-layout:2.1.3' + implementation 'com.android.support:design:28.0.0' + implementation files('../../../jvm/core/target/tvm4j-core-0.0.1-SNAPSHOT.jar') + testImplementation 'junit:junit:4.13.2' +} \ No newline at end of file diff --git a/apps/android_deploy/app/download-models.gradle b/apps/android_deploy/app/download-models.gradle index ed660e0221ee..4d1620bfd953 100644 --- a/apps/android_deploy/app/download-models.gradle +++ b/apps/android_deploy/app/download-models.gradle @@ -34,7 +34,7 @@ buildscript { jcenter() } dependencies { - classpath 'de.undercouch:gradle-download-task:3.2.0' + classpath 'de.undercouch:gradle-download-task:5.0.4' } } diff --git a/apps/android_deploy/app/src/main/AndroidManifest.xml b/apps/android_deploy/app/src/main/AndroidManifest.xml index bf3463f4d9c6..ce1b1fac4552 100644 --- a/apps/android_deploy/app/src/main/AndroidManifest.xml +++ b/apps/android_deploy/app/src/main/AndroidManifest.xml @@ -25,6 +25,7 @@ under the License. + + android:exported="true" + android:screenOrientation="unspecified"> @@ -52,6 +53,4 @@ under the License. - - diff --git a/apps/android_deploy/app/src/main/java/org/apache/tvm/android/demo/MainActivity.java b/apps/android_deploy/app/src/main/java/org/apache/tvm/android/demo/MainActivity.java index 85cc7a277b4d..6320b6aa8afd 100644 --- a/apps/android_deploy/app/src/main/java/org/apache/tvm/android/demo/MainActivity.java +++ b/apps/android_deploy/app/src/main/java/org/apache/tvm/android/demo/MainActivity.java @@ -35,9 +35,9 @@ import android.os.Environment; import android.os.SystemClock; import android.provider.MediaStore; -import android.support.v4.content.FileProvider; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; +import androidx.core.content.FileProvider; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.ImageView; @@ -51,6 +51,7 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; import java.util.Vector; import org.apache.tvm.Function; @@ -487,7 +488,7 @@ private final String getTempLibFilePath(String fileName) throws IOException { */ private File createImageFile() { // Create an image file name - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); @@ -527,6 +528,7 @@ public void onClick(DialogInterface dialog, int id) { @Override public void onRequestPermissionsResult (final int requestCode, final String[] permissions, final int[] grantResults){ + super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSIONS_REQUEST) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED diff --git a/apps/android_deploy/app/src/main/res/layout/activity_main.xml b/apps/android_deploy/app/src/main/res/layout/activity_main.xml index 0778374223d8..4b019e1fbdb3 100644 --- a/apps/android_deploy/app/src/main/res/layout/activity_main.xml +++ b/apps/android_deploy/app/src/main/res/layout/activity_main.xml @@ -19,29 +19,28 @@ specific language governing permissions and limitations under the License. --> - - - + android:layout_height="match_parent" + tools:context="org.apache.tvm.tvmrpc.MainActivity"> - + android:theme="@style/AppTheme.AppBarOverlay"> - + - + - + + diff --git a/apps/android_deploy/app/src/main/res/layout/content_main.xml b/apps/android_deploy/app/src/main/res/layout/content_main.xml index aa1b0ea72b3e..6bf3c19f7f81 100644 --- a/apps/android_deploy/app/src/main/res/layout/content_main.xml +++ b/apps/android_deploy/app/src/main/res/layout/content_main.xml @@ -35,10 +35,10 @@ under the License.