Import Unity project in already built Android Project - android-studio

I have created an Android application in Android Studio and Unity. I can start UnityPlayerActivity from other activities in Android Studio. Now, I updated the Unity project and the scene in Unity and I want to start this updated Scene/Activity in the already built Android Project.
I don't want to create a new Android Studio project from Unity by exporting and then copy the other older files in the new project. This process can be very tedious and also VCS with GitHub would be another challenge. There are more than 40 files in my current project and just because of 1 update in Unity Project, I don't think creating a new project is worth it or something I would like to do.
Some suggestions which I already have are:
Export from Unity again and copy-paste all the files created in the older project from Android Studio.
Create a library and import this library in Android Studio (I am not very clear how this works but I don't think this will work because of the limited scope of a project).
So, is there any way such that I can export an Activity from Unity and start the new Activity in Android Studio?
Any help is appreciated and thanks in advance if anyone can solve this for me.

The cleanest way would be to convert the exported Android-Project into a library.
Remove the activity from src/main/AndroidManifest.xml
Modify build.gradle
Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library'
Change implementation fileTree(dir: 'libs', include: ['*.jar']) to api fileTree(dir: 'libs', include: ['*.jar'])
Remove all the bundle {...}-stuff
This should enable you to build a .aar-File which you can use in your native Android-Project.
Depending on your Unity-Version (and packages) other modifications could be needed, but that will be pointed out by errors in AndroidStudio when building the library.
The modifications are based on this article and complemented for Unity 2017.4.
The newest Unity beta (2019.3) also supports creating a library directly from Unity. But I would not recommend using a beta in production.

Related

How to get Android Studio to recognize my plain Gradle Java project?

I have a simple multi-module Gradle project with Java code. When I import it in IntelliJ Community, it asks me to select the build tool from among Maven and Gradle, and will properly recognize the Gradle modules and show me a Gradle tool window.
When I do the same in Android Studio, I don't get the build tool question, and it won't recognize the projects or show me the Gradle tool window.
How do I get this to work in Android Studio?
Simply Try This,
Go to File -> Project Structure -> Sdk Location
This You will get a option menu like this (the given image)
there you can update your gradle settings.
The answer, as far as I've been able to find out, is: Don't even try. Use IntelliJ instead. Installing it is most certainly quicker than trying to get Android Studio to do this.
How do you open the project in Intellij? Do you open the folder? Or do you open the build.gradle?
Just like a maven project (where you should open the pom.xml, not the folder) you should open the project using the build.gradle, not the folder.
Intellij will then do what you expect and import your gradle project
The least required to make it recognize the project is to add AGP, the Android Gradle Plugin:
buildscript {
repositories {
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
}
}
Then one can define Android modules with:
apply plugin: "com.android.application"
apply plugin: "com.android.library"
apply plugin: "com.android.dynamic-feature"
Then it will recognize the android {} configuration block (without this Gradle DSL configuration block it would not know what or how to build for Android - as it isn't a sane Android project). Just see the documentation linked, this has little to do with the default Java tooling.
Gradle 7.2 is currently required to build. Even if one cannot mix the plugins per module, one can have different plugins per module - or use different IDE, depending on the tooling of the module. How compatible this really is, is being determined by the dependencies used in the Java module. The fact that one can only use either tooling per single module dictates the layout to be applied.
I suspect that you will have to manually move some code from the successful build files in intellij to the android build files.

Android Studio Designer Preview went Black after implementing Material Components library

I am new in Android Development. In the latest version of Android Studio I want to use Material Components for my app. But after implementing its gradle script my Android Studio Designer Preview went black/blank. As you can see in screenshot below.
I have implemented the following line in my build.gradle
implementation 'com.google.android.material:material:1.4.0'
After removing the material components library the preview looks as it should.
I have also tried to restart Android studio as well as my computer.
I just realised that I have used
implementation 'com.google.android.material:material:1.4.0'
instead of
implementation 'com.google.android.material:material:1.4.0-rc01'
Now the issue has been fixed.

No android menu group in android studio 3.0

I've updated android studio to 3.0 and android group disapeared from tools menu.
I have SDK installed
I need android studio for ionic - I'm beginner and don't know that tool
As far as I can determine (by experimenting) group and barriers are not supported in Constraint layout 1.0.2 which is what is used by default in Android Studio 3.0.
That is the the latest version of Constraint layout didn't ship with the latest version of Android Studio.
It is still in beta - beta 3 released on Oct 12th.
If you want to use it
open the projects build.gradle file and change it to read:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta3'
Only the last line needs changing i.e. from 1.0.2 to 1.1.0-beta3. You will need to resync the project and then you will see group and barriers in the right click context menu.

Building tess-two into a project using Android Studio's gradle build

There are answers about getting the tess-two project integrated into an Android project within Android Studio, but many are out-dated and none used the current capabilities defined here:
Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI)
What specific steps would be required to make tess-two functionality a part of the resulting APK, using functionality within Android Studio, rather than external file placement, manipulation and command line tools? So taking the building, creation of *.so files into the IDE. Specifically using just Android Studio's integrated build system (Gradle) as described here.
1. Start a new project
2. Import Tess-Two into the project
2b. Add any needed plug-ins
3. Add code in the main activity to get native functionality
4. Configure Android Studio build so that native functionality is available
This is where the specifics are required
4. Configure Android Studio build for native functionality
4a. Link C++ Project with Gradle
First, check the tess-two project for build files. You may select CMakeLists.txt or Android.mk files. Both are currently supported.
In this case, I used ndk-build, which seemed like a good bet for integrating the native code.
For more information, see Android Studio documentation
4b. Manage Long Commands
In Windows, errors may be encountered if the command length grows too large. To prevent problems, use LOCAL_SHORT_COMMANDS AND APP_SHORT_COMMANDS in the Android.mk file.
The "e=87" error is what you are avoiding by doing this:
For more on that topic, see stackoverflow question about error 87.
4c. Add Module Dependency for tess-two
In File > Project Structure > Dependencies use the + to add the tess-two dependency:
4d. Build the Project and check .apk file for .so files
The build, which takes a long time, should complete now. Validate that the .apk file contains the .so files, created during the build. With the tess-two libraries in an static initializer, run the project on your Android device:

How to import unity project into existing Android studios projecT?

I am having problems importing unity project into Android Studios. I already have a Android studio project (for menu and other things), and i want to add a scene from Unity 5 3D, as a second activity (not the main activity).
I want to call unity scene when user clicks the button.
I know how to export it from unity but it automatically sets it as MainActivity, and i cant find a way to manually overwrite it.
Edit:
I want to do it in Android Studios and not Eclipse. I just want to know if there is an easy way to do it, or at least a hint for how to do it.
Had the same requirement and successfully imported the Unity project into an existing Android Studio project. As mentioned the first thing will be to export the Unity project. I used Unity version 2017.1. Rough steps below:
Export Android project via Unity build settings
You can simply select the folder where your existing project is located. Unity should add a sub folder with an Android Studio format project and all dependencies and everything it needs to run.
Now we need to convert this "Application" project to a "Library" project. This is simply done by changing the build.gradle file. Replace apply plugin: 'com.android.application' with apply plugin: 'com.android.library'.
You also need to remove the applicationId and change the compile versions to match your project's versions.
Also make sure to modify the Unity module AndroidManifest.xml file. You will need to remove the LAUNCHER intent-filter as the UnityPlayerActivity will not be the main activity anymore.
android.intent.action.MAIN
android.intent.category.LAUNCHER
Also need to remove the Application node attributes so it won't conflict with your project's manifest.
Finally in your settings.gradle add the module and then add the Unity project dependency on your "app" module: compile project(':UnityFolder')
Build and now you should be able to start the Unity Scene by calling:
Intent intent = new Intent(this, UnityPlayerActivity.class);
startActivity(intent);
yes we can start another activity before unity Activity
1 step-create java class and add it in manifest and like in image
2nd step- create onClick in java class
link to unity game class s you can see in amage
see pic, you can choose button also
3rd step- in manifest replace Intent in own java class it will work [3]
From this link: https://nevzatarman.com/2015/01/04/unity-android-tutorial-opening-unity-scene-from-an-activity/
Add android:process=":UnityKillsMe" to the unity activity GameActivity.

Resources