Reputation: 85
I am trying to build an android plugin for unity3d that simply share a screenshot to another app via the FileProvider.
I migrated my project to androidx.
I have used androidx.core.content.FileProvider class.
My app crashes whenever I launch it, with the following Logcat:
2018-12-22 21:46:13.678 14755-14755/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anascompany.testandroidplugin, PID: 14755
java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk"],nativeLibraryDirectories=[/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/lib/x86, /data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk!/lib/x86, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:6396)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk"],nativeLibraryDirectories=[/data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/lib/x86, /data/app/com.anascompany.testandroidplugin-bwZMktz2HTL449ojsD7y9g==/base.apk!/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
I have searched about the problem and tried the following:
1. MultiDex problem.
In order to include MultiDex easily in my project I changed the minimum SDK to API 23 Marshmallow 6.0.
Where the Android documentation said that to include MultiDex you need to add multiDexEnabled true
in defaultConfig in the build.gradle file for the specified module.
Note: the documentation said that this is the only step required to include MultiDex if the minimum SDK is set to API 21 or higher.
2. wrong package problem (I guess that's the problem).
prior to androidx we used this package: com.android.support:support-v4
so according to this page you should replace the previous package with: androidx.legacy:legacy-support-v4:1.0.0
and also replaced android.support.v4.content.FileProvider
with androidx.core.content.FileProvider
.
here is my android manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anascompany.unity">
<application>
<provider
android:authorities="com.anascompany.unity.fileprovider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
and here is my build.gradle file for my module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
task copyPlugin(type: Copy){
dependsOn assemble
from ('build/outputs/aar')
into ('../../Assets/Plugins/Android')
include (project.name + '-release.aar')
}
Note: the target SDK is both in unity and android studio set to 28 "Android Pie".
Note: my question is different from this one because I have already included in the manifest androidx.core.content.FileProvider
Upvotes: 6
Views: 19881
Reputation: 169
I had the same initialization error. I changed the Code Shrinker property in Android Project Property from ProGuard and R8 to EMPTY. This solved the issue. Just remove the ProGuard property and it works. I will try to find another workaround later but for now, this is fine with me.
Upvotes: 0
Reputation: 61
if you want to use the newer implementation (androidx.core.content.FileProvider) and it still crashes you might as well need to put the library manually into the project.
I downloaded the aar file (https://mvnrepository.com/artifact/androidx.core/core/1.0.1) and since i was working on a unity3d project i put it into the folder "Assets\Plugins\Android\libs". For Android it might be "Android\lib".
Hope it was helpful for someone
Upvotes: 1
Reputation: 356
Finally I got the solution, it´s so so simple. As simple as copy manually "support-v4-24.1.1.aar" into the plugin unity project folder. Or other support aar android file. It comes with the SDK. In my case the file was in "C:\AndroidSDK\extras\android\m2repository\com\android\support\support-v4\24.1.1"
No matter the version but you should be sure that "FileProvider" class is inside the file. You can unzip the aar file simplily with winrar and be sure that the class is in there.
---UPDATED---
In summary: inside /yourProjectFolder/Assets/Plugins copy your unity-release.aar compiled android module and manually too the support-v4-24.1.1.aar (or other versión, the size of mine is near 1MB)
Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.unity">
<application>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.unity.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>
And here my module build.gradle:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
I hope it helps you and other.
Upvotes: 3
Reputation: 499
Change to
implementation 'androidx.core:core:1.0.1'
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Upvotes: 3