Search Unity

Activity context in Android Plugin

Discussion in 'Android' started by Bestlis, Mar 23, 2015.

  1. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    Hello everybody!

    I try to show android activity in Unity.

    Here is my code:

    package com.bestlis.advertismentplugin;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;

    public class MainActivity extends Activity {

    @override
    protected void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    }

    public static void ShowAdvert(Activity activity)
    {
    Intent intent = new Intent(activity, MainActivity.class);
    activity.startActivity(intent);
    }
    }

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:eek:rientation="vertical" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    Unity

    static AndroidJavaClass androidJC;
    static AndroidJavaObject androidJO;

    public static void ShowAdvertisment()
    {
    androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    androidJO = androidJC.GetStatic<AndroidJavaObject>("currentActivity");

    var jc = new AndroidJavaClass("com.bestlis.advertismentplugin.MainActivity");
    jc.CallStatic("ShowAdvert", androidJO);
    }



    It works well, but there is only black screen.

    If I try to set main_activity with setContentView(R.layout.activity_main) I get error.

    E/AndroidRuntime(23542): FATAL EXCEPTION: main
    E/AndroidRuntime(23542): java.lang.Error: FATAL EXCEPTION [main]
    E/AndroidRuntime(23542): Unity version : 4.5.5f1
    E/AndroidRuntime(23542): Device model : LENOVO Lenovo A760
    E/AndroidRuntime(23542): Device fingerprint: Lenovo/audi/audi:4.1.2/JZO54K/A760_1_S_2_001_0133_130828:user/release-keys
    E/AndroidRuntime(23542):
    E/AndroidRuntime(23542): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bestlis.first/com.bestlis.advertismentplugin.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030018
    E/AndroidRuntime(23542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2067)
    E/AndroidRuntime(23542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
    E/AndroidRuntime(23542): at android.app.ActivityThread.access$600(ActivityThread.java:133)
    E/AndroidRuntime(23542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)

    How can I show my main_activity.xml?

    Thanks!
     
    Last edited: Mar 23, 2015
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    A few things to note (some were not mentioned in the original post):

    1. How are you adding your new activity to the Unity project? Are you compiling it as a .jar and adding that somehow?
    2. Did you declare your activity in the AndroidManifest.xml ? (you should include one in your Plugins/Android folder
    3. Your activity should derive from UnityActivity, and not Activity
    Besides that, your C# code should access the main activity, and call the static method on it (after all, if you configure your activity to be the main one, you can directly call the method on it), unless you're trying to show another activity (not really sure from the code).
     
  3. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    1. Yes, I compiled it into .jar file.
    2. Yes, I added activity into manifest file: <activity android:name = "com.bestlis.advertismentplugin.MainActivity" />
    3. UnityActivity or UnityPlayerActivity?
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    My bad, I meant UnityPlayerActivity

    See more info here: http://docs.unity3d.com/Manual/PluginsForAndroid.html
     
  5. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    There is nothing in that link what I need, but thanks.
    I seen that article, but it didn't help me.

    Error gone when i change to UnityPlayerActivity, but no activity showed.

    Does anybody have work example?
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    That link shows how to derive from Unity's activity, how to define the AndroidManifest, etc.

    We are using a custom activity without any issue.

    Steps we've done:

    1. Create a .jar containing our new activity (derived from UnityPlayerActivity)
    2. Register this in the AndroidManifest.xml
    3. Copy the .jar to Assets/Plugins/Android

    The first test after performing these steps would be the game actually launching correctly.

    Note that any resources you're trying to access will not be included in the .JAR (the error you posted seems to be related to some missing resources, e.g: Android UI layouts, etc).
     
  7. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    Sorry, I fortgot to execute method.
    After change Activity to UnityPlayerActivity error remained.
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    What else do you have in your Assets/Plugins/Android and in your manifest?

    It is looking for some resource that does not exist in your Unity project.
     
  9. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    http://prntscr.com/6kd0l1

    I have only jar file and AndroidManifest.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bestlis.first"
    android:installLocation="preferExternal"
    android:theme="@android:style/Theme.NoTitleBar"
    android:versionCode="1"
    android:versionName="1.0">
    <supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:xlargeScreens="true"
    android:anyDensity="true"/>

    <application
    android:icon="@drawable/app_icon"
    android:label="@String/app_name"
    android:debuggable="true">
    <activity android:name="com.unity3d.player.UnityPlayerActivity"
    android:launchMode="singleTask"
    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>
    <activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
    android:launchMode="singleTask"
    android:label="@String/app_name"
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    </activity>
    <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
    android:launchMode="singleTask"
    android:label="@String/app_name"
    android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    <meta-data android:name="android.app.lib_name" android:value="unity" />
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
    </activity>

    <activity android:name = "com.bestlis.advertismentplugin.MainActivity" />
    </application>
    </manifest>
     
  10. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    JARs don't contain any resources, you can peek inside to see... If it contains, I don't think Android knows how to get it (the resources are being built by the android build process, it doesn't look for any resources inside the jar).

    Also, there are issues with how you declare things in the manifest:

    * At the top, the package you list is not the one containing your MainActivity.
    * The UnityPlayerActivity is marked as LAUNCHER. This should be your MainActivity.

    You should define it something like this:

    <activity android:name="com.bestlis.advertismentplugin.MainActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@String/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
    <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
     
  12. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    Resources included http://prntscr.com/6kd800

    When I changed launch activity, I get error while compliling:

    Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details.
    C:\Program Files\Android-SDK-windows\sdk\build-tools\21.1.2\aapt.exe package --auto-add-overlay -v -f -m -J gen -M AndroidManifest.xml -S "res" -I "C:/Program Files/Android-SDK-windows/sdk/platforms/android-21\android.jar" -F bin/resources.ap_

    stderr[
    AndroidManifest.xml:17: error: Error: No resource found that matches the given name (at 'label' with value '@String/app_name').
     
  13. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    We never had anything like that, what Unity version are you using ?
     
  14. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    I found solution http://students.ceid.upatras.gr/~be...1/27_create_unity_vuforia_android_plugin.html

    Man wrote:

    Note 1: We create the layout programmatically because otherwise i get Resource not found exception. This needs to be checked but i haven’t done it yet.

    So he had the same error with layout file...

    Does anybody know, how to include layout file into jar?

    Note: my launch activity com.unity3d.player.UnityPlayerActivity
     
    Last edited: Mar 23, 2015
  15. Bestlis

    Bestlis

    Joined:
    Jan 15, 2014
    Posts:
    32
    4.5.5.f1