Need to clean project after changing layouts in Android Studio - android

When I change small thing in XML layout files like maxLength of a TextInputEditText I need to clean project to let AS pickup change.
Structure of res folders is:
res
- layout
- drawables
- ...
No more subfolders. AS 3.1.1, Gradle 4.6. I tried to add this in gradle:
sourceSets {
main.res.srcDirs = [
'src/main/res/anim',
'src/main/res/color',
'src/main/res/drawable',
'src/main/res/drawable-hdpi',
'src/main/res/drawable-mdpi',
'src/main/res/drawable-xhdpi',
'src/main/res/drawable-xxhdpi',
'src/main/res/drawable-xxxhdpi',
'src/main/res/menu',
'src/main/res/mipmap-dpi',
'src/main/res/mipmap-hdpi',
'src/main/res/mipmap-mdpi',
'src/main/res/mipmap-xhdpi',
'src/main/res/mipmap-xxhdpi',
'src/main/res/mipmap-xxxhdpi',
'src/main/res/raw',
'src/main/res/values',
'src/main/res/values-land',
'src/main/res/values-large',
'src/main/res/values-v21',
'src/main/res/valuesw820dp',
'src/main/res/xml',
'src/main/res/layout',
'src/main/res']
}
Is there a way to prevent cleaning the project every time for change to take effect?
So whenever I make a change to the XML files and run the app, I should see my change but I have to clean the project every time and its getting annoying.

First of all you need to create your additional resource folder beside res folder instead of creating inside it. Then try make XML like this example:
sourceSets{
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java', '.apt_generated']
aidl.srcDirs = ['src/main/aidl', '.apt_generated']
res.srcDirs = [
'src/main/res',
'src/main/test',
'src/main/login',
'src/main/main',
'src/main/includes'
]
}
}
Note: Your additional resource folder will change effect without cleaning the project.

Problem was with build flavors. One of them was named main and that causes problems with projects scanning. Changing name fixed issue. It is marked in newer versions of gradle plugin.

I dont think you need to clean the project everytime. For some minor changes you dont even need to run the app again, simply clicking on instant run will do, as it will hotswap the changes directly into the current activity.
Also is there any particular reason you are adding the paths to gradle?
AFAIK, you need to do this only when you change the paths and set it to something else. In your case the paths are at their default locations so I dont see the reasoning behind this. Try removing this from the gradle and switching on instant run.
These links should help:
https://developer.android.com/studio/run/#instant-run
https://developer.android.com/studio/write/add-resources

There is no need to clean every time after changing in xml layout file , you just need to refresh your layout by clicking on blue icon at right end side of your preview section as shown in below image . in that there is property named Force Refresh Layout. click on that and your layout will be refreshed.

You can try enabling the INTANT RUN feature in Android Studio. It worked for me.
If you are using MAC go to Preferences, Search for Instant Run and check the Enable option on right side window.
After enabling, instead of running you can Go to RUN and click on Apply Changes.

Upgrade Android Studio to 3.1.3 solve my problem
Download Latest Version 3.1.3
or what you can do is click this info button and then Refresh the layout will solve the problem

Related

How to work with build.gradle file in Android Studio

After I make a change in the build.gradle file, what is my next step? I think there must be more steps I am missing…. compiling?? building ?? I am making these changes in Android Studio.
Not being that familiar with the build.gradle file, I am not sure what to do next. Any help would be most appreciated.
I want the change I made to the build.gradle file to have an effect on being able to install my project onto my Oculus Device. Right now I get the "Requires newer sdk version #29 (current version is #25).
after making a change in build.gradle, you have 2 different options, but they're the same thing :
you can click on the gradle sync button, then gradle will apply the recent changes you made or, as you can see in the blue text underneath, you can also make use of the Sync Now feature which will be displayed after any changes have been made.
Here's an example of the complete warning which is displayed after a change
after this, you can build/deploy your project or make changes in code as you need to, your dependencies have now been updated and will be available, assuming gradle syncs successfully

How to add resources to androidTest for android library in AndroidStudio

I made an androidLibrary project which contains some Volley request classes,
and want to write a unit test for some of these requests.
I have done that like a hundred times, and it works somehow after several tries, but never on the first try.
If I try this via androidStudio
RightClick Project->New->Android Resource Directory
you can choose between sourceSets "main", "debug" and "release". There is no androidTest option.
If I try it manualy, adding the following folders [moduleName]/src/androidTest/res/raw
and put a file inside (lets say image.jpg), I do not get the reference via R.raw.image. In fact I haven't access to R.raw in my Test at all.
I also tried a Build->rebuild project or Build->clean without success.
In other projects I have a my.app.namespace.R along a my.app.namespace.test.R which holds all test resources.
So my question is:
What do I need to do in order to add a res folder to my androidTest, which is accessible via R.[folder].[resId]?
Am I missing a crucial step after adding the files (i.e. telling gradle explicitly about it)?
Because adding a tag like this in my build.gradle also doesn't seem to make any difference:
android {
sourceSets {
androidTest {
java.srcDirs = ['src/androidTest/java']
res.srcDirs = ['src/androidTest/res']
}
}
}
If it matters I am using:
Android Studio 3.1.2
gradle-wrapper 4.4
minSdkVersion 19
targetSdkVersion 27
I figured out what is going on with the help of the update of Michael Krussel Answer:
Android Studio is not compiling the my.app.namespace.test package automatically. All you need to do is run the Test the first time, ignoring the errors AndroidStudio might present.
After the first run you got access to the whole test namespace (including the *.test.R class)
No additional gradle config (like sourceSets from above) are necessary!

Android layout subfolder and Instant run not working together

I configured my gradle project to read layout files from subfolders, but this makes Instant Run stop working.
The only way to see the changes is manually uninstalling the app and reinstalling it.
EDIT: is this bug. Is there any workaround for this?
This looks like an issue with layouts in nested folders. When I was making changes to layouts in nested directories, I had to make clean before run - otherwise app ui remained unchanged. When I put my nested layout dirs before the general dir in build.gradle, the gradle started to see the change in layouts and pick them up without 'clean'.
sourceSets {
main {
res.srcDirs = [
'src/main/res/layout/nested',
'src/main/res'
]
}
...

Android studio suddenly cannot resolve symbol R

I was having a project open in Android studio. It was generated by the wizard and working fine.
I did some small changes to activity_main.xml and when I changed back to MainActivity.java i get the error in several places that it cannot resolve R. I might have done something to cause this. But I´m not sure what since it appered when I edited the xml.
Does anyone know what might be the solution to this?
I can find the R.java in r/release/packege/com/ and it looks fine.
Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file. Please check your xml files and try to rebuild the project. Sometimes cleaning the project and rebuilding it also works.
In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java.
Downgrade Your Gradle Plugin Version
No amount of cleaning, rebuilding and restarting would do the trick for me.
The only thing that did the trick was downgrade our Gradle version from 3.4.0-alpha02 to 3.2.1.
So, instead of:
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
}
We used:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
After making that change and then doing a Gradle sync, everything worked.
Not sure if it's related to the alpha release or to that version or if changing your Gradle version at all just forces a refresh that otherwise cannot be done with the other commands, but there ya go.
Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!
Try to clean the project by doing the following:
If you are using Windows, open CMD, cd to your project directory and run gradlew clean.
I just had this problem and the reason was that after editing the layout android studio automatically added an import to the source files:
import android.R
Of course the main R class has not all the symbols in your layouts, which made everything a red mess.
Removing that line solved for me.
EDIT:
Seems like eclipse had the same problem early on.
I sometimes get this after updating Android Studio. Not always straight away but after a while (perhaps after a restart of the program).
Here is what has worked for me: Tools - Android - Sync project with Gradle files.
For some reason just clicking the "Sync now" link that shows up after changing the build.gradle file has not done any change for me. Even though the messages says approximately the same thing.
I had the same problem because in my strings.xml I forgot to escape a apostrophe. Use " \' " never " ' " in xml file!
I had that same problem crop up, too (most recently while I was at lunch--came back and WTF?). And since this was a simple Hello, World program, there wasn't many places these errors could hide. The XML files looked fine--I hadn't even touched the manifest. A clean did nothing.
So I commented out a section of my layout.xml to try to narrow down the problem. Sure enough, the errors went away.
And then I simply UNcommented out exactly what I had commented...and everything was hunky-dory. Strange! Perhaps there was some lingering issues that needed to be touched in order to cause a rebuild of the layout file. You'd think basic stuff like that wouldn't happen these days, but they do.
One more thing! It's possible that something may happen to Java as well. It's very hard to know the status of the Java run-time environment; just easier to start again. So here's one more thing to try: restart your computer, restart Android Studio, AND THEN Invalidate Caches and Restart. Yep, the whole-hog.
When all else fails restarting android sometimes helps. During the start files are loaded afresh and this enables detection of the R
This problem occurs when you rename the package name.
After renaming the package name, you must change the older package
name to a newer one.
For me it solved the problem once I changed that.
After that, you might get errors in xml files, that you have to change
the package name there also.
That's it.
I just had the R problem and after a few hours found that the problem was my regional spelling as used in one of my xml files. The problem word was behaviour, which is spelled behavior by Americans.
I am Australian and we are taught ye olde English way of spelling certain words and so I looked past it a number of times before the penny dropped.
Its a small thing, but it can easily sneak up and bite you on the nether regions and is hard to spot and can waste a lot of time.
In my case I had just added some new image files to the drawable folder and accidentally put a GIF in there with my PNGs. I had not incorporated them into the project at all yet. I removed the GIF and then did a clean and rebuild. That fixed it for me.
I had this exact problem when importing a Udacity Example Program. The Message Pane in Android Studio said the error was failing to load a .PNG in a sub-directory deep in the Project Files.
Turns out the PNG with all of the sub-directory path names was greater than the name length limit defined by Windows 7 (260 characters). I gave the Project a shorter name - to free up some space - and then Android Studio happily compiled.
After you tried make project, clean project, rebuild project but still doesn't work.
Try restart Android studio

android eclipse problem with R.java

Suddenly my Android app is compiling with loads of errors in Eclipse. When I use Project-Clean the console shows it processing until 'Writing symbols for class R' then it pauses for around a minute and finishes with 'Nothing to pre compile' This behaviour follows adding several new strings to my strings.xml and replacing strings in the code with :-
getResources().getString(R.string.name of string);
Any ideas? Would it be safe to delete the R.java file to force it to rebuild perhaps?
I faced a similar issue. I had missed putting a plus sign in android:id="#+id/GoTo". Everything was fine after I put back the missing plus sign, cleaned and rebuilt the project.
When you use eclipse is very important after you change something in strings.xml to save that file and also rebuild !, if you have checked the option for build automatically than is enough just to CTRL+S if not you have to rebuild manually , cause your R.strings values are not jet updated
Check if you import the right R file. It should be your projects file, not android.R.
Maybe your listing is not exact code but if it is, then resources names can't contain spaces. (I am talking about R.string.name of string)
This is my currently todo list when I have that kind of problem:
Refresh main project
Clean
Build All
if it wont work again. Close Eclipse -> repeat from 1.

Categories

Resources