Unity 2017及以上版本支持,不用导出到Android Studio工程,直接在Unity IDE中完成分包设置。
1.开启Gradle build system 在Unity Editor, 打开Build Settings 窗口 ( File > Build Settings…) 在平台列表中, 选择 Android 设置 Build System 到 Gradle (new) 不同Unity版本,Build Settings略有差异,请灵活处理。 2.更改Gradle settings (1) 对于Unity2017.2及以上版本,可以打开 Player Settings,如下勾选 Custom Gradle Template复选框。其它版本,需要复制 mainTemplate.gradle 文件(在Unity安装目录搜索mainTemplate )到 Assets/Plugins/Android/mainTemplate.gradle。 这样就可以自定义Gradle了。
下面介绍两个问题的解决方案:
1.设置targetVersion26: 在unity2017里是不能直接指定targetVersion26的,这样的话需要修改mainTemplate.gradle:
android { *********************** defaultConfig { targetSdkVersion 26 applicationId '**APPLICATIONID**' } lintOptions { abortOnError false } ****************将targetSdkVersion 更改为26即可进行打包
2.解决65535问题: (1) 在defaultConfig中添加multiDexEnabled true
defaultConfig { targetSdkVersion **TARGETSDKVERSION** applicationId '**APPLICATIONID**' multiDexEnabled true }(2) 在dependencies中添加com.android.support:multidex:1.0.1 一般当前应用的Android API最低版本都在20以下,因此需要在dependencies中添加com.android.support:multidex:1.0.1。
dependencies { compile 'com.android.support:multidex:1.0.1' compile fileTree(dir: 'libs', include: ['*.jar']) }(3) 删除minifyEnabled与useProguard参数 如果出现shrinking/minification is not supported with Multidex错误,请删除useProguard参数,甚至进一步删除minifyEnabled参数。 如下所示:
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** } }(4).初始化Multidex 如果 Assets/Plugins/Android/不存在AndroidManifest.xml,请从Unity的安装目录复制默认的AndroidManifest.xml到此目录。如果AndroidManifest.xml的application标签中没有为android:name设置MultiDexApplication或MultiDexApplication的子类,请添加android.support.multidex.MultiDexApplication。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:name="android.support.multidex.MultiDexApplication" > ... </application> </manifest>