关于Unity2020版本打包安卓工程IL2CPP没有SO的解决方案(增加2021的说明)

关于Unity2020版本打包安卓工程IL2CPP没有SO的解决方案

---------------2022.09.27增加Unity2021版本打包IL2CPP的说明-----------------------------------------

升级2022.1.115版本后要注意的:

添加到build.gradle里的BuildIL2Cpp生成.so库的任务需要修改路径,因为deploy下面的.netcoreapp3.1文件夹没了

commandLine(workingDir + "/src/main/Il2CppOutputProject/IL2CPP/build/deploy/il2cpp.exe",

 注释掉下面这句,因为没了这个文件。

"--map-file-parser=" + workingDir + "/src/main/Il2CppOutputProject/IL2CPP/MapFileParser/MapFileParser.exe",

---------------2022.09.27更新的内容----------------------------------------------------------------------------------------------

2020版本之后,Unity打包安卓工程Export Project没有IL2CPP.SO这个库,

但是却把这个库的源码工程给出来了。

 

如果本身就有自己写好的安卓工程,替换bin和库后,编出来的包是没法运行的。

但是,不要骂Unity、不要骂Unity、不要骂Unity。

因为,这种方式,为so库的二次加密,提供了便捷,这里就不说加密相关的事情了。

步入正题。

在unityLibrary的build.gradle里加入一段代码,用于将IL2CPP编成库。

只需将这段代码加入进来,则无须再手段编库了,按正常出包就行。

def getSdkDir() {
    Properties local = new Properties()
    local.load(new FileInputStream("${rootDir}/local.properties"))
    return local.getProperty('sdk.dir')
}

def BuildIl2Cpp(String workingDir, String targetDirectory, String architecture, String abi, String configuration) {
    exec {
        commandLine(workingDir + "/src/main/Il2CppOutputProject/IL2CPP/build/deploy/netcoreapp3.1/il2cpp.exe",
                "--compile-cpp",
                "--libil2cpp-static",
                "--platform=Android",
                "--architecture=" + architecture,
                "--configuration=" + configuration,
                "--outputpath=" + workingDir + targetDirectory + abi + "/libil2cpp.so",
                "--cachedirectory=" + workingDir + "/build/il2cpp_"+ abi + "_" + configuration + "/il2cpp_cache",
                "--additional-include-directories=" + workingDir + "/src/main/Il2CppOutputProject/IL2CPP/external/bdwgc/include",
                "--additional-include-directories=" + workingDir + "/src/main/Il2CppOutputProject/IL2CPP/libil2cpp/include",
                "--tool-chain-path=" + android.ndkDirectory,
                "--map-file-parser=" + workingDir + "/src/main/Il2CppOutputProject/IL2CPP/MapFileParser/MapFileParser.exe",
                "--generatedcppdir=" + workingDir + "/src/main/Il2CppOutputProject/Source/il2cppOutput",
                "--baselib-directory=" + workingDir + "/src/main/jniStaticLibs/" + abi,
                "--dotnetprofile=unityaot")
        environment "ANDROID_SDK_ROOT", getSdkDir()
    }
    delete workingDir + targetDirectory + abi + "/libil2cpp.sym.so"
    ant.move(file: workingDir + targetDirectory + abi + "/libil2cpp.dbg.so", tofile: workingDir + "/symbols/" + abi + "/libil2cpp.so")
}

android {
    task BuildIl2CppTask {
        doLast {
            BuildIl2Cpp(projectDir.toString().replaceAll('\\\\', '/'), '/src/main/jniLibs/', 'ARMv7', 'armeabi-v7a', 'Release');
        }
    }
    afterEvaluate {
        if (project(':unityLibrary').tasks.findByName('mergeDebugJniLibFolders'))
            project(':unityLibrary').mergeDebugJniLibFolders.dependsOn BuildIl2CppTask
        if (project(':unityLibrary').tasks.findByName('mergeReleaseJniLibFolders'))
            project(':unityLibrary').mergeReleaseJniLibFolders.dependsOn BuildIl2CppTask
    }
    sourceSets {
        main {
            jni.srcDirs = ["src/main/Il2CppOutputProject"]
        }
    }
}

 

posted @ 2022-08-29 11:10  威少小二orz  阅读(4208)  评论(6编辑  收藏  举报