3

I am new to espresso testing in android. When I try to build test apk I am getting this error:

Package 'com.class.xxx.test' from AndroidManifest.xml is not a valid Java package name as 'class' is a Java keyword.

Is there any possible way to change the package name in the test build config or any other solution?

Thanks!

AzAz
  • 43
  • 1
  • 3

2 Answers2

5

I was getting this because I had a package name with a - in it (i.e. test-support). In the module's AndroidManifest.xml I had

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thomas.myapp.test-support" />

The Java Language Spec says

If the domain name contains a hyphen, or any other special character not allowed in an identifier (§3.8), convert it into an underscore.

I didn't convert to an underscore, but just removing the hyphen works. I changed the AndroidManifest.xml to

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thomas.myapp.testsupport" />

and it works fine now. Khemraj's answer put me on the right track to figuring this out.

Thomas
  • 5,810
  • 7
  • 40
  • 48
3

As simple as it say.

not a valid Java package name, 'class' is a Java keyword.

There are some reserve keywords in Java. Which you can not use. You are using class, which is reserved word.

You can change class package name as solution.

List of Java reserved keywords.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • I need to change the package name only on build for test apk do you maybe know how to do that? – AzAz Jul 23 '18 at 10:31
  • Just use name refracting, select package to rename (class), then press shift + F6 to rename refract. It will handle all imports automatically. – Khemraj Sharma Jul 23 '18 at 10:33
  • Yes but it is auto generated and when I change it it gets back to old one on build... – AzAz Jul 23 '18 at 10:37
  • You can not change only test package name. I think, Change app package name, you need not change `build.gradle` package name, just use refracting in your `manifest.xml` package name. – Khemraj Sharma Jul 23 '18 at 10:38