유니티(Android) 익스포트시 Firebase 설정
Unity 버젼 2020.3.36f1
Android Studio Version : 2022.1.1
유니티에 Firebase가 연동되어 있고 유니티를 안드로이드 스튜디오 쪽으로 익스포트 해야 할때
Firebase 라이브러리가 스튜디오쪽 라이브러리와 충돌될 가능성이 있습니다.
안드로이드 스튜디오에서 Firebase프로젝트를 잡아주고 충돌을 피해 가는 시행착오를 적어봅니다.
Android Studio 설정
빌드후 Firebase 프로젝트를 추가를 해야됩니다.
settings.gradle
include ':app'
include ':unityLibrary'
project(':unityLibrary').projectDir=new File('.\\androidBuild\\unityLibrary')
include ':unityLibrary:FirebaseApp.androidlib' // 추가
이제 몇가지 더 수정해야됩니다
build.gradle (FirebaseApp.androidlib) 에서 다음과 같이 설정합니다.
compileSdkVersion 31
buildToolsVersion '30.0.2' // 제거 해도 됨.
defaultConfig {
targetSdkVersion 31 // 9 -> 31 수정
}
중복 파일 설정
build.gradle(UnityLibrary)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//implementation(name: 'androidx.arch.core.core-runtime-2.0.0', ext:'aar')
implementation(name: 'androidx.asynclayoutinflater.asynclayoutinflater-1.0.0', ext:'aar')
//implementation(name: 'androidx.coordinatorlayout.coordinatorlayout-1.0.0', ext:'aar')
//implementation(name: 'androidx.core.core-1.2.0', ext:'aar')
//implementation(name: 'androidx.cursoradapter.cursoradapter-1.0.0', ext:'aar')
//implementation(name: 'androidx.customview.customview-1.0.0', ext:'aar')
//implementation(name: 'androidx.documentfile.documentfile-1.0.0', ext:'aar')
//implementation(name: 'androidx.drawerlayout.drawerlayout-1.0.0', ext:'aar')
//implementation(name: 'androidx.fragment.fragment-1.0.0', ext:'aar')
//implementation(name: 'androidx.interpolator.interpolator-1.0.0', ext:'aar')
implementation(name: 'androidx.legacy.legacy-support-core-ui-1.0.0', ext:'aar')
//implementation(name: 'androidx.legacy.legacy-support-core-utils-1.0.0', ext:'aar')
//implementation(name: 'androidx.lifecycle.lifecycle-livedata-2.0.0', ext:'aar')
//implementation(name: 'androidx.lifecycle.lifecycle-livedata-core-2.0.0', ext:'aar')
//implementation(name: 'androidx.lifecycle.lifecycle-runtime-2.0.0', ext:'aar')
//implementation(name: 'androidx.lifecycle.lifecycle-viewmodel-2.0.0', ext:'aar')
//implementation(name: 'androidx.loader.loader-1.0.0', ext:'aar')
//implementation(name: 'androidx.localbroadcastmanager.localbroadcastmanager-1.0.0', ext:'aar')
//implementation(name: 'androidx.print.print-1.0.0', ext:'aar')
implementation(name: 'androidx.slidingpanelayout.slidingpanelayout-1.0.0', ext:'aar')
implementation(name: 'androidx.swiperefreshlayout.swiperefreshlayout-1.0.0', ext:'aar')
//implementation(name: 'androidx.versionedparcelable.versionedparcelable-1.1.0', ext:'aar')
//implementation(name: 'androidx.viewpager.viewpager-1.0.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-ads-identifier-18.0.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-base-18.1.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-basement-18.1.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-api-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-base-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-impl-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-sdk-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-measurement-sdk-api-21.2.0', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-stats-17.0.2', ext:'aar')
implementation(name: 'com.google.android.gms.play-services-tasks-18.0.2', ext:'aar')
implementation(name: 'com.google.firebase.firebase-analytics-21.2.0', ext:'aar')
implementation(name: 'com.google.firebase.firebase-common-20.3.0', ext:'aar')
implementation(name: 'com.google.firebase.firebase-components-17.1.0', ext:'aar')
implementation(name: 'com.google.firebase.firebase-installations-17.0.1', ext:'aar')
implementation(name: 'com.google.firebase.firebase-installations-interop-17.0.1', ext:'aar')
implementation(name: 'com.google.firebase.firebase-measurement-connector-19.0.0', ext:'aar')
implementation project('FirebaseApp.androidlib')
}
빌드할때마다 매번 지우고 하는게 귀찮고 실수할 가능성이 높아집니다.
유니티에서 안드로이드 플러그인 라이브러리를 설정하는법이 있습니다.
ProjectSettings\AndroidResolverDependencies.xml 파일에서 빌드시 포함할 파일을 명시할 수 있습니다.
<file>Assets/Plugins/Android/androidx.asynclayoutinflater.asynclayoutinflater-1.0.0.aar</file>
<file>Assets/Plugins/Android/androidx.legacy.legacy-support-core-ui-1.0.0.aar</file>
<file>Assets/Plugins/Android/androidx.slidingpanelayout.slidingpanelayout-1.0.0.aar</file>
<file>Assets/Plugins/Android/androidx.swiperefreshlayout.swiperefreshlayout-1.0.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-ads-identifier-18.0.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-base-18.1.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-basement-18.1.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-api-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-base-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-impl-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-sdk-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-measurement-sdk-api-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-stats-17.0.2.aar</file>
<file>Assets/Plugins/Android/com.google.android.gms.play-services-tasks-18.0.2.aar</file>
<file>Assets/Plugins/Android/com.google.errorprone.error_prone_annotations-2.9.0.jar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-analytics-21.2.0.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-annotations-16.2.0.jar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-common-20.3.0.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-components-17.1.0.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-installations-17.0.1.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-installations-interop-17.0.1.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-measurement-connector-19.0.0.aar</file>
<file>Assets/Plugins/Android/com.google.firebase.firebase-app-unity-10.4.0.aar</file>
<file>Assets/Plugins/Android/javax.inject.javax.inject-1.jar</file>
<file>Assets/Plugins/Android/org.jetbrains.kotlin.kotlin-stdlib-common-1.7.10.jar</file>
AndroidResolverDependencies.xml 파일이 다시 초기화 되게 하지 않으려면 해줘야 할 것이 하나 있습니다.
유니티 메뉴의 Assets > External Dependency Manager > Android Resolver > Settings 클릭.
Enable Auto-Resolution과 Enable Resolution On Build 이 둘을 체크 해제하여 자동으로 Resolver 창이 뜨지 않게 합니다.
이렇게 APK를 빌드하면 이런 에러를 또 보게됩니다.
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable |
해결책은 탐색기에서 아래 해당 파일들을 물리적으로 삭제해야 합니다.
unityLibrary\libs 폴더 아래에 삭제해야할 파일 목록
하지만 자동화 하는 방법이 있습니다.
유니티의 OnPostProcessBuild함수를 이용하는 방법입니다.
https://docs.unity3d.com/ScriptReference/Build.IPostprocessBuild.html
지금은 구식이 되어버린 방식이라네요.
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
{
switch (buildTarget)
{
case BuildTarget.Android:
{
string strPath = "../androidBuild/unityLibrary/libs/"; // 빌드 경로에 맞게
string[] strName = {
"androidx.annotation.annotation-1.5.0",
"androidx.arch.core.core-common-2.0.0",
"androidx.collection.collection-1.0.0",
"androidx.concurrent.concurrent-futures-1.1.0",
"androidx.lifecycle.lifecycle-common-2.0.0",
"com.google.guava.listenablefuture-1.0",
"org.jetbrains.annotations-13.0",
"org.jetbrains.kotlin.kotlin-stdlib-1.7.10"
};
for (int i = 0; i < 8; i++)
{
File.Delete(strPath + strName[i] + ".jar");
}
Debug.Log("Success Delete Files !!!");
break;
}
}
NDK 설정
유니티에서 IL2CPP 사용시 NDK 버젼이 안맞다고 에러가 뜰 수 있습니다.
유니티가 지원하는 버젼으로 세팅해 줍니다. 2020.3.36f1버젼은 19.0.5232133입니다.
build.gradle (app)
android {
defaultConfig {
ndk {
ndkVersion '19.0.5232133'
}
}
local.properties
ndk.dir=C\:\\Program Files\\Unity\\Hub\\Editor\\2020.3.36f1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\NDK
유니티의 Preferences에서 Copy Path를 클릭하면 경로를 가져올수 있습니다.![]() |
이제 빌드를 해보면 성공할수 있을듯합니다.
반드시 기기에 넣고 Firebase가 초기화 되는지까지 확인해야합니다.
끝.