title: accessing android jar in unity3d
body: <p>I have an android project for camera flashlight, which when deployed from eclipse works fine. I am trying to access the flashlight function from my C# code in unity3d but it doesn't work. To verify if I am calling the android method correctly, I created a string function in the same activity and it is returning the string correctly. I am not familiar to native android coding. It would be great if you could have a look at the code and help me out.</p>
  
  <p>I know there are some threads in unity forum and stackoverflow explaining the same, I tried to find a solution on these threads but no luck! So, posted this thread..</p>
  
  <p>Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),</p>
  
  <pre><code>package com.example.newflash;
  
  import android.hardware.Camera;
  import android.hardware.Camera.Parameters;
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Window;
  import android.view.WindowManager;
  
  public class MainActivity extends Activity {
  
      private static Camera camera;
      private static Parameters parameters;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          //Hide the window title.
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      }
  
      public static String dummyString()
      {
          return "dummy string";
      }
  
      public static void setFlashOn()
      {
          if (camera == null)
              camera = Camera.open();
          parameters = camera.getParameters();
          parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
          camera.setParameters(parameters);
      }
  
      public static void setFlashOff()
      {
          parameters = camera.getParameters();
          parameters.setFlashMode(Parameters.FLASH_MODE_OFF);     
          camera.setParameters(parameters);
      }
  }
  </code></pre>
  
  <p>Below is my unity C# code,</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using System.Runtime.InteropServices;
  using System;
  
  public class testJar : MonoBehaviour
  {
      bool torchon;
      AndroidJavaClass testClasslight;
  
      void Start ()
      {
          torchon = false;
      }
  
      void OnGUI ()
      {
          string teststring = "empty";
  
          AndroidJavaClass testClass = new AndroidJavaClass("com.example.glassplaces.MainActivity");
          teststring = testClass.CallStatic&lt;string&gt;("dummyString");
  
          GUI.Label (new Rect (20, 20, 100, 60), teststring);
  
          if(GUI.Button(new Rect (20, 100, 100, 60), "ON"))
          {
              torchon = true;
          }
  
          if(torchon == true)
          {
              GUI.Label(new Rect(200, 20,100,60), "torch ON");
              testClass.CallStatic&lt;AndroidJavaObject&gt;("setFlashOn");
          }
      }
  }
  </code></pre>
  
  <p>The permissions to access camera in AndroidManifest.xml when added, the app doesn't start at all. On excluding the xml file from the project, the "dummyString" method still returns the string.</p>
  
  <p>Below is the AndroidManifest.xml,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.newflash"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
      &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/app_icon"
          android:label="@string/app_name"&gt;
          &lt;activity
              android:name="com.example.newflash.MainActivity"
              android:label="@string/app_name" &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;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Below is the warning which unity shows in console with the above xml included during Build &amp; Run,</p>
  
  <pre><code>Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
  UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
  </code></pre>
  
  <p>It would be great if someone could help me out. Any help is much appreciated.</p>
  
  <p>Thank you in advance!</p>