[Android] Something's went wrong with pytorch_android-1.4.0-SNAPSHOT

zetyquickly: <p>Tried to set up HelloWorldApp (<a href="https://github.com/pytorch/android-demo-app" rel="nofollow noopener">https://github.com/pytorch/android-demo-app</a>) with the most latest version of PyTorch built from source.<br>
After a little bit of struggling with gradle build I’ve ended up with crash on devices.</p>
<p>Here is the <code>build.gradle :app</code></p>
<pre><code class="lang-auto">apply plugin: 'com.android.application'

repositories {
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

android {
    packagingOptions {
        pickFirst "**/libfbjni.so"
    }
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "org.pytorch.helloworld"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    } 
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    // implementation 'org.pytorch:pytorch_android:1.3.0'
    // implementation 'org.pytorch:pytorch_android_torchvision:1.3.0'
    implementation(name:'pytorch_android-1.4.0-SNAPSHOT', ext:'aar')
    implementation(name:'pytorch_android_torchvision-1.4.0-SNAPSHOT', ext:'aar')
    implementation(name:'pytorch_android_fbjni-1.4.0-SNAPSHOT', ext:'aar')
}
</code></pre>
<p>Note: <code>.aar</code> libs are present</p>
<p>Crash stacktrace</p>
<pre><code class="lang-auto">2019-10-04 22:31:16.059 16203-16203/org.pytorch.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.pytorch.helloworld, PID: 16203
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/facebook/soloader/nativeloader/NativeLoader;
        at org.pytorch.Module.&lt;init&gt;(Module.java:27)
        at org.pytorch.Module.load(Module.java:23)
        at org.pytorch.helloworld.MainActivity.onCreate(MainActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:8068)
        at android.app.Activity.performCreate(Activity.java:8056)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3757)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3955)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2392)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8147)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.soloader.nativeloader.NativeLoader" on path: DexPathList[[zip file "/data/app/org.pytorch.helloworld-eCefKQPfLWFqNPNZZUphIA==/base.apk"],nativeLibraryDirectories=[/data/app/org.pytorch.helloworld-eCefKQPfLWFqNPNZZUphIA==/lib/arm64, /data/app/org.pytorch.helloworld-eCefKQPfLWFqNPNZZUphIA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64, /hw_product/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at org.pytorch.Module.&lt;init&gt;(Module.java:27) 
        at org.pytorch.Module.load(Module.java:23) 
        at org.pytorch.helloworld.MainActivity.onCreate(MainActivity.java:39) 
        at android.app.Activity.performCreate(Activity.java:8068) 
        at android.app.Activity.performCreate(Activity.java:8056) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3757) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3955) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2392) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8147) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100) 
</code></pre>
<p>May be we should not rely on unstable version but <code>module = Module.load()</code> is a core feature <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>

zetyquickly: <p>There is still an issue.<br>
Here’s what I’ve noticed except an issue <code>pytorch_android_torchvision-1.4.0-SNAPSHOT.aar</code> works with <code>org.pytorch:pytorch_android:1.3.0</code> from remote repo.<br>
The problem occurs only with <code>pytorch_android-1.4.0-SNAPSHOT.aar</code></p>

IvanKobzarev: <p>Hello Emil,</p>
<p>If you have not changed the source - you can use our nightly builds that are published every night to nexus sonatype maven repo.</p>
<p>To use it:</p>
<p>repositories {<br>
…<br>
maven {<br>
url “<a href="https://oss.sonatype.org/content/repositories/snapshots" rel="nofollow noopener">https://oss.sonatype.org/content/repositories/snapshots</a>”<br>
}<br>
}</p>
<p>dependencies {<br>
implementation ‘androidx.appcompat:appcompat:1.1.0’<br>
implementation ‘org.pytorch:pytorch_android:1.4.0-SNAPSHOT’<br>
implementation ‘org.pytorch:pytorch_android_torchvision:1.4.0-SNAPSHOT’<br>
}</p>
<p>I rechecked HelloWorldApp with the latest published 1.4.0-SNAPSHOT - it works fine.</p>
<ol start="2">
<li>About your failure:</li>
</ol>
<p>It looks like that in your aar you do not have dependency: ( com.facebook.soloader:nativeloader )<br>
which was added after 1.3.0 release:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/android/pytorch_android/build.gradle#L59" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/android/pytorch_android/build.gradle#L59" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/master/android/pytorch_android/build.gradle#L59</a></h4>
<pre class="onebox"><code class="lang-gradle"><ol class="start lines" start="49" style="counter-reset: li-counter 48 ;">
<li>
</li>
<li>    useLibrary 'android.test.runner'</li>
<li>    useLibrary 'android.test.base'</li>
<li>    useLibrary 'android.test.mock'</li>
<li>}</li>
<li>
</li>
<li>dependencies {</li>
<li>    api project(':fbjni')</li>
<li>
</li>
<li>    implementation 'com.android.support:appcompat-v7:28.0.0'</li>
<li class="selected">    implementation 'com.facebook.soloader:nativeloader:0.8.0'</li>
<li>
</li>
<li>    testImplementation 'junit:junit:' + rootProject.junitVersion</li>
<li>    testImplementation 'androidx.test:core:' + rootProject.coreVersion</li>
<li>
</li>
<li>    androidTestImplementation 'junit:junit:' + rootProject.junitVersion</li>
<li>    androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion</li>
<li>    androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion</li>
<li>    androidTestImplementation 'androidx.test:rules:' + rootProject.rulesVersion</li>
<li>    androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion</li>
<li>}</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
Please add it manually:<br>
implementation ‘com.facebook.soloader:nativeloader:0.8.0’</p>
<p>In case of using maven dependency gradle will parse pom file that has this dependency and add it automatically. For manual aar files we have to add all transitive dependencies manually.</p>

hurricane013: <p>hello Emil,</p>
<p>I am having trouble while trying to deploy keypoint_rcnn in android. Can you please explain me how to build latest pytorch mobile using source? I read documentation but its not clear enough for me. Can you please help me out?</p>

zetyquickly: <p>The latest source is built daily so I recommend you to try to follow the solution that Ivan has provided.<br>
This snapshot version is the most recent. Add the repository and pytorch mobile library will be downloaded automatically</p>

hurricane013: <p>I have tried with adding dependency pytorch 1.4 snapshot to export keypoint_rcnn in android. But now build fails without showing any errors.  what should be exact problem?</p>

zetyquickly: <p>At first I think you need to make sure that your TorchScript model works well in Python and it can be found in the right place on your phone.</p>
<p>At the second point I think there should be an errors if the build crashes</p>

hurricane013: <p>Yes my torchscript  model works well on python. and there is no error after failing of build. By the way, I came to know that they haven’t added support for keypoint_rcnn yet. Take a look at this : <a href="https://github.com/pytorch/vision/issues/1943" rel="nofollow noopener">https://github.com/pytorch/vision/issues/1943</a></p>