Created
May 31, 2021 08:55
-
-
Save tylearymf/fb1512a41c8968854bf8c6d78be64fe7 to your computer and use it in GitHub Desktop.
提取Unity离线打包所需的相关jar包资源
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
namespace ExtractGradleJar | |
{ | |
class Program | |
{ | |
static string[] AllowedExtensions = new[] { ".jar", ".pom" }; | |
/// <summary> | |
/// 提取Unity离线打包所需的相关jar包资源 | |
/// | |
/// 外网环境下: | |
/// 1、前往 https://developer.android.com/studio#offline 下载离线组件包(Google Maven dependencies) | |
/// 2、如果是第一次操作请删除 %USER_HOME%/.android 文件夹和 %USER_HOME%/.gradle 文件夹 | |
/// 3、将下载的压缩包 offline-gmaven-stable.zip 解压到目标文件夹 %USER_HOME%/.android/manual-offline-m2/ 下 | |
/// 4、按照 %USER_HOME%/.android/manual-offline-m2/gmaven_stable/README 创建对应的 offline.gradle | |
/// 5、打开Unity,创建个空项目并成功BuildAPK | |
/// 6、运行此软件会生成 %USER_HOME%/.extractGradle-xxx 文件夹 | |
/// 7、最后将该文件夹里面的内容拷贝并覆盖到 内网环境里的目标文件夹 %USER_HOME%/.android/manual-offline-m2/gmaven_stable/ 下即可。 | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) | |
{ | |
var userHomePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); | |
userHomePath = Path.GetDirectoryName(Path.GetDirectoryName(userHomePath)); | |
var gradlePath = Path.Combine(userHomePath, ".gradle"); | |
var cacheFilesPath = Path.Combine(gradlePath, "caches/modules-2/files-2.1"); | |
var directoryInfo = new DirectoryInfo(cacheFilesPath); | |
if (!directoryInfo.Exists) | |
{ | |
// 需要运行Unity打包成功后就会自动生成该文件夹 | |
Console.WriteLine($"文件夹不存在. {cacheFilesPath}"); | |
Console.ReadKey(); | |
return; | |
} | |
// 创建目标文件夹 | |
var extractGradlePath = gradlePath.Replace(".gradle", ".extractGradle-" + DateTime.Now.ToString("yyyy.MM.dd-HH.mm.ss.fff")); | |
Directory.CreateDirectory(extractGradlePath); | |
// 源文件夹到目标文件夹的拷贝 | |
var childDirectories = directoryInfo.GetDirectories(); | |
foreach (var directory in childDirectories) | |
{ | |
var path1 = Path.Combine(extractGradlePath, directory.Name.Replace(".", "/")); | |
Directory.CreateDirectory(path1); | |
foreach (var child in directory.GetDirectories()) | |
{ | |
CopyDirectory(child.FullName, Path.Combine(path1, child.Name), true); | |
} | |
} | |
// 调整目标文件夹的jar包路径 | |
FixedJarPackagePath(new DirectoryInfo(extractGradlePath)); | |
Console.WriteLine("操作完成"); | |
Console.ReadKey(); | |
} | |
static void FixedJarPackagePath(DirectoryInfo directoryInfo) | |
{ | |
foreach (var childDirectory in directoryInfo.GetDirectories()) | |
{ | |
var files = childDirectory.GetFiles().Where(file => AllowedExtensions.Contains(file.Extension)).ToList(); | |
if (files.Count > 0) | |
{ | |
foreach (var fileInfo in files) | |
{ | |
fileInfo.CopyTo(fileInfo.FullName.Replace(childDirectory.Name, string.Empty), true); | |
} | |
childDirectory.Delete(true); | |
} | |
else | |
{ | |
FixedJarPackagePath(childDirectory); | |
} | |
} | |
} | |
static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting) | |
{ | |
bool ret = false; | |
try | |
{ | |
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; | |
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; | |
if (Directory.Exists(SourcePath)) | |
{ | |
if (Directory.Exists(DestinationPath) == false) | |
Directory.CreateDirectory(DestinationPath); | |
foreach (string fls in Directory.GetFiles(SourcePath)) | |
{ | |
FileInfo flinfo = new FileInfo(fls); | |
flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting); | |
} | |
foreach (string drs in Directory.GetDirectories(SourcePath)) | |
{ | |
DirectoryInfo drinfo = new DirectoryInfo(drs); | |
if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false) | |
ret = false; | |
} | |
} | |
ret = true; | |
} | |
catch (Exception ex) | |
{ | |
ret = false; | |
} | |
return ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment