Unity通过Gradle打包方式会自动生成对应的gradle相关文件,再进行打包,虽然编辑器可以设置一部分参数,但是因为Unity版本不同,参数支持可能不够。
以Unity2018.4.17为例
仅支持设置部分路径,如需设置gradle版本,并没有地方设置。
?对于这种情况,其实Unity可以通过IPostGenerateGradleAndroidProject进行设置,不需要升级Unity版本。
比如我们可以这样修改打包后的gradle.properties文件,打包完后修改部分参数,再让Unity自动进行打包apk
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";
if (File.Exists(gradlePropertiesFile))
{
File.Delete(gradlePropertiesFile);
}
StreamWriter writer = File.CreateText(gradlePropertiesFile);
writer.WriteLine("org.gradle.jvmargs=-Xmx4096M");
writer.WriteLine("android.useAndroidX=true");
writer.WriteLine("android.enableJetifier=true");
writer.Flush();
writer.Close();
}
}
甚至我们可以打包后修改AndroidManfiest文件
using System.Xml;
using UnityEditor;
using UnityEditor.Android;
using UnityEditor.XR.Oculus;
#if UNITY_ANDROID
internal class OculusManifestBTFixer : IPostGenerateGradleAndroidProject {
static readonly string k_AndroidURI = "http://schemas.android.com/apk/res/android";
static readonly string k_AndroidManifestPath = "/src/main/AndroidManifest.xml";
void CreateNameValueElementsInTag(XmlDocument doc, string parentPath, string tag,
string firstName, string firstValue, string secondName = null, string secondValue = null, string thirdName = null, string thirdValue = null) {
var xmlNodeList = doc.SelectNodes(parentPath + "/" + tag);
// don't create if the firstValue matches
foreach (XmlNode node in xmlNodeList) {
foreach (XmlAttribute attrib in node.Attributes) {
if (attrib.Value == firstValue) {
return;
}
}
}
XmlElement childElement = doc.CreateElement(tag);
childElement.SetAttribute(firstName, k_AndroidURI, firstValue);
if (secondValue != null) {
childElement.SetAttribute(secondName, k_AndroidURI, secondValue);
}
if (thirdValue != null) {
childElement.SetAttribute(thirdName, k_AndroidURI, thirdValue);
}
var xmlParentNode = doc.SelectSingleNode(parentPath);
if (xmlParentNode != null) {
xmlParentNode.AppendChild(childElement);
}
}
public void OnPostGenerateGradleAndroidProject(string path) {
if (!OculusBuildTools.OculusLoaderPresentInSettingsForBuildTarget(BuildTargetGroup.Android))
return;
var manifestPath = path + k_AndroidManifestPath;
var manifestDoc = new XmlDocument();
manifestDoc.Load(manifestPath);
string nodePath = "/manifest";
CreateNameValueElementsInTag(manifestDoc, nodePath, "uses-permission", "name", "android.permission.BLUETOOTH");
manifestDoc.Save(manifestPath);
}
public int callbackOrder { get { return 20000; } }
}
#endif
参考链接:
android - Is there a way to change the gradle.properties file in Unity - Stack Overflow
|