[Unity] -Unity5.6.3f1 use Gradle hit the Android package - filled pit

Background & Objective Questions &

  • Background: The old project has been packaged with the Internal Unity5.6.3f1 default mode, combined with Jenkins build. Construction of a new project using 2018.4.2f1
  • Problem: When the project millet access the latest version of SDK, access the Jar package a lot, met a number of methods than 64K problems of using Unity Innternal a packaging can not be packaged successfully; If you choose to use Export Gradle Project manner, automated processes large changes ; only choose to use Unity Gradle is packaged.
  • Record Unity5.6.3f1 using Unity Gradle packaging process and solve the problems encountered.

step

  • New Unity empty project (recommended for small test project test), used in the project to import the Plugins / Android directory contents, while import millet SDK resources, Build Settings Build System in select Gradle.
  • Unity5.6.3f1 from the installation directory "Unity \ Editor \ Data \ PlaybackEngines \ AndroidPlayer \ Tools \ GradleTemplates", get to mainTemplate.gradle and libTemplate.gradle two files copied to the new project in the above
  • Modify mainTemplate.gradle, added within buildscript / repositories and allprojects / repositories objects
repositories {
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
//注意保留原来的内容
...
}
  • And modifying mainTemplate.gradle libTemplate.gradle, modify buildscript / dependencies object classpath 'com.android.tools.build:gradle:2.1.0' version number of gradle 2.3.3
 dependencies {
  classpath 'com.android.tools.build:gradle:2.3.3'
 }
  • Modify mainTemplate.gradle, add in dependencies objects
dependencies {
//注意保留原来的内容
...
 compile 'com.android.support:multidex:1.0.3'
}

Add in android / defaultConfig object

android {
 defaultConfig {
//注意保留原来的内容
...
  multiDexEnabled true
 }
}
  • Gradle updated version of Unity 5.6.3f1
* Unity 5.6.3f1中集成的Gradle版本是 2.1.0
* Unity 2018.4.2f1集成的Gradle版本是 4.6.0
分别本地安装的找到Unity 5.6.3f1和Unity 2018.x的安装目录“Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle”中的lib文件,先备份5.6.3f1的lib文件,再将2018.x的lib复制过来。
  • Jenkins project configuration: Configuration Andrews signed the Unity player settings
//例如
PlayerSettings.Android.keystoreName = KeystorePath;
PlayerSettings.Android.keyaliasName = "alias";
PlayerSettings.Android.keyaliasPass = "123456";
PlayerSettings.Android.keystorePass = "123456"; 
  • Jenkins project configuration: Configuration BundleId in the Unity player settings
//例如
PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, BundleName);
  • Jenkins project configuration: Select the Unity build settings in Gradle
//例如
 EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
  • Integration SDK provides the millet Jar package
使用ANT整合打包小米提供的Jar  (命名格式:support-xxx-27.1.1.jar)
和工程内现有的android-support-v4.jar进行对比
删除从小米导出的Jar里的对比相同的类 (尽量保证工程内现有使用android-support-v4.jar的功能稳定,只增加一个小米的不稳定因素)
尝试打包

problem solved

  • If you encounter when packing fails the test, the next attempt to pack, remember to delete the Temp directory under the project directory Unity
  • Without open proxy, encountered problems can not get gradle
摘自下文参考链接中的类似日志
* What went wrong:
A problem occurred configuring root project 'gradleOut'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:2.1.0.
     Required by:
         project :
      > Could not resolve com.android.tools.build:gradle:2.1.0.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.1.0/gradle-2.1.0.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.1.0/gradle-2.1.0.pom'.
               > Connect to XXX [/XXX] failed: Connection refused: connect
    * 解决方法:
    配置阿里云国内镜像
  • When using the default execution packaged version Unity5.6.3f of gradle
* What went wrong:
A problem occurred evaluating root project 'gradleOut'.
> Failed to apply plugin [id 'com.android.application']
   > Gradle version 2.10 is required. Current version is 4.0.1. If using the gradle wrapper, try editing the distributionUrl in E:\Unity3D\TTAiLaoyu\Temp\gradleOut\gradle\wrapper\gradle-wrapper.properties to gradle-2.10-all.zip
    * 解决方法:
拷贝2018版本的Unity的gradle到5.6.3f1下,并修改.gradle文件的gradle版本号2.3.3
如果使用AndroidStudio,方法一样,也是更新最新的gradle解决这个问题的版本
如:使用最新的AndroidStudio3.0
buildscript {
repositories {
jcenter() 
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
    注:如果使用的是AndroidStudio2.3或更低版本,请使用
classpath 'com.android.tools.build:gradle:2.3.3'
  • Repeat encountered jar bags:
duplicate entry: com/bumptech/glide/gifdecoder/GifDecoder$BitmapProvider.class 类似日志
    解决方法:尝试删除新导入jar包中的同包名+类名的类文件
  • Configure your application for multi-dex file:
当 minSdkVersion 低于20及以下时,注意配置三点
android {
        defaultConfig {
            ...
            minSdkVersion 15
            targetSdkVersion 28
           // 1
             multiDexEnabled true
        }
        ...
    }
    dependencies {
           // 2
      compile 'com.android.support:multidex:1.0.3'
    }

    // 3
不替换Application
  <?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>
替换Application
 public class MyApplication extends MultiDexApplication { ... }
替换Application 但无法修改基类
public class MyApplication extends SomeOtherApplication {
      @Override
      protected void attachBaseContext(Context base) {
         super.attachBaseContext(base);
         MultiDex.install(this);
      }
    }

Summary & Follow-up

  • Unity packed error message, look carefully positioning the core issue, depending on the problem, find out how to solve them; for Gradle syntax and some common problems need to record and organize.
  • Dex multi-file support library has some known limitations will be incorporated into your compiled application configuration, you should be aware of these limitations and targeted testing:
启动期间在设备的数据分区上安装 DEX 文件的过程相当复杂,如果辅助 DEX 文件较大,可能会导致应用无响应 (ANR) 错误。在这种情况下,您应通过 ProGuard 应用代码压缩,以尽量减小 DEX 文件的大小,并移除未使用的那部分代码。
当运行的版本低于 Android 5.0(API 级别 21)时,使用多 dex 文件不足以避开 linearalloc 限制(问题 78035)。此上限在 Android 4.0(API 级别 14)中有所提高,但这并未完全解决该问题。在低于 Android 4.0 的版本中,您可能会在达到 DEX 索引限制之前达到 linearalloc 限制。因此,如果您的目标 API 级别低于 14,请在这些版本的平台上进行全面测试,因为您的应用可能会在启动时或加载特定类组时出现问题。

Reference & thanks

Guess you like

Origin www.cnblogs.com/juxing-blog/p/11725466.html