Embedded Unity within my App - android

I'm trying to build an AR project in Unity and use it as a library in my native android app.
I used this article as a guideline:
https://medium.com/#davidbeloosesky/embedded-unity-within-android-app-7061f4f473a
I successfully imported the project to Android Studio, but the settings.gradle file is missing.
Although I can run the project from the studio, when I convert it to a library and use its aar file in my native android app - I fail to build the project.
Has anyone succeeded doing it?

I followed the guideline and it worked out. I put the .aar file below the native app libs file. In app build.gradle, under dependencies add:
implementation(name:"AndroidPluginDemo",ext:"aar")
and under android add
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}

Just to add on to the above answers asking about the camera not working, the problem for me was the one of the classes in the unity app wasn't being found when i imported that library into my android app.
When you're converting the application to a library project, take note in the build.gradle of the dependencies.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(name: 'vlsdk', ext:'aar')
}
In my case I had to find the 'vlsdk.aar' file that was being referenced, and import that as a new module (as is done in step 6 of the tutorial) into my native android app. Hope this helps.

I know this is very old and i hope you have found a solution. but to anyone else facing this issue.
Add the ".aar" libraries found in the libs folder of unity's exported project to the build.gradle file as dependencies(they also need to be physically present in the native projects libs folder).
In my case when i built the .aar library from the exported project it did not add the dependencies(unity exported project also has dependencies found in the libs folder). I only excluded the "unity-classes.jar" file as my unity code is already in the ".aar" that i created.
Check the following link
https://medium.com/#randive.rishiraj/7-steps-to-integrate-google-arcore-unity-project-to-a-native-android-app-d85793ba0b37
Hope this helps.

To implement Unity features inside native apps we need to change Unity Project as a library, and then integrate inside our native application.
From version 2019.3.a2 Unity introduced a new feature Unity as a library in native apps both Android and Ios.
Steps:
1. Export Unity Project as an Library (Android)
2. Add Unity Library Module inside Native Android Project
3.Show Unity View inside Android Activity
Previously, the exported Unity project will have only one gradle module and one manifest file.
But now the Structure has been changed to two gradle modules.
1. launcher
2.unityLibrary
Both launcher and unityLibrary module will have its own build.gradle and manifest files.
unityLibrary - module will have Unity runtime and project data, it can be easily integrated into any other gradle project. It will help
to integrate Unity inside our native android project.
launcher - module will have all icons and the application name.
But we want use only unityLibrary module as an library inside our native android project.
This blog helps to implement unity view in your native android app.
https://navinkumar0118.medium.com/show-unity-view-inside-native-android-app-8035b9b6895a

Related

Export libgdx project as Android library including dependencies

I have two, separate Android projects. One is a regular Android application and the other one is a libgdx project.
My goal is to be able to compile the libgdx project as an Android library into aar file, so I could use it in the regular, non libgdx, Android application (I'm going to start the libgdx game's activity from the regular Android project).
The libgdx project consists of several modules (I'm using only the android and the desktop modules), so in my libgdx project I can find 3 modules: android, desktop and core (where basically the whole game's code is resides). When compiling and running the game on Android, the android module kicks in, but it uses the core module as a dependency.
When trying to change the libgdx project into an Android library project and compiling it into aar, it seems like it lacks the needed dependencies (like the core module, in addition to some other dependencies).
How can I create an aar file from the libgdx project which has all the needed dependencies?
So eventually I managed to find a solution for my problem.
Lets start with the fact that my first impression was misleading, and the problem I had was not a libgdx specific problem, but a gradle "problem".
In short, the reason behind that is that the aar/jar files don't contain the transitive dependencies and don't have a pom file which describes the dependencies used by the library.
To overcome this behaviour you need somehow to specify the dependencies in your project. You can choose between 2 approaches:
first approach:
You can use a central repository, such as JCenter, and publish the project as library. In this case, gradle will be able to download the dependencies using the pom file which contains the dependencies list.
Second approach:
You need to manually copy all your dependencies to the libs folder. You can do this relatively easy by writing a small gradle task:
task copyCompileDependenciesToLibs(type: Copy) {
def libsPath = project.projectDir.toString() + "/libs"
from configurations.compile
into libsPath
}
This snippet will copy all your dependencies to the libs folder, and once you compile your library project the dependencies will be included.

Why the lib module can't include the 3rd lib in android studio

I had an issue
"java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/lang/StringUtils;"
when i tried to run an android app that deponds on my own android lib.
Fllowing is my steps:
Created an android project ,and then built a new module named "sptvlib" in it.
This module depends on a 3rd party lib named "common-lang" that is a String utils lib.please see the "sptvlib" gradle file:
dependencies {
...
compile 'org.apache.directory.studio:org.apache.commons.lang:2.6'
...
}
After built the whole project, i got the module sptvlib.aar file, and copied it to the "libs" directory of another android app project.
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
...
compile(name: 'sptvlib', ext: 'aar')
...
}
I built this app successfully, but when it run, the logcat showed " java.lang.NoClassDefFoundError" error.
I have to re-add "commons.lang" declatration to the app project gradle, and then it run ok... we have added the 3rd party lib to my module use the "compiled". Why the app that deponds on my own module still could't find it in the runtime?
I assume you are using some function of commons.lang in your main app project.
Since you are using compiled assembled code of your sptvlib library, you can used the compiled exposed functions of sptvlib in your main app project.
But you can not access the library functions of commons.lang directly in your main app project.
This is because you have not added sptvlib code as a module to your app project. you are only using the compiled version of sptvlib.

How do I use another library in my app?

The title is a little misleading but I honestly don't know how to word it in any other way. This is my project structure:
I want to use the highlighted (fasteranimationscontainer-master) in my current app that I'm working on. I imported it by putting the jar file in my library folder then adding it to my library through Android Studio. But I try to create an object of that imported library, it doesn't show up/import.
I'm still learning how to use android studio so any help would be much appreciated!
1. Library with only the Source Code
To include a library with only the source code (like FasterAnimationsContainer) to your project, you only need to import it as a module from File -> New-> Import Module in Android Studio:
There should be a dialog for the project folder, enter the path where your library reside.
Then you need to add the module to your app build.gradle as a dependency:
android {
...
dependencies {
...
compile project(':fasteranimationscontainer')
...
}
}
Now you can use it in your project.
2. Library from Maven or JCenter
To add dependencies to your project where the library has uploaded to maven or jcenter, you need to modify your app build.gradle file and add extra lines configuring the packages you require. For example, for certain Google or Android, dependencies look like:
android {
...
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:6.5.+'
// Support Libraries
compile 'com.android.support:support-v4:22.2.1'
}
}
Try to always read the library README first.
Suggestion:
If you still learning using Android Studio, you can read Using Android Studio. Then read Getting Started with Gradle, because Android Studio is tightly related with Gradle.

Multiple AAR files

I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.
If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.
Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}

Android/Gradle: Where is this dependency embedded?

I'm using the new Android build system that is based on Gradle, together with the early access preview Android Studio. Now, I have two projects: an Android library project, and an Android app project (basically a demo for the library).
In the library project I have added a dependency to the gson library, so my build.gradle file looks like this:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'com.google.code.gson:gson:2.2.+'
}
Still, everything works fine and dandy and I'm able to use gson in my library and then my app. But I want to understand where this library is embedded. I've opened both the .aar that is built by the library project and the .apk of the demo app. I was expecting to find the jars for the two dependencies in at least one of these, but I didn't.
So where are they?
From Android Tools website:
These items, plus the output of the compilation of the project’s own source code, are sent to dex for bytecode conversion and inclusion in the final APK.
In other words, they are in your *.dex file inside the APK.
As #SharkyXTS said, the code from any external dependencies is compiled into the final .dex file inside your APK. The reason why you can't find any references to these dependencies in the .aar is because there aren't any.
The .aar format is only supported through Maven for now, so dependencies are found through there. I believe there are plans to eventually support local .aar dependencies (without Maven), but the Android plugin isn't quite there yet. You can see this issue for more information.

Categories

Resources