Unity项目如何加入AndroidX依赖,解决APK运行闪退问题?

环境:Unity2020.3.0 / Android3.4.1

Gradle 5.6.4 (Unity端)
Gradle 5.1.1 (安卓端)

问题:安卓supportV4升级成AndroidX,打出的APK运行闪退。

安卓supportV4升级成AndroidX,导出aar给Unity,Unity打包出APK,APK运行闪退。报错如下:
在这里插入图片描述
在这里插入图片描述

解决方法步骤:

在unity的.gradle文件 和 android 的.gradle 都要添加远程依赖,同时修改gradleTemplate.properties文件, 允许Unity引擎打包时添加AndroidX依赖。

具体步骤:

1、在AS的build.gradle文件中添加AndroidX依赖

	api 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    implementation 'io.grpc:grpc-okhttp:1.14.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.14.0'
    implementation 'io.grpc:grpc-stub:1.14.0'
    implementation 'com.google.protobuf:protobuf-lite:3.0.1'

2、修改gradleTemplate.properties文件, 允许Unity引擎打包时添加AndroidX依赖。

文件地址:unity安装路径下2020.3.0f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates\gradleTemplate.properties
在这里插入图片描述

修改内容:添加如下两行:

android.useAndroidX=true
android.enableJetifier=true

在这里插入图片描述

2、在Unity项目的mainTemplate.gradle配置文件中添加AndroidX依赖

mainTemplate.gradle模板获取方法:在ProjectSetting\Player\PublishingSetting中勾选CustomMainGradleTemplate,生成.gradle文件模板,然后在Assets\Plugins\Android路径下打开此配置文件,在如下位置添加如下代码
在这里插入图片描述

	api 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'javax.annotation:javax.annotation-api:1.2'
    implementation 'io.grpc:grpc-okhttp:1.14.0'
    implementation 'io.grpc:grpc-protobuf-lite:1.14.0'
    implementation 'io.grpc:grpc-stub:1.14.0'
    implementation 'com.google.protobuf:protobuf-lite:3.0.1'

猜你喜欢

转载自blog.csdn.net/qq_43505432/article/details/118153053