Can I make R file path independent from packageName? - android

For exampel my application has com.example.app1 packageName. When importing R file it hase com.example.app1.R path. If I change packageName to com.example.app2 it will have com.example.app2.R path. So it must be changed in all files (Eclipse does it atomatically) but I don't want to do it. So can I have path to R file independent from packageName?

No, you can't do that.
R is generated at compile time by the Android development platform as a class (and a collection of inner classes) belonging to the package your.app.package.name. As of today (API 20), no user preference or setting allows changing the way it is generated.

Can I make R file path independent from packageName? NO
Eclipse will immediately generate the resource file R.java related to your package name, if it changes the imports must chage since R.java file have all the resources IDs provided to resources related to your package.

Related

Android studio -- how to include resource file in Java package using gradle

I have an android project which requires to include a non-java resource file to included in the final package with Java class.
If you are using Eclipse, it is very easy, just put the non-Java resource file in the same package of Java classes, Eclipse will automatically copy the resource file to the destination.
I tried the same method in Android Studio, which uses Gradle for building, but it doesn't pack the resource file in the final Java class package. I have no control over how to read the resource file, it must use class.getResourceAsStream() to read the resource file.
Is there any way to pack resource file in Java package in the final product? Any suggestion is much appreciated.
First, Create a java resources folder.
Second, Create the same structure with a package name of a class.
for example, picture blow, you will have the same folder structure if you have a Tobee.class in a com.tobee package.
You can read a file in a Tobee class by calling a getResource or getResourceAsStream method.
getClass().getResourceAsStream("jar_properties");
Alright, I have solved the issue: pack everything in a jar including resource files then import this jar into the Android project.

Package name does not correspond to blank file path

In Android Studio 2.3.3, I get the following error in the java source files:
Package name 'com.foo.bar' does not correspond to the file path ''
Note the blank filepath in the error, which I believe differences this from possible duplicate questions about package names vs file paths. This seems like something more than just the package statement is damaged.
Assuming I know the top level directory on my own hard drive where this is installed (i.e., I know where StudioProjects is and have verified that they are there) how do I go about repairing this... or at least figure out what the correct package statement is?
Update: Note that the manifest file does have a "package" tag, the contents of which I can copied directly into the package statements in the Java source files. They were identical what was there before so the error persists.
In this case, the problem was caused by having the java source files in the directory:
src/main/java
Rather than the directory:
src/main/java/com/foo/bar
This means that the package statements were pointing to an erroneous location (successfully detected) and the only reasonable place they could exist was expressed by an empty string (correctly reported but confusing.)
The simple solution was to create a new folder (technically a new package) called foo using the tools within Android Studio, relocating the source files there, and changing the package files to
src/main/java/foo/
package foo;
Android Studio assisted with refactoring, although it missed a few locations.

what is the work of R.java and BuildConfig.java both are auto generated files

I'm new in android development.
I ant to the use of R.java and BuildConfig.java both are auto generated files.
R.java maps your resource adresses to static int variables, which provides ease of use to you.
BuildConfig.java specifies special flags such as DEBUG which are interpreted by dalvik machine to allow debug for your app
R.java file auto-generated file. It stores the all Id's and drawable names and class names etc.., and build config file maintains the build generation process
The R.java is the way Android provide resource access from within your code. As you wrote or when you alters xml files etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code.

Making subfolders in a resource folder

Is it possible to make subfolders in the resource folders in the Android project? I have about 200 images (thumbnails) that I need in my project and I could add them in the drawable-mdpi, but it would be better to not mix these images with the other ones. Something like drawable-mdpi --> thumbs --> all images here.
No this is not allowed. You are only allowed to make folders specified by the android documentation.
The allowed sub folder names are specified in the link. Android generates the R.java based on these structures and putting sub folders can cause errors.
actually, there are mechanisms in place that allow the R.java file to be generated when there are folders with non-standard names in the res folder.
(i ran into this wanting to share a git repo as a submodule of both an iOS and Android project, but not wantint the Android project to pick up files that resided in a folder i designated.)
aapt is the tool that creates the R.java file, and it can be invoked with the --ignore-assets argument. there is a set of defaults for this found in the google source documentation, or a less verbose description simply by invoking aapt from the command-line without any arguments (or --help, which isn't a valid argument, but presents help nevertheless). using the line aapt.ignore.assets=xxx in an ant.properties file in your Android project will accomplish pretty much the same thing, depending upon your needs or preferences.
if you do not have a build.xml or other mechanism that forces usage of ant (which i do not), one of the aapt --ignore-assets defaults is <dir>_*, which means ignore any folders starting with _.
this was my fallback: i created a directory called _iOS_retina and placed all of my #2x files in there. (in Xcode, i can simply pull in resources from wherever they reside). the default invocation of aapt simply ignores it. to further streamline my project, i also updated my .project file to contain a resource filter that ignores this folder, and thus it doesn't take any space in my eclipse environment, either.
<filteredResources>
<filter>
<id>1371429105277</id>
<name></name>
<type>26</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-true-false-_iOS_retina</arguments>
</matcher>
</filter>
</filteredResources>

Understanding symbolic links to Android library projects in Eclipse/ADT

When referencing a library project with the current version of the ADT, an Eclipse linkedResource will be created in the .project file. I have a few questions about this.
Why an Eclipse link at all? Why not simply add the src/ folder of the library directly using a relative path? You have to set a relative path to it in default.properties anyway, for aapt to see its resource files.
This is more an Eclipse question, but I couldn't find an answer to this. The link that will be created has type 2. What does that mean? Which types are there?
The link does not use the location attribute, but locationURI, having this format: _android_<lib_name>_5deb8a74/src/main/java What is that, and where does it point? Does the 5deb8a74 part carry any special meaning, or is it just a random string generated to avoid name clashes?
UPDATE
I found the documentation on link definition syntax:
link - the definition of a single linked resource.
name - the project-relative path of the linked resource as it appears in the workspace.
type - the resource type. Value values are: "1" for a file, or "2" for a folder.
location - the local file system path of the target of the linked resource. Either an
absolute path, or a relative path whose first segment is the name of a workspace path variable.
locationURI - if the file is not in the local file system, this attribute contains the absolute URI of the resource in some backing file system.
Still not sure why the ADT would use a locationURI, and I still haven't found where these URIs are defined (i.e. where they point to).

Categories

Resources