Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android flavor and resource files

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?

like image 883
Bandzio Avatar asked Feb 23 '15 11:02

Bandzio


People also ask

What are resources file in Android?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.

What is Android Flavour?

Android Product Flavors are used to create different app versions. App versions can be free or paid. They can have different themes and texts. They can use different environments or APIs. Let's assign two product flavors free and paid in our application.


2 Answers

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.

like image 196
Nick Palmer Avatar answered Oct 13 '22 17:10

Nick Palmer


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.

like image 27
Sam Avatar answered Oct 13 '22 17:10

Sam