【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

相关推荐
黎猫大侠3 小时前
一次Android Fragment内存泄露的bug解决记录|Fragment not attach to an Activity
android·bug
袁震5 小时前
android HashMap和List该如何选择
android·hashmap·sparsearray
xiaogai_gai7 小时前
高效管理钉钉收款单数据集成到MySQL的技术方案
android·mysql·钉钉
_extraordinary_7 小时前
MySQL 索引(一)
android·数据库·mysql
gjc5927 小时前
MySQL OCP试题解析(2)
android·数据库·mysql·开闭原则
Watink Cpper8 小时前
[Linux]多线程(二)原生线程库---pthread库的使用
android·linux·运维·原生线程库·pthread库
笨鸭先游13 小时前
前台--Android开发
android
fareast_mzh13 小时前
Lightweight App Alternatives
android
pq113_617 小时前
OrangePi Zero 3学习笔记(Android篇)4 - eudev编译(获取libudev.so)
android·笔记·学习
鸿蒙布道师21 小时前
鸿蒙NEXT开发动画案例3
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei