title: Activity class not found even though the class appears in dex disassembly
body: <p>I'm developing an Android app (let's call it <em>AppName</em>) in Unity 2017.3 Beta 1. I need a custom Activity to run some Java code on Startup. I have the code for the <code>AppNameActivity</code> in a separate project which uses <code>gradle</code> to package the Activity into the <code>AppName.jar</code> file which is then dropped into Unity's <code>AppName/Assets/Plugins/Android</code> folder, along with a custom <code>AndroidManifest.xml</code> which referencet it. I uses Unity's <em>Build and Run</em> feature (Build System: Gradle, Development Build).</p>
  
  <p>When I start the app on my Android device, it crashes instantly. It seems as if the Activity class was missing, but on closer inspection, it is actually there.</p>
  
  <p>Via <code>adb logcat</code> I can obtain this error:</p>
  
  <pre><code>V/ActivityThread(24831): Handling launch of ActivityRecord{2609126c token=android.os.BinderProxy@2101f035 {de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}}
  D/OpenGLRenderer(23986): Flushing caches (mode 0)
  D/OpenGLRenderer(23986): Flushing caches (mode 0)
  I/art     (24831): Rejecting re-init on previously-failed class java.lang.Class&lt;de.companyname.appname.rebuild.AppNameActivity&gt;
  D/AndroidRuntime(24831): Shutting down VM
  E/AndroidRuntime(24831): FATAL EXCEPTION: main
  E/AndroidRuntime(24831): Process: de.companyname.appname.rebuild, PID: 24831
  E/AndroidRuntime(24831): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}: java.lang.ClassNotFoundException: Didn't find class "de.companyname.appname.rebuild.AppNameActivity" on path: DexPathList[[zip file "/data/app/de.companyname.appname.rebuild-2/base.apk"],nativeLibraryDirectories=[/data/app/de.companyname.appname.rebuild-2/lib/arm, /vendor/lib, /system/lib]]
  E/AndroidRuntime(24831):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
  E/AndroidRuntime(24831):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2532)
  E/AndroidRuntime(24831):    at android.app.ActivityThread.access$900(ActivityThread.java:169)
  E/AndroidRuntime(24831):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
  E/AndroidRuntime(24831):    at android.os.Handler.dispatchMessage(Handler.java:111)
  E/AndroidRuntime(24831):    at android.os.Looper.loop(Looper.java:194)
  E/AndroidRuntime(24831):    at android.app.ActivityThread.main(ActivityThread.java:5562)
  E/AndroidRuntime(24831):    at java{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}.lang.reflect.Method.invoke(Native Method)
  E/AndroidRuntime(24831):    at java.lang.reflect.Method.invoke(Method.java:372)
  E/AndroidRuntime(24831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
  E/AndroidRuntime(24831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
  E/AndroidRuntime(24831): Caused by: java.lang.ClassNotFoundException: Didn't find class "de.companyname.appname.rebuild.AppNameActivity" on path: DexPathList[[zip file "/data/app/de.companyname.appname.rebuild-2/base.apk"],nativeLibraryDirectories=[/data/app/de.companyname.appname.rebuild-2/lib/arm, /vendor/lib, /system/lib]]
  E/AndroidRuntime(24831):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  E/AndroidRuntime(24831):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  E/AndroidRuntime(24831):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  E/AndroidRuntime(24831):    at android.app.Instrumentation.newActivity(Instrumentation.java:1070)
  E/AndroidRuntime(24831):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2371)
  E/AndroidRuntime(24831):    ... 10 more
  E/AndroidRuntime(24831):    Suppressed: java.lang.NoClassDefFoundError: de.companyname.appname.rebuild.AppNameActivity
  E/AndroidRuntime(24831):        at dalvik.system.DexFile.defineClassNative(Native Method)
  E/AndroidRuntime(24831):        at dalvik.system.DexFile.defineClass(DexFile.java:226)
  E/AndroidRuntime(24831):        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
  E/AndroidRuntime(24831):        at dalvik.system.DexPathList.findClass(DexPathList.java:321)
  E/AndroidRuntime(24831):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
  E/AndroidRuntime(24831):        ... 14 more
  E/AndroidRuntime(24831):    Suppressed: java.lang.ClassNotFoundException: de.companyname.appname.rebuild.AppNameActivity
  E/AndroidRuntime(24831):        at java.lang.Class.classForName(Native Method)
  E/AndroidRuntime(24831):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
  E/AndroidRuntime(24831):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
  E/AndroidRuntime(24831):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  E/AndroidRuntime(24831):        ... 13 more
  E/AndroidRuntime(24831):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
  </code></pre>
  
  <p>The relevant part of my <code>AndroidManifest.xml</code>:</p>
  
  <pre><code>&lt;application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:isGame="false" android:banner="@drawable/app_banner"&gt;
      &lt;activity android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection" android:name="de.companyname.appname.rebuild.AppNameActivity"&gt;
        &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MAIN" /&gt;
          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
        &lt;meta-data android:name="unityplayer.UnityActivity" android:value="true" /&gt;
      &lt;/activity&gt;
      (...)
    &lt;/application&gt;
  </code></pre>
  
  <p>Here's the output of <code>dexdump</code>, executed on the Android device via <code>adb shell</code> with the argument <code>/data/app/de.companyname.appname.rebuild-2/base.apk</code>. </p>
  
  <pre><code>Processing './base.apk'...
  Opened './base.apk', DEX version '035'
  (...)
  Class #69            -
    Class descriptor  : 'Lde/companyname/appname/rebuild/AppNameActivity;'
    Access flags      : 0x0001 (PUBLIC)
    Superclass        : 'Lde/companyname/appname/rebuild/UnityPlayerActivity;'
    Interfaces        -
    Static fields     -
      #0              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : 'ACTION_USB_PERMISSION'
        type          : 'Ljava/lang/String;'
        access        : 0x001a (PRIVATE STATIC FINAL)
      #1              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : 'TAG'
        type          : 'Ljava/lang/String;'
        access        : 0x001a (PRIVATE STATIC FINAL)
    Instance fields   -
      #0              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : 'mUsbManager'
        type          : 'Landroid/hardware/usb/UsbManager;'
        access        : 0x0002 (PRIVATE)
      #1              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : 'mUsbReceiver'
        type          : 'Landroid/content/BroadcastReceiver;'
        access        : 0x0012 (PRIVATE FINAL)
      #2              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : 'validProductNames'
        type          : 'Ljava/util/Collection;'
        access        : 0x0002 (PRIVATE)
    Direct methods    -
      #0              : (in Lde/companyname/appname/rebuild/AppNameActivity;)
        name          : '&lt;init&gt;'
        type          : '()V'
        access        : 0x10001 (PUBLIC CONSTRUCTOR)
        code          -
        registers     : 4
        ins           : 1
        outs          : 2
        insns size    : 32 16-bit code units
        catches       : (none)
        positions     : 
          0x0000 line=27
          0x0003 line=22
          0x000a line=25
          0x0018 line=28
          0x001f line=29
        locals        : 
          0x0000 - 0x0020 reg=3 this Lde/companyname/appname/rebuild/AppNameActivity; 
  (...)
  </code></pre>
  
  <p>I also used the tool <code>AndroidSdk/tools/bin/apkanalyzer dex packages ~/src/appname/UnityExport.apk</code> to get a disassembly on my dev machin, which yields:</p>
  
  <pre><code>sh: 0: getcwd() failed: No such file or directory
  P d 465 944 66105   &lt;TOTAL&gt;
  P d 420 454 49362   com
  (...)
  C d 8   10  1067    de.companyname.appname.rebuild.AppNameActivity
  M d 1   1   119     de.companyname.appname.rebuild.AppNameActivity &lt;init&gt;()
  (...)
  </code></pre>
  
  <p>So I'm rather sure that the class <code>de.companyname.appname.rebuild.AppNameActivity</code> is present on the device.</p>
  
  <p><strong>Update:</strong>
  I was worried about the package being mentioned twice in a row in the error log: <code>{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}</code> so I changed the value in the manifest from <code>android:name="de.companyname.appname.rebuild.AppNameActivity"</code> to <code>android:name=".AppNameActivity"</code>. Still, I get <em>exactly</em> the same error message.</p>