我有一个Android项目,其中也必须包含一个Unity项目。在没有导入Unity项目的情况下,我可以成功地生成发行版构建APK(签名APK)。但是在包含Unity项目后,我就面临这个问题。
随后出现了4个错误
Program type already present: androidx.arch.core.internal.SafeIterableMap$Entry
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
Caused by: com.android.tools.r8.utils.AbortException: Error: Program type already present: androidx.arch.core.internal.SafeIterableMap$Entry
Error: Program type already present: androidx.arch.core.internal.SafeIterableMap$Entry
我将Unity项目导入到Android项目中的步骤:
按原生安卓应用水平gradle是。
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "application.package.name"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
// arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
cppFlags "-std=c++11", "-frtti", "-fexceptions"
}
}
ndk {
abiFilters 'armeabi-v7a'/*, 'arm64-v8a'*/
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
// ndkBuild {
// path 'src/main/jni/Android.mk'
// }
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
aaptOptions {
noCompress "tflite"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
configurations {
compile.exclude group: 'androidx.annotation', module: 'annotation'
// added after getting com.android.tools.r8.errors.CompilationError: Program type already present: androidx.annotation.AnimRes
}
repositories {
maven {
url 'https://google.bintray.com/tensorflow'
}
flatDir {
dirs 'libs'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(name: 'tensorflow-lite', ext: 'aar')
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
implementation "android.arch.core:runtime:1.1.1"
implementation "android.arch.core:common:1.1.1"
implementation project(':openCVLibrary341')
// compile 'org.tensorflow:tensorflow-lite:+'
implementation project(':macelibrary')
testImplementation 'junit:junit:4.12'
implementation(project(':unity')){
exclude group: 'androidx.arch.core' // added after reading the below linked question
}
}
Unity项目的gradle文件。
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
}
allprojects {
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
}
}
}
apply plugin: 'com.android.library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
android {
compileSdkVersion 29
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
ndk {
abiFilters 'armeabi-v7a'
}
versionCode 1
versionName '0.1'
}
lintOptions {
abortOnError false
}
aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb']
ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
}
/*buildTypes {
debug {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
signingConfig signingConfigs.debug
jniDebuggable true
}
release {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
signingConfig signingConfigs.debug
}
}*/
packagingOptions {
doNotStrip '*/armeabi-v7a/*.so'
}
buildTypes {
release {
multiDexEnabled = true
}
}
}
这个问题是这个问题的重复 疑问但原问题没有答案,我也没有足够的信誉去评论和询问这个问题。唯一不同的是,看完这个问题后,我从Unity项目中删除了Firebase,并尝试生成签名的APK,这也没有用,同样的错误。
请帮我解决这个问题。先谢谢你了!
我设法解决了这个问题,把androidx.*库从安卓项目中删除。libs 导出的unity项目的目录。
androidx.*库已经被包含在原生android项目中。在将Unity项目作为库收录的同时,库中也有这些androidx.*库,这些都是导致这个问题的原因。
原来,在将Unity项目导出为原生Android项目时,Unity默认包含了这些androidx.*库。只有当我们想从该项目中构建一个APK时,才需要这些库。但如果要从其中构建一个库,这些androidx.*库是不需要的。