I'm trying to create an Android plugin for my Unity game. I have watched a lot of tutorials (most of them are outdated based on eclipse) and have read the documentation also. I'm using Unity 2019.3.0f6. I want to extend my main activity in Android Studio project with UnityPlayerActivity
I don't understand what does the ending lines mean "Locate the file, and add classes.jar to the classpath Unity uses to compile the new Activity. Compile your Activity source file and package it into a JAR or AAR package, then copy it into your Project folder"
I understand UnityPlayerActivity does not exist in the classes.jar at PlaybackEngines/AndroidPlayer/Variations/mono or il2cpp/Development or Release/Classes/ and so I can't import com.unity3d.player.UnityPlayerActivity; I can only import UnityPlayer and IUnityPlayerLifecycleEvents. I am also interested to understand what is the UnityPlayer class and IUnityPlayerLifecycleEvents in this context.
But the UnityPlayerActivity.java is available at C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player
How do I add it in the Unity3d library or classes.jar. Or even import it to extend my mainactivity. I don't understand what am I doing wrong here.
I am using Android Studio, although I am new to it.
Don't worry, make android plugins for unity could be a little bit messy at the start, even more if you don't have any experience with Android!
Some tips:
Project directory structure should be: Assets/Plugins/Android (it's
important, I've been struggling for this stupidity here)
Insert classes.jar in AndroidStudio project in app/libs.
Insert external dependencies (if you are using it) like
"support-v4-24.1.1" into Android/libs
To create plugins on AndroidStudio you need to create a library (this steps is to create it from an activity):
On graddle remove ID line
On the same file, change .implementation to .library
To recompile the plugin do the follow:
Rebuild AndroidStudio solution
Go to AndroidStudio solution...app\build\outputs\aar get the .aar
file
Copy and paste it, change the extension from .aar to .zip or .rar
Open the modified file and extract 2 items:
classes.jar (this is another classes.jar, not the same stored in app/libs in your AndroidStudio project)
AndroidManifest.xml
Copy those files into Unity project in Assets/Plugins/Android
(remember, project directory structure is important!)
You can download the classes.jar file (the first one) from my Utility_Repo or from the path you name it D:\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes\classes.jar.
I've got into the same situation, and after some search I've found that you should take that class from elsewhere and simply copy it into your project. On my machine the class is in "...path-to-unity-installation...\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player".
I think that the new approach that sidesteps using UnityPlayerActivity is totally worth attention, though.
There is a really great tutorial and accompanying youtube video on how to make native Android plugins and incorporate in Unity 3D.
http://www.cwgtech.com/using-android-webview-to-display-a-webpage-on-top-of-the-unity-app-view/
Create a new Module, eg: name it "UnityActivity".
Add classes.jar which can be found in unity install folder as Dependences in the method of "CompileOnly"
Add the Source Code of "UnityPlayerActivity" in to module of UnityActivity.
Add the new Module as Dependences to "Your Module" in the method of "CompileOnly"
Now you can create your CustomActivity extends from UnityPlayerActivity. and build into *.aar.
The idea is to mock the dependency com.unity3d.player. In Android Studio:
Create a new project (MyUnityPlayerActivity) with "no activity".
From File/New/New Module, create new Module (Player) with package name com.unity3d.player.
Apply these for "both" modules (app and player):
Clean-up any non-library references (icons, themes,...etc.) from AndroidManifest.xml.
Delete everything under the folder res except res/values/strings.xml.
In build.gradle of the module:
Replace id 'com.android.application' with id 'com.android.library'.
Delete the line applicationId "...".
Delete all lines related with tests.
Delete all dependencies.
Add local Unity installation dependency as "compile only" (so that it is not included in build).
dependencies {
compileOnly files('C:/Program Files/Unity/Hub/Editor/2019.4.32f1/Editor/Data/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar)
}
In module player:
Copy UnityPlayerActivity.java from C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player\ to /player/java/com.unity3d.player.
In module app:
In build.gradle of module MyUnityPlayerActivity add compile only dependency to the module player we just created.
dependencies {
compileOnly files('C:/Program Files/Unity/Hub/Editor/2019.4.32f1/Editor/Data/PlaybackEngines/AndroidPlayer/Variations/mono/Release/Classes/classes.jar)
compileOnly project(':player')
}
Create new class MyUnityPlayerActivity.
package com.mycompany.myapplication.player;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayerActivity;
public class MyUnityPlayerActivity extends UnityPlayerActivity {
private static final String TAG = "Unity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Running MyUnityPlayerActivity.");
}
}
It should be good to go.
Related
I have to use PKCS10 class into my project. I tried to import sun.security.pkcs.PKCS10; into my activity code but studio shows "Cannot resolve symbol PKCS10" at both places where I have imported it and where I want to instantiat it.
Note: I haven't added any dependency or library (like .jar) to my project.
I want to know whether can it be automatically?
The class sun.security.pkcs10.PKCS10 was removed from the JDK. Using a class that was internal and already removed from the JDK is going a step beyond just using an internal API.
Put this into your build.gradle file.
compileJava {
options.forkOptions.javaHome = file(System.properties['java.home'])
}
And if this does not work, put this into you build.gradle(app) dependencies.
compile group: 'com.sun', name: 'rt', version: '1.5.0_06'
Is it, or would it be possible to modify a libraries' packagename at compile time using a gradle task similar to how jarjar works for changing the packagenames of a .jar file.
The library is pretty standard and included as follows:
dependencies {
compile 'com.scottyab:rootbeer-lib:0.0.6'
}
The purpose of this would be to change the name to avoid static detection of the library by root cloaking apps.
Any advice or guidance much appreciated! Thanks
There is already a Gradle plugin that does something similar. It relocates dependencies, Shadow.
It bundles and relocates common dependencies in libraries to avoid classpath conflicts.Shadow uses the ASM library to modify class byte code to replace the package name and any import statements for a class. Any non-class files that are stored within a package structure are also relocated to the new location.
This is how you relocate a package:
shadowJar {
relocate 'junit.framework', 'shadow.junit'
}
The code snippet will rewrite the location for any class in the junit.framework to be shadow.junit. For example, the class junit.textui.TestRunner becomes shadow.junit.TestRunner. In the resulting JAR, the class file is relocated from junit/textui/TestRunner.class to shadow/junit/TestRunner.class.
There is already a Gradle plugin that does something similar. It relocates dependencies, Shadow.
It bundles and relocates common dependencies in libraries to avoid classpath conflicts.Shadow uses the ASM library to modify class byte code to replace the package name and any import statements for a class. Any non-class files that are stored within a package structure are also relocated to the new location.
This is how you relocate a package:
shadowJar {
relocate 'junit.framework', 'shadow.junit'
}
The code snippet will rewrite the location for any class in the junit.framework to be shadow.junit. For example, the class junit.textui.TestRunner becomes shadow.junit.TestRunner. In the resulting JAR, the class file is relocated from junit/textui/TestRunner.class to shadow/junit/TestRunner.class.
I can see the library you are using is an open source library. You can simply clone the repo from here https://github.com/scottyab/rootbeer
and modify it to use any name you want. You don't even need to build your changes as a aar library. Simply import it as a module in Android Studio.
Gradle tasks are very powerful as well and it should be possible to do it with gradle but I don't know how.
I am not sure why I cannot import the android.support.v7.app.ActionBarActivity class. I looked at and followed the steps described in this question to change the jar file of the project. However in the solution the first step was to delete the jar file that was already there, I however did not have that file. I am not using gradle.
This is code I got from the android tutorial.
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActvity;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
}
The error that comes up first is
Error:(5, 30) java: package android.support.v7.app does not exist
In the manifest also wrote this line.
<uses-sdk android:minSdkVersion="7"/>
I assume this is okay because I would need to make sure that v7 stuff is supported. As mentioned in the title I am using IntelliJ.
If you are using IntelliJ IDEA and not Android Studio (gradle based), you
should add the library by going to File > Project Structure
then go to your module, tab Dependencies, and add the jar file from there.
Jar files in libs are not automatically added in IntelliJ, you have to manually add them as dependencies.
Add this in build.gradle in dependencies
compile 'com.android.support:appcompat-v7:21.0.0'
I have a library I want to add to my Android Application. But the problem is that the library is separated into .java files. Now I have tried to add a module to do the job but it does not work. My problem is I do not know how to add a bunch of .java files to my Android project to use throughout my app. I am new to Android dev and I have looked around but cannot find a clear explanation of how to add my type of library to an Android project.
I can see a couple of problems in the screenshot that you just posted above.
1) It seems that you're not telling Gradle (and therefore Android Studio) that you have Java files that need to be compiled. You can fix that by adding this to the build.gradle of the TMDB-Lib module:
sourceSets {
main {
java {
srcDir 'libs'
}
}
}
and then adding the module to your settings.gradle.
2) It seems that the Tmdb classes don't have a package (i.e. their first line isn't a package declaration) so the right way to import them into your Java code is by using just their class name:
import TmdbApi;
instead of:
import TMDB-lib.libs.TmdbApi;
I want to add this project as library to my project in android studio.
this is what I tried,
I have my project directory as f:/my project/my app/src
and my library in f:/my project/my library/src
I import the module (the library) by going to file > import module > selecting the library
then I got to file > project structure > modules > dependencies tab > select my project > add module dependency apply ok and then done
however when I use the code from the library I get the usual syntax error (the class ... could not be found)
also I noticed this popup (see image)
I am new to android studio or intelliJ, how do I fix this.
Thanks!
Edit the settings.gradle file (in directory f:/my project), it must contains something like this:
include 'my app','my library'
If this file don't exists: create it manually. The settings.gradle contains the list of gradle modules in a multi-module project.
Then you must add the dependency to your library in app. To do so edit the my app/build.gradle and add this line :
dependencies {
compile project(':my library')
}
I also notice that you don't use default structure for your projects (i.e. you put the code in src/ instead of src/main/java) so you will have to overwrite some values of the default fileSet in the build.gradle of your projects. Be sure to have something like this in my app/build.gradle and my library/build.gradle :
android {
sourceSets {
main {
java.srcDirs = ['src']
}
}
}