1.官方文档的重要性
官网地址:https://docs.unity3d.com/cn/2022.1/ScriptReference/index.html
Editor 编辑器日志与Player 运行日志 路径
C:\Users\songzhewen\AppData\Local\Unity\Editor
C:\Users\songzhewen\AppData\LocalLow\longtu\NRJ
2.unity 最新版本打包现状
然后打包,拉跨,各种失败
最新版Unity 2020.3.22 不能打包的问题解决方案(android build tools )
我也是碰到了这个错误,用的Unity 2020.3.22。Gradle文件选择的是Unity下载的,应该是6.11的版本。Android SDK下载了31.0.0。但是我看网上说主要是SDK构建工具31上缺少2个文件,即1.dx.bat2.dx.jar,解决方案是这些文件在文件位置中被命名为d8,因此将它们的名称更改为dx将解决错误。
步骤如下:
1)进入C:\Users\user\AppData\Local\Android\Sdk\build-tools\31.0.0目录
2)找到一个名为d8.bat的文件,这是Windows批处理文件。
3)重命名d8.bat为dx.bat。
4)进入C:\Users\user\AppData\Local\Android\Sdk\build-tools\31.0.0\lib目录
5)在将d8.jar重命名为dx.jar
这里面的路径是你对应的Android SDK的路径。参考的是这个博客的评论。
然后就打包成功了。
unity打包gradle打包现状分析(惨不忍睹)
因为外网测试需要,下载了unity 最新稳定开发版(2020.3.32),各种环境均无问题,按官方推介做法
unity gradle常见问题 解决原理:
无非就是新旧版本的gradle导致的,请参考姊妹篇了解unity中的gradle打包原理。
https://blog.csdn.net/osuckseed/article/details/93089977
举例说明:报错信息很明确
1.明确了必需使用6.1.1 ,unity安装路径下替换即可
2.明确的错误,清单文件合并报错
3.错误位置
unity gradle解决心得:
多测试,快速迭代,注意报错信息:
unity gradle 打包报错信息,多种多样,遇到实在无法解决的如下面这种无任何提示的,删除类库或代码,用二分法查找:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unityLibrary:processReleaseManifest'.
> java.lang.NullPointerException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
对于有明确报错的处理方法:
ex1:
error: style attribute 'attr/colorPrimary
ex2:
Execution failed for task ':launcher:processReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
ex3:
unity This project uses AndroidX dependencies, but the 'android.useAndroidX'
and so on 有明确错误的一定要百度
对于一些引用了其它类库导致的问题,或是需要使用Gradle7 以上的
先删除打包时的引用,将他们拷贝到libs下,只使用,不参与打包
文章来源:https://www.toymoban.com/news/detail-421018.html
3.后处理使用案例(IPostGenerateGradleAndroidProject):
unity后处理修改 gradle.properties
using System.Collections.Generic;
using System.IO;
using UnityEditor.Android;
using UnityEngine;
public class AndroidPostBuildProcessor : IPostGenerateGradleAndroidProject
{
public int callbackOrder
{
get
{
return 999;
}
}
void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path)
{
Debug.Log("Bulid path : " + path);
string gradlePropertiesFile = path + "/../gradle.properties";
Debug.Log("Bulid path : " + gradlePropertiesFile);
if (File.Exists(gradlePropertiesFile))
{
File.Delete(gradlePropertiesFile);
}
using(StreamWriter sw = File.CreateText(gradlePropertiesFile))
{
sw.WriteLine("org.gradle.jvmargs=-Xmx4096M");
sw.WriteLine("org.gradle.parallel=true");
sw.WriteLine("android.enableR8=false");
sw.WriteLine("unityStreamingAssets=.unity3d");
sw.WriteLine("android.useAndroidX=true");
sw.WriteLine("android.enableJetifier=true");
}
// StreamWriter writer = File.CreateText(gradlePropertiesFile);
// // writer.WriteLine("org.gradle.jvmargs=-Xmx4096M");
// // writer.WriteLine("android.useAndroidX=true");
// // writer.WriteLine("android.enableJetifier=true");
// writer.WriteLine("android.useAndroidX=true");
// writer.WriteLine("android.enableJetifier=true");
// writer.Flush();
// writer.Close();
}
}
unity 后处理更改清单文件(AndroidManifest)
public void OnPostGenerateGradleAndroidProject(string path) {
//路径参考
string k_AndroidManifestPath = "/src/main/AndroidManifest.xml";
//launcher\build\intermediates\instant_app_manifest\release
string k_AndroidManifestPath2 = "/launcher/build/intermediates/instant_app_manifest/release/AndroidManifest.xml";
var manifestPath = path + "/.."+k_AndroidManifestPath2;
if (!File.Exists(manifestPath))
{
}
else
{
//string[] str = File.ReadAllLines(path);
string str = File.ReadAllText(manifestPath);
Debug.Log("id:"+manifestPath);
Debug.Log("id:"+Application.identifier);
Debug.Log("id:"+path);
var bef = str.Contains(Application.identifier);
Debug.Log("ber:"+bef.ToString());
str = Regex.Replace(str,Application.identifier,"ltbase_package_name");
var afte = str.Contains(Application.identifier);
Debug.Log("afte:"+afte.ToString());
File.WriteAllText(manifestPath,str);
}
}
Unity xode工程后处理
[PostProcessBuildAttribute(0)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
if (target != BuildTarget.iOS) {
return;
}
string projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject pbxProject = new PBXProject();
pbxProject.ReadFromFile(projectPath);
//Add file to unity iphone
//设置安装包的名字
string unityIphoneGuid = pbxProject.GetUnityMainTargetGuid();
//设置编译属性(SetBuildProperty)
var setList = new Dictionary<string,string>()
{
{"ENABLE_BITCODE","No"},
{"OTHER_LDFLAGS","-ObjC"},
{"ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES","Yes"},
{"DEBUG_INFORMATION_FORMAT","DWARF with dSYM File"},
{"CODE_SIGN_STYLE","Manual"},
};
pbxProject.SetBuildProperty(unityIphoneGuid,"ENABLE_BITCODE","No");
//添加资源文件
var fileList = new List<string>() { "a.json","b.json","c.dat"};
var newPath = pathToBuiltProject.Replace("/Xcode/", "Projectios");
fileList.ForEach(s =>
{
var res = pbxProject.GetResourcesBuildPhaseByTarget(unityIphoneGuid);
var resGuid = pbxProject.AddFolderReference(newPath+s,pathToBuiltProject+"Data/"+s,PBXSourceTree.Source);
pbxProject.AddFileToBuildSection(unityIphoneGuid,res,resGuid);
});
//添加类库
string unityFrameworkGuid = pbxProject.GetUnityFrameworkTargetGuid();
pbxProject.AddFrameworkToProject(unityFrameworkGuid,"a.framework",true);
//添加宏(特殊标签)
string targetGuid = pbxProject.TargetGuidByName("Unity-iPhone");
//在Other Linker Flags 添加 -ObjC
pbxProject.AddBuildProperty(targetGuid,"OTHER_LDFLAGS","-ObjC");
//设置Enable Bitcode 为No
pbxProject.SetBuildProperty(targetGuid,"ENABLE_BITCODE","No");
//添加.framework库
pbxProject.AddFrameworkToProject(targetGuid,"Security.framework",false);
pbxProject.AddFrameworkToProject(targetGuid,"CoreGraphics.framework",false);
pbxProject.AddFrameworkToProject(targetGuid,"WebKit.framework",false);
pbxProject.AddFrameworkToProject(targetGuid,"CoreTelephony.framework",true);
pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libsqlite3.0.tbd", "Frameworks/libsqlite3.0.tbd", PBXSourceTree.Sdk));
pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libz.tbd", "Frameworks/libz.tbd", PBXSourceTree.Sdk));
pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libc++.tbd", "Frameworks/libc++.tbd", PBXSourceTree.Sdk));
//写入文件,如果不写入不会生效的
File.WriteAllText(projectPath,pbxProject.WriteToString());
//为plist写入项目中需要的配置 一般项目中接入微信会需要
string plistPath = System.IO.Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementDict dict = plist.root.CreateDict("NSAppTransportSecurity");
dict.SetBoolean("NSAllowsArbitraryLoads", true);
//iOS跳转需要配置
PlistElementArray array = plist.root.CreateArray ("LSApplicationQueriesSchemes");
array.AddString("weixinULAPI");
array.AddString("weixin");
PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes");
dict = urlTypes.AddDict();
dict.SetString("CFBundleURLName", "weixin");
PlistElementArray schemes = dict.CreateArray("CFBundleURLSchemes");
schemes.AddString("APPID");
dict = urlTypes.AddDict();
dict.SetString("CFBundleURLName", "pay");
schemes = dict.CreateArray("CFBundleURLSchemes");
schemes.AddString("xxx-xxxx");
dict = urlTypes.AddDict();
dict.SetString("CFBundleURLName", "app");
schemes = dict.CreateArray("CFBundleURLSchemes");
//bundleID
schemes.AddString("com.xxx.xx.xxxxx");
//写入plist
plist.WriteToFile (plistPath);
}
后处理函数的具体使用(PostProcessBuildAttribute)
打包完成后的一些文件的拷贝删除与替换文章来源地址https://www.toymoban.com/news/detail-421018.html
// C# example:
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public class MyBuildPostprocessor {
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
Debug.Log( pathToBuiltProject );
}
}
4.完全完整的自动化打包流程示例
待续 (先占位置,后续会把具体实例补充完整)
到了这里,关于Unity打包与编译,后处理以及自动化等等(持续更新)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!