unity 兼容 androidx

报错

ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.3.1] AndroidManifest.xml:24:18-86
	is also present at [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
	Suggestion: add ‘tools:replace="android:appComponentFactory"‘ to <application> element at AndroidManifest.xml:10:3-27:17 to override.

 

环境

  gradle:3.4.3  

 

原因

  需要支持androidx

 

解决方式:

unity 2017

([rootProject] + (rootProject.subprojects as List)).each {
	ext {
		it.setProperty("android.useAndroidX", true)
		it.setProperty("android.enableJetifier", true)
	}
}

demo:

buildscript {
	repositories {
		google()
		jcenter()
	}

	dependencies {
		classpath ‘com.android.tools.build:gradle:3.2.1‘
		classpath ‘com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta02‘ //Jetifier workaround
	}
}

allprojects {
   repositories {
      google()
      jcenter()
      flatDir {
        dirs ‘libs‘
      }
   }
}

([rootProject] + (rootProject.subprojects as List)).each {
	ext {
		it.setProperty("android.useAndroidX", true)
		it.setProperty("android.enableJetifier", true)
	}
}

apply plugin: ‘com.android.application‘

dependencies {
	implementation fileTree(dir: ‘libs‘, include: [‘*.jar‘])
**DEPS**}

android {
	compileSdkVersion **APIVERSION**
	buildToolsVersion ‘**BUILDTOOLS**‘

	defaultConfig {
		minSdkVersion **MINSDKVERSION**
		targetSdkVersion **TARGETSDKVERSION**
		applicationId ‘**APPLICATIONID**‘
		ndk {
            abiFilters **ABIFILTERS**
        }
		versionCode	**VERSIONCODE**
		versionName	‘**VERSIONNAME**‘
		multiDexEnabled true
	}

	dexOptions {
		jumboMode true
		javaMaxHeapSize "4g"
	}

	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**
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}


**SOURCE_BUILD_SETUP**

  

unity 2018

IPostGenerateGradleAndroidProject

public class AndroidPostBuildProcessor : IPostGenerateGradleAndroidProject
{
    public int callbackOrder
    {
        get
        {
            return 999;
        }
    }


    void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path)
    {
        Debug.Log("Bulid path : " + path);
        string gradlePropertiesFile = path + "/gradle.properties";
        if (File.Exists(gradlePropertiesFile))
        {
            File.Delete(gradlePropertiesFile);
        }
        StreamWriter writer = File.CreateText(gradlePropertiesFile);
        writer.WriteLine("org.gradle.jvmargs=-Xmx4096M");
        writer.WriteLine("android.useAndroidX=true");
        writer.WriteLine("android.enableJetifier=true");
        writer.Flush();
        writer.Close();

    }
}

  

参考

https://*.com/questions/54186051/is-there-a-way-to-change-the-gradle-properties-file-in-unity/58004419#58004419

https://github.com/googlesamples/unity-jar-resolver/issues/241#issuecomment-514995546

 

unity 兼容 androidx

上一篇:Android Intent用法总结


下一篇:移动端学习(1)