注意:本文都是针对使用Gradle编译
从Unity2018.4.x到Unity2020.3.x都是可以直接将Android的适配资源直接放到${PROJECT_PATH}/Assets/Plugins/Android/对应的目录下的,如:在此目录下可以方安卓平台对应的assets、res目录及子目录资源,打包时Unity会自动将这些资源文件打入到最终的游戏包中,但从Unity2020.3.x版本开始已经出现警告,在Unity2021.3.x版本中直接编译不过。针对不同版本的兼容处理如下:
Unity2018.4.x:
在Unity编辑器左上角找到 File -> Build Settings... -> Player Settings...(弹框中) -> Publishing Settings 然后在里面找到 Custom Main Gradle Template选项和Custom Gradle Properties Template选项,两个选项都勾选上,则在
${PROJECT_PATH}/Assets/Plugins/Android/目录下会生成 mainTemplate.gradle和gradleTemplate.properties文件,我们可以根据自己的需要在指定的位置加入maven 远端仓库引入,如下:
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
**BUILD_SCRIPT_DEPS**}
}
allprojects {
repositories {
google()
mavenCentral()
flatDir {
dirs 'libs'
}
// Publish SDK maven repository.
maven {
url "http://10.1.4.209:8081/repository/maven-public/"
}
// BI SDK maven repository
maven {
url "http://10.1.4.64:8081/repository/maven-public/"
}
}
}
apply plugin: 'com.android.application'
**APPLY_PLUGINS**
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Publish sdk library
implementation 'com.esdk.sdk:global-api:1.2.4'
implementation 'com.esdk.sdk:unity-bridge:1.2.2'
// BI SDK library
implementation 'com.esdk.sdk:data-bi:1.2.4'
**DEPS**}
android {
compileSdkVersion **APIVERSION**
buildToolsVersion '**BUILDTOOLS**'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion **MINSDKVERSION**
targetSdkVersion **TARGETSDKVERSION**
applicationId '**APPLICATIONID**'
ndk {
abiFilters **ABIFILTERS**
}
versionCode **VERSIONCODE**
versionName '**VERSIONNAME**'
multiDexEnabled true
}
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
}
lintOptions {
abortOnError false
}
aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
}**SIGN**
buildTypes {
debug {
minifyEnabled **MINIFY_DEBUG**
useProguard **PROGUARD_DEBUG**
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
jniDebuggable true
}
release {
minifyEnabled **MINIFY_RELEASE**
useProguard **PROGUARD_RELEASE**
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
}
}**PACKAGING_OPTIONS****SPLITS**
**BUILT_APK_LOCATION**
bundle {
language {
enableSplit = false
}
density {
enableSplit = false
}
abi {
enableSplit = true
}
}
}**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**
在gradleTemplate.properties文件中配置上androidx的支持,如下:
org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
unityStreamingAssets=.unity3d**STREAMING_ASSETS**
# AndroidX config
android.useAndroidX=true
android.enableJetifier=true
**ADDITIONAL_PROPERTIES**
${PROJECT_PATH}/Assets/Plugins/Android/目录下的结构如图:
Unity2018.4.x -> Unity2019.4.x
在Unity编辑器左上角找到 File -> Build Settings... -> Player Settings...(弹框中) -> Publishing Settings 然后勾选如下图选项
我们发现在${PROJECT_PATH}/Assets/Plugins/Android/目录下会生成文件变多了,我们重点关注mainTemplate.gradle、baseProjectTemplate.gradle、launcherTemplate.gradle文件,我们会发现跟Unity2018.4.x相比差异:
把原mainTemplate.gradle文件中关于项目的配置抽离到了baseProjectTemplate.gradle中了
mainTemplate.gradle变成了依赖moudule。
项目App插件抽离到了launcherTemplate.gradle文件中了。
备注:我们默认还是将自己所需依赖的maven远端仓库在mainTemplate.gradle中声明,当然对于熟悉Android的同学将依赖声明到launcherTemplate.gradle中也是可以的。
baseProjectTemplate.gradle
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
allprojects {
buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
mavenCentral()
}
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.0.1'
// classpath 'com.google.gms:google-services:4.3.3'
// Add the Crashlytics Gradle plugin.
// classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
**BUILD_SCRIPT_DEPS**
}
}
repositories {**ARTIFACTORYREPOSITORY**
google()
mavenCentral()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
// Publish SDK maven repository.
maven {
url "http://10.1.4.209:8081/repository/maven-public/"
}
// BI SDK maven repository.
maven {
url "http://10.1.4.64:8081/repository/maven-public/"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
mainTemplate.gradle
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
apply plugin: 'com.android.library'
**APPLY_PLUGINS**
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Publish sdk library
implementation 'com.esdk.sdk:global-api:1.2.4'
implementation 'com.esdk.sdk:unity-bridge:1.2.2'
// BI SDK library
implementation 'com.esdk.sdk:data-bi:1.2.4'
**DEPS**}
android {
compileSdkVersion **APIVERSION**
buildToolsVersion '**BUILDTOOLS**'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion **MINSDKVERSION**
targetSdkVersion **TARGETSDKVERSION**
ndk {
abiFilters **ABIFILTERS**
}
versionCode **VERSIONCODE**
versionName '**VERSIONNAME**'
consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD**
}
lintOptions {
abortOnError false
}
aaptOptions {
noCompress = ['.ress', '.resource', '.obb'] + unityStreamingAssets.tokenize(', ')
ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
}**PACKAGING_OPTIONS**
}**REPOSITORIES**
**IL_CPP_BUILD_SETUP**
**SOURCE_BUILD_SETUP**
**EXTERNAL_SOURCES**
最终的目录结构如下:
Unity2019.4.x -> Unity2020.3.x
这个升级过程中目录结构不变,但是编译会有警告,提示如assets、res等资源目录及目录下的文件不要放在${PROJECT_PATH}/Assets/Plugins/Android/目录下,建议打成aar资源包单独引用。升级后跟Unity2019.4.x的差异:
baseProjectTemplate.gradle 文件中引用的 AGP(Android gradle plugin)版本从3.4.0 升级到了4.0.1了,因为此APG依赖的Gradle版本为也升级了,而新的Gradle版本引用远程Maven库默认支持https,若用http则需要加一个allowInsecureProtocol = true 声明。
assets、res等资源默认及目录下的文件建议打成aar资源包,或依赖moudule方式。
处理方法:
baseProjectTemplate.gradle
// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN
allprojects {
buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
mavenCentral()
}
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.0.1'
**BUILD_SCRIPT_DEPS**
}
}
repositories {**ARTIFACTORYREPOSITORY**
google()
mavenCentral()
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
// Publish SDK maven repository.
maven {
allowInsecureProtocol = true
url "http://10.1.4.209:8081/repository/maven-public/"
}
// BI SDK maven repository.
maven {
allowInsecureProtocol = true
url "http://10.1.4.64:8081/repository/maven-public/"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
而资源文件会有如下警告
OBSOLETE - Providing Android resources in Assets/Plugins/Android/res is deprecated, please move your resources to an AAR or an Android Library. See "AAR plug-ins and Android Libraries" section of the Manual for more details.
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
我们可以忽略警告直接打出Andorid包,当然也可以使用下面的版本进行处理,因为在Unity2020.3.x版本中还是非强制的,而在Unity2021.3.x版本中已经是强制的了。
Unity2020.3.x -> Unity2021.3.x
当Unity升级到2021.3.x版本后Android的assets、res等资源就从警告变为编译不过了,我们必须处理,处理方式提示有两种:
方式一:将资源打成aar包放在Android目录下或Android/libs目录下,网上大多数都是此方法处理的。
方式二:(推荐)
在${PROJECT_PATH}/Assets/Plugins/Android/目录下创建一个资源文件夹,文件夹命名要要.androidlib结尾,如:我将资源文件夹命名为main-resource.androidlib, 此目录下需要存放一个build.gradle文件,build.gradle文件中内容如下:
build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 19
targetSdkVersion 30
consumerProguardFiles 'proguard-rules.pro'
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
}
注意: compileSdkVersion 和 targetSdkVersion 的值根据游戏自己编译支持的最高Andriod版本保持一致
在main-resource.androidlib文件加下一次创建src/main/ 目录结构,然后在main文件加下存放一个AndroidManifest.xml文件,如下:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.esdk.game.resource">
</manifest>
建议package的值使用 ${游戏包名}.resource , 非强制。
将之前版本中的assets、res等资源原封不动的移动到main文件夹下即可。
最终${PROJECT_PATH}/Assets/Plugins/Android/main-resource.androidlib/src/main/ 下的结构为:
调整完成后我们就可以正常打出带自定义Android资源的游戏包了。
完成示例请参考:https://github.com/changcsw/UnityMultiVersionForAndroid