<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">PyTorch Java Native Interface is conflicting with React Native one. (they both use fbjni with a slightly different version).<br>
This cause issue during the android build in an React Native project:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="java.lang.RuntimeException: Duplicate class com.facebook.jni.CppException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.CppSystemErrorException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$1 found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$Destructor found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$DestructorList found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$DestructorStack found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$Terminus found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridClassBase found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridData found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridData$Destructor found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.IteratorHelper found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.MapIteratorHelper found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.NativeRunnable found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.ThreadScopeSupport found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.UnknownCppException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
"><pre class="notranslate"><code class="notranslate">java.lang.RuntimeException: Duplicate class com.facebook.jni.CppException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.CppSystemErrorException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$1 found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$Destructor found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$DestructorList found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$DestructorStack found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.DestructorThread$Terminus found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridClassBase found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridData found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.HybridData$Destructor found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.IteratorHelper found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.MapIteratorHelper found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.NativeRunnable found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.ThreadScopeSupport found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)
  Duplicate class com.facebook.jni.UnknownCppException found in modules fbjni-java-only-0.0.3.jar (com.facebook.fbjni:fbjni-java-only:0.0.3) and pytorch_android_fbjni-1.4.0-runtime.jar (org.pytorch:pytorch_android_fbjni:1.4.0)

</code></pre></div>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Create a new React Native project using the CLI: <code class="notranslate">npx react-native init AwesomeProject</code></li>
<li>Add the PyTorch android dependencies:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="        implementation 'org.pytorch:pytorch_android:1.4.0'
        implementation 'org.pytorch:pytorch_android_torchvision:1.4.0'"><pre class="notranslate"><code class="notranslate">        implementation 'org.pytorch:pytorch_android:1.4.0'
        implementation 'org.pytorch:pytorch_android_torchvision:1.4.0'
</code></pre></div>
<ol start="3" dir="auto">
<li>Change the <code class="notranslate">minSdkVersion</code> to 21 in the root android/build.gradle</li>
<li>Sync gradle and build the android app</li>
</ol>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">The Application should be able to build without having "duplicate class" issue and start on the device</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="➜ python collect_env.py
Collecting environment information...
PyTorch version: N/A - 1.4.0
Is debug build: N/A
CUDA used to build PyTorch: N/A

OS: Mac OSX 10.15.5 (x86_64)
GCC version: Could not collect
Clang version: 11.0.3 (clang-1103.0.32.62)
CMake version: Could not collect

Python version: 2.7 (64-bit runtime)
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy==1.8.0rc1
[conda] Could not collect"><pre class="notranslate"><code class="notranslate">➜ python collect_env.py
Collecting environment information...
PyTorch version: N/A - 1.4.0
Is debug build: N/A
CUDA used to build PyTorch: N/A

OS: Mac OSX 10.15.5 (x86_64)
GCC version: Could not collect
Clang version: 11.0.3 (clang-1103.0.32.62)
CMake version: Could not collect

Python version: 2.7 (64-bit runtime)
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy==1.8.0rc1
[conda] Could not collect
</code></pre></div>
<h2 dir="auto">Additional context</h2>
<p dir="auto">There were some talk on the PyTorch forum <a href="https://discuss.pytorch.org/t/pytorch-not-compatible-with-react-native-library/73841/6" rel="nofollow">here</a> saying that someone (from PyTorch team?) was discussing with the RN Team, but there is no update since March 20 AND the issue come after the reported issue here.</p>
<p dir="auto">This failure occurs when compilation OOMs, but the OOM exit code is not propagated. "Resume build" will not fix errors like this, as resume build will reuse the (corrupted) build product.</p>
<hr>
<p dir="auto">I got this durably on a PR of mine (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="382859312" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/14247" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/14247/hovercard" href="https://github.com/pytorch/pytorch/pull/14247">#14247</a> commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/a6435488130ef94c5b6c80c5cb10d2a1cf6ff16a/hovercard" href="https://github.com/pytorch/pytorch/commit/a6435488130ef94c5b6c80c5cb10d2a1cf6ff16a"><tt>a643548</tt></a>); this test doesn't seem to fail on master all the time, but I did find the failure on one other PR: <a href="https://ci.pytorch.org/jenkins/job/pytorch-builds/job/py2-clang7-rocmdeb-ubuntu16.04-test/3885/console" rel="nofollow">https://ci.pytorch.org/jenkins/job/pytorch-builds/job/py2-clang7-rocmdeb-ubuntu16.04-test/3885/console</a> for <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="385396349" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/14482" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/14482/hovercard" href="https://github.com/pytorch/pytorch/pull/14482">#14482</a> (most other failures are the known SIGIOT problem)</p>
<p dir="auto">Full error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="19:08:19 test_ByteTensor_mode (__main__.TestCuda) ... /usr/bin/python: symbol lookup error: /var/lib/jenkins/.local/lib/python2.7/site-packages/torch/lib/libcaffe2_hip.so: undefined symbol: THCudaByteTensor_mode
19:08:19 Traceback (most recent call last):
19:08:19   File &quot;test/run_test.py&quot;, line 431, in &lt;module&gt;
19:08:19     main()
19:08:19   File &quot;test/run_test.py&quot;, line 423, in main
19:08:19     raise RuntimeError(message)
19:08:19 RuntimeError: test_cuda failed!"><pre class="notranslate"><code class="notranslate">19:08:19 test_ByteTensor_mode (__main__.TestCuda) ... /usr/bin/python: symbol lookup error: /var/lib/jenkins/.local/lib/python2.7/site-packages/torch/lib/libcaffe2_hip.so: undefined symbol: THCudaByteTensor_mode
19:08:19 Traceback (most recent call last):
19:08:19   File "test/run_test.py", line 431, in &lt;module&gt;
19:08:19     main()
19:08:19   File "test/run_test.py", line 423, in main
19:08:19     raise RuntimeError(message)
19:08:19 RuntimeError: test_cuda failed!
</code></pre></div>
<p dir="auto">log: <a href="https://ci.pytorch.org/jenkins/job/pytorch-builds/job/py2-clang7-rocmdeb-ubuntu16.04-test/3874/console" rel="nofollow">https://ci.pytorch.org/jenkins/job/pytorch-builds/job/py2-clang7-rocmdeb-ubuntu16.04-test/3874/console</a></p>