If have a gradle (5.4.1) project (plugin 3.4.1) with two flavors (main and other) using the same sourceSets. I've come across an issue when renaming a resource id (in a layout XML). Here are the observations:
Android Studio 3.4.1 doesn't show any errors and works as expected, i.e. I can click through from kotlin code to the id in the layout xml.
Rebuilding the main flavor also works as expected (no compilation errors).
Rebuilding the other flavor gives a "unresolved reference" compilation error for the new ID. Gradle clean does not help. However, setting org.gradle.caching=false in the project-level gradle.properties does work (following Gradle sync, Clean, Rebuild). This explains why clean was not helping when caching was enabled.
On closer inspection of the R.java file, it seems to be using IDs from some rather old project state (presumably from the gradle cache stored in the user home directory). Now, I could just delete the gradle cache, but I'd rather find out why resource ID changes are not being processed properly. Whichever way Android Studio is running builds in the background, is working fine, just not when performing Build/Run etc.
All this investigation required a lot of trial and error: switching various gradle.properties settings one by one to see which one was leading to the problem, and I'm still no closer to finding the root cause of the problem.
What is the best approach to solving this type of problem, or is trial and error the only way?
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!
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
My game is able to build and run on debug mode so I was hoping it would be ready to be built in release mode but I'm currently stuck with this error in the title. Anyone knows the solution?
EDIT : I've tried commenting the imageAspectRatioAdjust in common_attrs.xml but it would cause alot more different errors.
EDIT : I've found out that there is two file that contains imageAspectRatioAdjust in the release folder of my game when I tried to build it, but there is only 1 file that contains imageAspectRationAdjust in debug. Even if I tried to delete one or the other, I have to clean and rebuild and it would reappear again in the same folders.
I've added Facebook Android SDK into a sub project which a main project is dependant on, which requires me to add Facebook Android SDK into the main project as well but I've accidentally updated some of the packages in the component on the main project and did not update the packages on the sub project, which seems to be the cause of this error.
I am having an Android Studio project, that works perfectly fine with Windows and is stored in Mercurial repository. Importing it on Windows can be done without any issues, yet on Linux everything crashes, as modules cannot be read correctly - AndroidManifest.xml is apparently missing.
I am aware of other threads and I have searched for suitable solution, but I am afraid that so far I haven't managed to fix the problem.
It is very important to me, that the project is set up correctly and whenever new person clones the project it will work without issues (and without necessity of recreating the project from scratch) regarding the operating system.
The current build.gradle file in failing module is already pointing to the correct location of manifest file:
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
Yet I am still having the following error:
Error:/AndroidManifest.xml (No such file or directory)
Im am on a Mac so I don't know if it's the same, there should be two tags to the left of your code, just beside the name of your class, something like :
<> public class missingFile {
if u click on those tags, suddenly Android Studio will give you an option to open your xml file.
I hope it helps, goodluck