Android flavor and resource files
Asked Answered
D

2

11

I have problem with android gradle build. Long story short, I have created 2 flavors:

productFlavors{
        abcDemoFree{
        }

        abcDemo{
        }
}

Eeach flavor has his own Activity(Settings.java) and layout(settings_layout.xml) for this activity. From main both files were removed, so Settings.java and settings_layout.xml exist only in flavors.

abcDemoFree
    - java
        - Settings.java
    - res
        - settings_layout.xml

abcDemo
    - java
        - Settings.java
    - res
        - settings_layout.xml

Android Studio created 4 build variants:

abcDemoFreeDebug
abcDemoFreeRelease
abcDemoDebug
abcDemoRelease

Everything works nice, I am able to create apk with behavior I am expecting from each flavor. Problem is when I switch to one buildVariant, for example to abcDemoDebug, android studio reports bugs from resources file in another flavor, in my example from abcDemoFreeDegub/res/settings_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"

</LinearLayout>

These lines

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

generate bugs : URI is not registered.

How to get rid of these errors? Or maybe this is normal behavior?

Diurnal answered 23/2, 2015 at 11:27 Comment(0)
M
2

Unfortunately AndroidStudio uses the resources from the wrong flavor when you switch. You have to do a clean and then build after switching the variant to get these errors to go away.

Musette answered 25/8, 2015 at 18:0 Comment(0)
T
2

The errors come if you are not actually switching the build variant. If you just look at the file it will show the errors, but if you actually switch the build config and rebuild you will be fine. Also confirm that you did not do specific Implementation or Compile for flavors like:

myFlavorImplementation android.libraries.forexample

as then it would be missing the necessary libraries to cleanup the warning.

enter image description here

Use the build variant slide out to change to the other debug builds and it should auto rebuild, but if not, just do a rebuild yourself. Also make sure you are looking at the actual files and not the generated files as sometimes if you are using annotation tools or data binding with layout wrappers, and you had the android tag in the layout and first child tag it can cause those to be red as well on generation of final layout file to be packaged.

Traceable answered 15/2, 2018 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.