Cordova and proguard : Camera Plugin NPE on takePicture

2.1k views Asked by At

sorry if someone already asked for this. I'm very new on Stack, and i didn't found a solution.

Here is the stack i get form the usage of my app after I minified it.

08-29 13:27:30.330 10037-10192/? E/PluginManager: Uncaught exception from plugin
                                              java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                  at android.support.v4.content.FileProvider.parsePathStrategy(Unknown Source)
                                                  at android.support.v4.content.FileProvider.getPathStrategy(Unknown Source)
                                                  at android.support.v4.content.FileProvider.getUriForFile(Unknown Source)
                                                  at org.apache.cordova.camera.CameraLauncher.takePicture(Unknown Source)
                                                  at org.apache.cordova.camera.CameraLauncher.callTakePicture(Unknown Source)
                                                  at org.apache.cordova.camera.CameraLauncher.execute(Unknown Source)
                                                  at org.apache.cordova.CordovaPlugin.execute(Unknown Source)
                                                  at org.apache.cordova.PluginManager.exec(Unknown Source)
                                                  at org.apache.cordova.CordovaBridge.jsExec(Unknown Source)
                                                  at org.apache.cordova.engine.SystemExposedJsApi.exec(Unknown Source)
                                                  at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
                                                  at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:7)
                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                  at android.os.Looper.loop(Looper.java:154)
                                                  at android.os.HandlerThread.run(HandlerThread.java:61)

This is my proguard file :

    # ionic
-keep class org.apache.cordova.** { *; }
-keep class org.apache.cordova.camera.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin
-keep class com.ionic.keyboard.IonicKeyboard.** { *; }

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

# Not sure if needed, found it in several documentations
-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

#-libraryjars ./libs/android-support-v4.jar
-dontwarn android.support.v4.**

-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
-keep class android.support.v4.content.FileProvider

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-dontwarn com.worklight.common.internal.WLTrusteerInternal*
-dontwarn com.worklight.jsonstore.**
-dontwarn org.codehaus.jackson.map.ext.*
-dontwarn com.worklight.androidgap.push.GCMIntentService
-dontwarn com.worklight.androidgap.plugin.WLInitializationPlugin
-dontwarn android.net.SSLCertificateSocketFactory
-dontwarn android.net.http.*

-keepattributes *Annotation*

If i use the "choose from the gallery" item, all is ok, but every time i try to use the camera i get this error.

For the android manifest i have this :

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="24" android:versionName="2.4" package="" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            android:name="android.support.v4.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

</manifest>

Notice that i removed the package name from the manifest (for this post on stack only, in my build there is the name)

If i remove proguard with minifyenabled false, the app is ok and deals with the camera.

I dont know what is shrinked and i should add to keep ? Or perhaps have you a hint / tips to found what is bad in my method?

Thx.

4

There are 4 answers

3
MSD On BEST ANSWER

If anyone is still stuck on this

Add this line to Your proguard rules

-keep class com.abc.xyz.BuildConfig { *; }

where com.abc.xyz is your Package name

that's it ......

thank me later...

happy coding

1
onur On

Hi i solved same problem. are there any classnotfounderror? In my problem there were a classnotfoundexception bottom of the NPE. After I added that unfound class in proguard-rules , Problem was solved.

My problem log like java.lang.ClassNotFoundException: com.xyz.XYZ.BuildConfig

Uncaught exception from plugin java.lang.NullPointerException: 
Attempt to invoke virtual method 'android.content.res.XmlResourceParser 
android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                                       at android.support.v4.content.FileProvider.parsePathStrategy(Unknown Source)
                                                                       at android.support.v4.content.FileProvider.getPathStrategy(Unknown Source)
                                                                       at android.support.v4.content.FileProvider.getUriForFile(Unknown Source)
                                                                       at org.apache.cordova.camera.CameraLauncher.takePicture(Unknown Source)
                                                                       at org.apache.cordova.camera.CameraLauncher.callTakePicture(Unknown Source)
                                                                       at org.apache.cordova.camera.CameraLauncher.execute(Unknown Source)
                                                                       at org.apache.cordova.CordovaPlugin.execute(Unknown Source)
                                                                       at org.apache.cordova.PluginManager.exec(Unknown Source)
                                                                       at org.apache.cordova.CordovaBridge.jsExec(Unknown Source)
                                                                       at org.apache.cordova.engine.SystemExposedJsApi.exec(Unknown Source)
                                                                       at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
                                                                       at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:7)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:145)
                                                                       at android.os.HandlerThread.run(HandlerThread.java:61)
09-03 11:38:37.630 6926-6926/com.xyz.XYZ D/SystemWebChromeClient: file:///android_asset/www/js/bundles/fileUploadMobile_bundle.js: Line 1 : Failed because: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
09-03 11:38:37.630 6926-6926/com.xyz.XYZ I/chromium: [INFO:CONSOLE(1)] "Failed because: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference", source: file:///android_asset/www/js/bundles/fileUploadMobile_bundle.js (1)
09-03 11:38:37.690 6926-9554/com.xyz.XYZ D/BuildHelper: Unable to get the BuildConfig, is this built with ANT?
09-03 11:38:37.690 6926-9554/com.xyz.XYZ W/System.err: **java.lang.ClassNotFoundException: com.xyz.XYZ.BuildConfig**

After that add that line in proguard-rules

-keep public class com.xyz.XYZ.BuildConfig
    
-keepclassmembers class com.xyz.XYZ.BuildConfig {
       public *;
    }
1
Prasanth Nath On

Add this in Manifest file, Inside the application tag. I tried this it worked

Here is the provider section in my Manifest xml file:

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>
2
Shiva Kumar H R On

I too was facing the same problem, and the following resolved it for me:

Update the config.xml file.

  • Open the config.xml file that is in your project folder.
  • Inside the <platform name="android"> tag, add the following tag: <preference name="applicationId" value="{PACKAGE_ID}"/> where {PACKAGE_ID} is replaced by the Android Package ID of your project.
  • Save the config.xml file.

Reference: https://www.ibm.com/support/knowledgecenter/SSHS8R_7.1.0/com.ibm.worklight.dev.doc/dev/t_android_cam_api_24.html#t_android_cam_api_24