【Unity3D】导出Android项目以及Java混淆

Android Studio 下载文件归档 | Android Developers

Android--混淆配置(比较详细的混淆规则)_android 混淆规则-CSDN博客

Unity版本:2019.4.0f1

Gradle版本:5.6.4(或5.1.1)

Gradle Plugin版本:3.4.0

Android Studio版本:3.6.3

新建空工程,转Android平台,配置情况如下:

想尝试使用Gradle 6.7.1 Gradle Plguin 4.2.0打包空项目,反反复复出问题(失败)

为什么尝试这个是因为有些Google插件要求这些版本,如果只是测试Java混淆的可以不需要这么麻烦,全部使用默认的即可。

baseProjectTemplate.gradle

复制代码
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**        
            // maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            // maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
            // mavenLocal()
            // mavenCentral()
            google()
            jcenter()
        }

        dependencies {
            // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
            // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
            // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
            // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
            classpath 'com.android.tools.build:gradle:4.2.0'
            **BUILD_SCRIPT_DEPS**
        }
    }

    repositories {**ARTIFACTORYREPOSITORY**
        // maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        // maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        // mavenLocal()
        // mavenCentral()
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

classpath 'com.android.tools.build:gradle:4.2.0',可自行找网上找到对应关系

gradle plugin 4.2.0版本对应gradle 6.7.1版本

复制代码
distributionUrl=https\://mirrors.cloud.tencent.com/gradle//gradle-6.7.1-all.zip

利用镜像地址加速下载gradle-6.7.1-all gradle压缩包,下载到C:\Users\用户名\.gradle\wrapper\dists下。

Build时发生报错:

* What went wrong:
This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.2 or newer.

调整Gradle 6.6.1,发现还是不行。调整回使用5.1.1或5.6.4推荐的gradle版本。Unity 2019.4.0默认是5.1.1版本

D:\UnityHubInstallPath\2019.4.0f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib

可通过观察这些文件后缀版本名看自己的对应多少版本或网上查询,这些版本不对应上就会出很多编译问题,即使你是空工程!!!

全部使用默认打包Android工程

只有上面的这种成功信息,不要只看CONFIGURE SUCCESSFUl,而是其他相关的警告也没有才正常,正常后就能看到Build Apk,否则是无法看到的。

解决jcenter相关的包下载不到问题 使用镜像地址 gradle plugin保持默认3.4.0

cpp 复制代码
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

allprojects {
    buildscript {
        repositories {
            // maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
             maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
            // mavenLocal()
            // mavenCentral()
            google()
            jcenter()
        }

        dependencies {
            classpath 'com.android.tools.build:gradle:3.4.0'
            
        }
    }

    repositories {
        // maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
         maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        // mavenLocal()
        // mavenCentral()
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle版本改用5.6.4-all

cpp 复制代码
#Wed Jan 08 16:45:43 CST 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
#distributionUrl=https\://mirrors.cloud.tencent.com/gradle//gradle-6.7.1-all.zip
distributionUrl=https\://mirrors.cloud.tencent.com/gradle//gradle-5.6.4-all.zip
cpp 复制代码
org.gradle.jvmargs=-Xmx4096M
org.gradle.parallel=true
android.injected.studio.version.check = false

加上这行避免一些提示你要去更新Android Studio版本的报错。

完成打包APK如下

可调整为release版本和签名形式打包apk或aab

混淆Java部分:

将false改为true,开启混淆,proguard-android.txt是默认混淆文件,不用管。

开启混淆后就会使用

复制代码
proguard-unity.txt文件进行混淆,里面有各种混淆规则
cpp 复制代码
-keep class bitter.jnibridge.* { *; }
-keep class com.unity3d.player.* { *; }
-keep interface com.unity3d.player.IUnityPlayerLifecycleEvents { *; }
-keep class org.fmod.* { *; }
-keep class com.google.androidgamesdk.ChoreographerCallback { *; }
-keep class com.google.androidgamesdk.SwappyDisplayManager { *; }
-ignorewarnings

混淆前后比对,通过Android Studio 查看apk的classes.dex

相关推荐
天空之城--5 小时前
Android逆向安全合规接单指南
android·安全
消失的旧时光-19437 小时前
Android 系统层扫盲 09:AMS、ATMS、WMS、PMS 到底分别管什么?
android·wms·pms·ams·aosp·atms
Godikov7 小时前
Android 系统级设备应用踩坑实录:sharedUserId 签名、SDK 授权失败 -4、开机自启与 U 盘 OTA 升级
android
IT毕设实战小研11 小时前
基于大数据的国内主要农作物产量趋势分析与可视化
android·java·大数据·django·课程设计
企业数字化笔记12 小时前
固定资产历史数据怎么导入系统?Excel模板、字段映射和数据校验
android·java·数据库·后端
龙之叶12 小时前
Android出海系列-GMS认证介绍
android
潮族大Z12 小时前
Android性能优化:启动、内存、卡顿的一站式排查手册
android
事圆则缓14 小时前
从suspend字节码到 Retrofit 协程桥:挂起函数识别与恢复
android·retrofit
杉氧15 小时前
RN 性能调优指南:重渲染(Re-renders)控制与长列表(FlatList)优化
android·前端·react native
Coffeeee15 小时前
claude-video 一个让你的Agent拥有看视频能力的Skill
android·人工智能·aigc