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'
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小程小程,永不消沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值