- Home /
problem accessing android jar in unity
hello developers!
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.
I know there are some threads in unity forums and stackoverflow, I tried to find a solution in almost all the links I could find but no luck!
Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),
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);
}
}
Below is my unity C# 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<string>("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<AndroidJavaObject>("setFlashOn");
}
}
}
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.
Below is the AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newflash"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name">
<activity
android:name="com.example.newflash.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Below is the warning which unity shows in console with the above xml included during Build & Run,
Unable to find unity activity in manifest. You need to make sure orientation attribut is set to sensor manually.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
It would be great if someone could help me out. Any help is much appreciated.
Thank you in advance!
Jainam
Answer by stevethorne · Dec 17, 2013 at 01:45 PM
Your activity in Java has to be an activity that extends "UnityPlayerActivity", not your conventional "Activity". That is, com.unity3d.player.UnityPlayerActivity. You can do a search here for UnityPlayerActivity to find out more about this.
hi Steve! Thanks for the link! Following all the steps on the link, I still have the same results, string is called but flashlight doesn't work. I don't have experience in Android coding, so am confused about .so files. I have not generated .so files.. If *.so files are required how did the string function get called without them?
Hi Steve! Your link was really helpful. I found my mistake in the manifest file. now it's working. thanks!
How did you get it? I'm trying to reproduce your sample code, but when I press the torch button, doesn't occur anything. I extends from UnityPlayerActivity the Launcher Activity, it was what I changed from $$anonymous$$anifest. I'm working on Unity 4.3.4 and Nexus Tablet.
Answer by jainam · Mar 20, 2014 at 03:50 PM
I asked this question on the forum as my code didn't work. So if you are using the above code, it won't work ;) Here is the working code...hope it helps :)
Create the package with the name "com.unity3d.player" in Android SDK.
Below is android MainActivity.java (which I converted into a jar file from eclipse and copied in unity project, ~Assets/Plugins/Android/bin/),
package com.unity3d.player;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.WindowManager;
import android.content.Context;
import android.content.pm.PackageManager;
public class MainActivity extends UnityPlayerActivity {
private static Camera camera;
private static Parameters parameters;
static boolean s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Context context = this;
PackageManager packageManager = context.getPackageManager();
// if device support camera?
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
//yes
s = true;
}else{
//no
s = false;
}
}
public static String dummyString()
{
return "dummy string";
}
public static String sendFlashStatus()
{
if(s==true)
{
return "has flash";
}
else
{
return "no flash";
}
}
public static String setFlashOn()
{
if (camera == null)
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
return "";
}
public static String setFlashOff()
{
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
return "";
}
}
Below is my unity C# code,
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
public class testJar : MonoBehaviour
{
AndroidJavaClass testClass;
string toggleMethod, somestring, hasFlash;
bool callOnce;
void Start ()
{
testClass = new AndroidJavaClass("com.unity3d.player.MainActivity");
hasFlash = testClass.CallStatic<String>("sendFlashStatus");
toggleMethod = "setFlashOff";
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
}
void OnGUI ()
{
somestring = testClass.CallStatic<string>("dummyString");
GUI.Label (new Rect (20, 20, 100, 60), somestring);
GUI.Label (new Rect (150, 20, 100, 60), ""+hasFlash);
if(GUI.Button(new Rect (Screen.width/2-50, Screen.height-120, 100, 60), "Toggle torch"))
{
callOnce = true;
if(toggleMethod == "setFlashOff")
{
toggleMethod = "setFlashOn";
}
else
{
toggleMethod = "setFlashOff";
}
}
if(callOnce)
{
testClass.CallStatic<AndroidJavaObject>(""+toggleMethod);
callOnce = false;
}
}
}
Below is the AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.MainActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks!!! I've tested on two devices and it works on one. $$anonymous$$y next step, launch the camera android device.
@ jainam When I use the extends UnityPlayerActivity on Android Studio, It doesn't recognize it and it has an error. Is there something I need to reference in my project? Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613124259im_/https://answers.unity.com/themes/thub/images/avi.jpg)