1、环境
- Unity 2021.3.8f1
2、基础知识梳理
2.1 自定义配置文件用途
1、mainTemplate.gradle
配置构建Android Library的信息。
在Gradle工程中,对应unityLibrary/build.gradle
Unity的内容应当构建为一个Android Library程序(Unity主程序),这样它可以嵌入到其他Android程序中。
2、launcherTemplate.gradle
配置构建独立Android应用(启动程序)的指令。
在Gradle工程中,对应launcher/build.gradle
3、baseProjectTemplate.gradle
配置在所有其他Template、Gradle工程中都共享的内容。
在Gradle工程中,对应build.gradle
。
4、gradleTemplate.properties
配置Gradle构建需要的环境。如:JVM内存、是否支持多JVM、是否压缩native libs
在Gradle工程中,对应gradle.properties
。
5、LauncherManifest.xml
启动程序的Manifest配置文件
在Gradle工程中,对应launcher/src/main/AndroidManifest.xml
。
6、AndroidManifest.xml(MainManifest)
游戏主程序的Manifest配置文件
在Gradle工程中,对应unityLibrary/src/main/AndroidManifest.xml
。
中途报错解决
1、不能使用Plugins/Android/assets目录放资源
Error building Player: Exception: OBSOLETE -
Providing Android resources in Assets/Plugins/Android/assets was removed,
please move your resources to an AAR or an Android Library.
See "AAR plug-ins and Android Libraries" section of the Manual for more details.
解决:
正经的解决方法有两个:
1、将assets资源打包到一个aar文件里,放到Plugins/Android目录下
2、将assets下的资源放到一个独立Android Library子模块里(也就是带.androidlib
的目录里,如:mylib.androidlib)
这里我们平台同学发现一个“漏洞”,丢在StreamingAssets里的文件,跟Plugins/Android/assets里的文件最终会放到一起~
最终我们决定把资源直接放到StreamingAssets里
2、IL2CPP模式编译ARM64的apk失败
Building Library\Bee\artifacts\Android\d8kzr\libil2cpp.so failed with output:
Library/Bee/artifacts/Android/87lik/03uo_osix1.lump.o: In function `std::__ndk1::_DeallocateCaller::__do_call(void*)':
E:/SDK\android-ndk-r21d\toolchains\llvm\prebuilt\windows-x86_64\bin\..\sysroot\usr\include\c++\v1/new:334:(.text._ZNSt6__ndk112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED2Ev[_ZNSt6__ndk112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED2Ev]+0x10): relocation truncated to fit: R_AARCH64_JUMP26 against symbol `operator delete(void*)' defined in .text._ZdlPv section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android\libc++_static.a(new.o)
Library/Bee/artifacts/Android/87lik/03uo_osix1.lump.o: In function `il2cpp::os::Console::InternalKeyAvailable(int)':
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:222:(.text._ZN6il2cpp2os7Console20InternalKeyAvailableEi+0x2c): relocation truncated to fit: R_AARCH64_CALL26 against symbol `__FD_SET_chk@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:226:(.text._ZN6il2cpp2os7Console20InternalKeyAvailableEi+0x3c): relocation truncated to fit: R_AARCH64_CALL26 against symbol `div@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:236:(.text._ZN6il2cpp2os7Console20InternalKeyAvailableEi+0x68): relocation truncated to fit: R_AARCH64_CALL26 against symbol `select@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:238:(.text._ZN6il2cpp2os7Console20InternalKeyAvailableEi+0x74): relocation truncated to fit: R_AARCH64_CALL26 against symbol `__errno@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:244:(.text._ZN6il2cpp2os7Console20InternalKeyAvailableEi+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against symbol `ioctl@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
Library/Bee/artifacts/Android/87lik/03uo_osix1.lump.o: In function `il2cpp::os::Console::SetProperty(int, bool)':
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:50:(.text._ZN6il2cpp2os7ConsoleL11SetPropertyEib+0x28): relocation truncated to fit: R_AARCH64_CALL26 against symbol `tcgetattr@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:66:(.text._ZN6il2cpp2os7ConsoleL11SetPropertyEib+0x68): relocation truncated to fit: R_AARCH64_CALL26 against symbol `tcsetattr@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:(.text._ZN6il2cpp2os7ConsoleL11SetPropertyEib+0xb8): relocation truncated to fit: R_AARCH64_CALL26 against symbol `__stack_chk_fail@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
Library/Bee/artifacts/Android/87lik/03uo_osix1.lump.o: In function `il2cpp::os::Console::TtySetup(std::__ndk1::basic_string, std::__ndk1::allocator > const&, std::__ndk1::basic_string, std::__ndk1::allocator > const&, unsigned char*, int**)':
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:287:(.text._ZN6il2cpp2os7Console8TtySetupERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEESA_PhPPi+0x40): relocation truncated to fit: R_AARCH64_CALL26 against symbol `getenv@@LIBC' defined in .text section in E:/SDK/android-ndk-r21d/toolchains/llvm/prebuilt/windows-x86_64/bin/../sysroot/usr/lib/aarch64-linux-android/22/libc.so
E:/Unity\2021.3.8f1\Editor\Data\il2cpp\libil2cpp\os\Posix/Console.cpp:290:(.text._ZN6il2cpp2os7Console8TtySetupERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEESA_PhPPi+0x48): additional relocation overflows omitted from the output
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
解决:
具体啥问题没搞清楚,连搜带蒙测试下来:
打包时关闭
Develpment Build -> Script Debugging
选项,就不会报错
参考文章:
Unity2020 il2cpp 代码多的情况下,直接打包出错,使用obb模式或导出到AndroidStudio工程再打apk没问题
3、Gradle工程编译APK缺少tools.jar
编译的命令:
java -classpath D:\Mole\RSUpgrade\PackageTool\Android\gradle-wrapper.jar org.gradle.wrapper.GradleWrapperMain aR -p .\UAProj64
报错:
> Task :unityLibrary:compileReleaseJavaWithJavac FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unityLibrary:compileReleaseJavaWithJavac'.
> Could not find tools.jar. Please check that E:\SDK\jre1.8.0 contains a valid JDK installation.
解决:
问题原因是我本地的jre目录下,没有tools.jar
这个包。
解决方式是把jdk1.8.0_171\lib\tools.jar
拷贝到jre1.8.0\lib
里面。
当然,这个解决方案其实有些奇怪。因为要手动修改安装好的环境。