Cannot resolve symbol R when move class out of default package - android

When my "ExampleAdapter" class is in the sample package as my MainActivity, I don't get an error. Let's say my MainActivity is in "com.example". When I create a new package inside of the "com.example" package (let's say "com.example.adapters"), and move my "ExampleAdapter" to the new package, I get the dreaded error, "Cannot resolve symbol 'R'". Why does it matter which package my adapter is in?

Why does it matter which package my adapter is in?
The R and BuildConfig classes are always code-generated into the Java package that you name in the package attribute of your <manifest> element in your AndroidManifest.xml file. Any Java classes outside of that package that need to refer to R or BuildConfig need to add the corresponding import statement, as with any other Java class from a foreign package.

Related

import R package without product package name

project: qt5.10/android/qml
I need to use the icon from the resources - R. drawable.icon
Code below is successfully built. But it uses the name of the application package.
import myapp.foobar.com.R;
...
.setSmallIcon(R.drawable.icon)
I would like to import an R package without being bound to the application package name. Is that possible?
Or maybe there is a way to access resources without importing the R package?
Or import R-package by specifying the name of the main package indirectly? something like this: import auto.R
No, its not possible...R will always consist of Package name with starting

Manifest package and applicationID can differ from real java packages of the application?

Let's analyze this situation:
Manifest package and application ID on gradle file: com.myweb.mysuperapppackagenamewithaso
Real java packages inside the sourcecode of my app:
com.mysourcecode.package1
com.mysourcecode.package2
with the MainActivity here:
com.mysourcecode.package1.MainActivity.java
Is this possible and safe? Manifest package and applicationID can differ from real java packages of the application?
Below given are the three things which we need to think about,
applicationId: BuildConfig.APPLICATION_ID
packageName: getApplicationContext().getPackageName()
Java package: BuildConfig.class.getPackage().toString()
getPackageName gives the same applicationId which is created at the final moment from the gradle file and it overrides the AndroidManifest package. The final AndroidManifest contains the same applicationId.The getPackageName is the same as the applicationId as the applicationId overrides the packageName in the AndroidManifest at the final moment.
But for the Java code, the package is same as the project structure. The package that is used in your source code to refer to your R class, and to resolve any relative activity, service registrations, continues to be called the package as defined in your manifest. So, the AndroidManifest should have the package same as Java package to resolve relative activity, service.
So, with the same java package, we can create any number of APKs with all unique applicationId.
Example : application flavors
Reference : Android Package Name Vs Application ID

java.lang.NoSuchMethodError in android

I have include a jar file in my android project. Inside the jar file there is a class OverlayView in a package name android.widget. I can import the class into my Test.java file by import android.widget.OverlayView; and call the method init(). After these I can compile successfully. But while running the app it throws an exception
java.lang.NoSuchMethodError: android.widget.OverlayView.init
I didnt get class definition error
Your library contains android.widget? There is already a package with android.widget as its name. My guess is that it's conflicting with your library. Try deleting the import statements, by this time you should get an error because you deleted the import. Place your cursor in the method that has error then wait for the import suggestion to pop out, then alt+enter to import package. See if it is conflicting with the default android.widget package.

Adding a new class in android project must import R.java file

I just added a new class to my Android project, and I have an error that says that I have to import R... I don't understand because R is not supposed to be imported... I don't know why it is not recognized in this class and it is recognized in the others ?
I had several packages of an existing project and I created a new package and created a new class in that extends Activity. I also added the activity in the Manifest. The class contains only the basic onCreate method and the problem is on the line setContentView(R.layout.activity_manage_card) that gives me the error

Does the app name belong in the "import R" line?

I had to explicitly add my "import R" to my main activity's source.
However, when I run the app nothing displays in the Emulator to let me know my app is running. I'm wondering if the "import" I added is wrong.
Assuming my package is named com.goSitOnAPotatoPanOtis.This.
should it be this:
import com.goSitOnAPotatoPanOtis.R;
or this:
import com.goSitOnAPotatoPanOtis.This.R;
?
From my experience you should not have to add an import *.R class, and it will actually cause problems when building. That class should be available to your activities already.
The R.java class is generated in the application package as defined by the manifest package attribute:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goSitOnAPotatoPanOtis" android:versionCode="1"
android:versionName="1.00">
If your activity is in that package you do not need to import R.java because of Java package visibility. If your activity is in another package you need to import it with
import com.goSitOnAPotatoPanOtis.R;
Even if you import the R class when it is not necessary it should not cause your application to stop running.
It should be import com.goSitOnAPotatoPanOtis.R; you can then reference that R using just the R.id.some_id or whatever you are trying to reference from the generated R class.

Categories

Resources