Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildHelper provokes ClassNotFoundException when applicationid is not same as manifest package name #513

Closed
cdmahoney opened this issue Oct 9, 2018 · 8 comments
Labels

Comments

@cdmahoney
Copy link

Error occurs when trying to take picture with camera plugin. Abbreviated stack trace:

10-09 13:04:53.164: W/System.err(17950): java.lang.ClassNotFoundException: <application id>.BuildConfig
10-09 13:04:53.164: W/System.err(17950): 	at java.lang.Class.classForName(Native Method)
10-09 13:04:53.164: W/System.err(17950): 	at java.lang.Class.forName(Class.java:453)
10-09 13:04:53.164: W/System.err(17950): 	at java.lang.Class.forName(Class.java:378)
10-09 13:04:53.164: W/System.err(17950): 	at org.apache.cordova.BuildHelper.getBuildConfigValue(BuildHelper.java:54)
10-09 13:04:53.164: W/System.err(17950): 	at org.apache.cordova.camera.CameraLauncher.execute(CameraLauncher.java:140)

Error can be reproduced by defining applicationIdSuffix in build-extras.gradle, for example with

		debug {
			applicationIdSuffix ".debug"
			versionNameSuffix ".debug"
		}

The problem is caused by BuildHelper calling 'context.getPackageName', which returns the application id, not the package name.

Ironically, the application id is what CameraLauncher is trying to get in the first place, so I've been able to fix the problem modifying code in CameraLauncher:

        Context context = cordova.getContext();
        this.applicationId = context.getPackageName();
        // this.applicationId = (String) BuildHelper.getBuildConfigValue(cordova.getActivity(), "APPLICATION_ID");

But I think this should really work out of the box... (maybe add a method to CordovaInterface to return BuildConfig, which could be informed optionally from the application's CordovaActivity implementation?)

@Benoit1980
Copy link

having problem with the camera too in 7.1.1, it wont work anymore.

@cdmahoney
Copy link
Author

@freddycrugger, if you're getting the java.lang.ClassNotFoundException error try manually editing the execute method in CameraLauncher.java (in folder platform\android\app\src\main\java\org\apache\cordova\camera) to use the value returned by context.getPackageName() directly, as in the example I gave above.

If that fixes the problem you might want to set up a hook to copy the patched version of CameraLauncher.java to platform\android\app\src\main\java\org\apache\cordova\camera to prevent losing the changes if the plugin is reinstalled (be careful too if you update the plugin - you may need to update the patched file too.)

@Benoit1980
Copy link

I reverted to 7.0.0 as I was getting too many issues(not just with the camera), thanks anyway.

@janpio
Copy link
Member

janpio commented Oct 30, 2018

@cdmahoney You seem to have a good grasp of the problem, are you up to submitting a PR fixing this maybe? Thanks.

@cdmahoney
Copy link
Author

@janpio OK, I'll submit a PR to fix the problem in the camera plugin. Will be doing it from home though, so may take a few days to get it done.

@cdmahoney
Copy link
Author

@janpio - I just created pull request:
apache/cordova-plugin-camera#364

Hope it's all in order - let me know if any changes required!

@kristianheljas
Copy link
Contributor

kristianheljas commented Jun 22, 2019

@janpio - New PR: #751

Issue is that context.getPackageName() returns android package name which does not have to match java package name.

Since getBuildConfigValue() tries to load java class (BuildConfig), it should use java package name instead using context.getClass().getPackage().getName()

@timbru31
Copy link
Member

timbru31 commented Apr 7, 2020

Closing as PR #751 has been merged.

@timbru31 timbru31 closed this as completed Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants