My understanding is that suppressing individual warnings can be done in XML by adding an ignore property, e.g.
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="SpUsage"
android:textSize="30dp"
This works fine when the text size is directly specified. But when I use a reference to a dimension, e.g.
android:textSize="#dimen/largeTextSize"
Lint produces a warning in spite of the ignore.
Let's assume that the given case is a justified exception, and that the guidance on generally preferring sp dimensions over dp for text is well understood and followed.
Because this is an exception I do not want to disable the warning globally. How can I do it locally?
You need to add the suppression on the dimen value resource:
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="largeTextSize" tools:ignore="SpUsage">123dp</dimen>
</resources>
Only that approach helped me:
Specify lint checking preferences in the lint.xml file. Place it in the root directory of your Android project.
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="SpUsage" severity="ignore" />
</lint>
More about lint config look at: https://developer.android.com/studio/write/lint#config
Related
I'm trying to use the Runtime Resource Overlay (RRO) mechanism to overlay an xml resource, which is using custom attributes and custom namespace. When building the overlay APK the aapt2 (link) throws an attribute not found error.
How do I make known the custom attribute from the main application to the overlay?
Is it even possible to use custom attributes in an overlay?
Details:
The overlay contains of two files:
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.test.simpleappoverlay">
<overlay
android:targetPackage="de.test.simpleapp"
android:targetName="Test"/>
</manifest>
and the xml file res/xml/my_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<MyConfig xmlns:app="http://schemas.android.com/apk/res/de.test.simpleapp"
app:text="hello">
</MyConfig>
<!-- I also tried: xmlns:app="http://schemas.android.com/apk/res-auto" -->
The main application defines the attribute text in res/values/attrs.xml:
...
<declare-styleable name="MyConfig">
<attr name="text" format="string" />
</declare-styleable>
Furthermore it defines the overlayable tag in res/values/overlayable.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<overlayable name="Test">
<policy type="public">
<item type="xml" name="my_config"/>
</policy>
</overlayable>
</resources>
To build the overlay I do this:
aapt2 compile -v --dir app/src/main/res/ -o SimpleAppOverlay.flata
and
aapt2 link -v --no-resource-removal
-I ~/Library/Android/sdk/platforms/android-29/android.jar
--manifest app/src/main/AndroidManifest.xml
-o sao.apk SimpleAppOverlay.flata
Which leads to the following output:
note: including /Users/bernd/Library/Android/sdk/platforms/android-29/android.jar
aapt2 W 09-01 14:33:06 20083 694697 ApkAssets.cpp:138] resources.arsc in APK '/Users/bernd/Library/Android/sdk/platforms/android-29/android.jar' is
compressed
note: linking package 'de.test.simpleappoverlay' using package ID 7f note: merging archive SimpleAppOverlay.flata
note: merging 'xml/my_config' from compiled file app/src/main/res/xml/my_config.xml
note: enabling pre-O feature split ID rewriting AndroidManifest.xml:
note: writing to archive (keep_raw_values=false)
note: writing AndroidManifest.xml to archive
note: linking app/src/main/res/xml/my_config.xml (de.test.simpleappoverlay:xml/my_config)
app/src/main/res/xml/my_config.xml:2: error: attribute text (aka
de.test.simpleappoverlay:text) not found
error: failed linking file resources.
I had a similar problem to this where I was trying to overlay an app with custom attributes in Android 10 and have a solution. There are two changes that are needed:
Part 1
It looks like your app name is de.test.simpleapp so your my_config.xml file should look like:
<?xml version="1.0" encoding="utf-8"?>
<MyConfig xmlns:app="http://schemas.android.com/apk/prv/res/de.test.simpleapp"
app:text="hello">
</MyConfig>
The important piece is specifying the 'prv/res/app.packagename' so it uses the namespace of the base app for the private attributes. If you used 'apk/res-auto' that would not work as it resolves to the name of your app (which in this case is the overlay app de.test.simpleappoverlay) which does not contain the definition of the private attributes.
Part 2
Since you now have a dependency on the main app when linking, you have to include it in the link command with a -I simpleapp.apk (or whatever the APK name is of the base app). Right now you are just including android.jar in the aapt2 link step which only contains the 'android' namespace and attributes. Therefore, you now need to add your base app in the include step so it can link properly against its namespace and attributes.
Like some of the other answers said though, this problem goes away in Android 11, but if you're stuck on Android 10 like I am hopefully this helps.
I don't think including new ids is supported in the current implementation of the RROs (at least in Android Q).
Regarding your error, aapt uses the android.jar to generate the apk for your overlay. Since it cannot find your new attribute, it throws an error. For this to work I believe you would need to use a modified android.jar including your attribute. One way of doing this is by modifying the Android SDK in the AOSP and creating your own version that you would then use for the aapt command.
Some time ago I switched my AOSP environment from Android 10 to 11. Google made quite a few changes to the Overlay mechanism. To my big surprise these changes fixed the problems I had when trying to "overlay" custom attributes.
With the Android 10 environment I observed while debugging that the Android XML parser returns "null" when trying to read the mentioned attributes. With idmap I was able to confirm that the attributes were present in the overlay and the target app, and that they were properly mapped.
Also all the linker errors were gone.
I want to have androidTest resources specific to each app flavor. I found an answer on this site that indicated you can just make a resource directory androidTestFlavorName and it will be managed like all the other resources. So I have a directory app/src/androidTestFlavorName/res/values and a file strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="test_sec_code">aStringValue</string>
</resources>
I have a test class app/src/androidTest/java/com/company/package/StartupTest.kt. In that class:
val code = context.getString(R.string.test_sec_code)
test_sec_code is red, so the IDE doesn't like it, and the compiler reports: Unresolved reference: test_sec_code
My selected build variant is flavorNameDebug. So am I setting up these resources wrong, or is my goal not possible?
Edit:
I tried putting the resource in app/src/androidTest/res/values/strings.xml and it can't be found there either. Surely there must be a way to define test resources right? Hello, is this thing on?
So I updated my Android Studio to v3.2. When I tried compiling the project, build fails.
Below is the error:
Android resource compilation failed
Output: C:\Users\Ashish\AndroidStudioProjects\StartUp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:900: error: <item> inner element must either be a resource reference or empty.
Command: C:\Users\Ashish\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.0-4818971-windows.jar\7f1fbe9171e916e5044000cd76b749c8\aapt2-3.2.0-4818971-windows\aapt2.exe compile --legacy \
-o \
C:\Users\Ashish\AndroidStudioProjects\StartUp\app\build\intermediates\res\merged\debug \
C:\Users\Ashish\AndroidStudioProjects\StartUp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml
Daemon: AAPT2 aapt2-3.2.0-4818971-windows Daemon #0
Please do tell if there is any more information required. Any help will be appreciated.
I was facing this issue today after updating gradle from 3.1.4 to 3.2.0. I don't know why, but the build started to throw that exception. i deleted the build folder, and deleted the gradle caches folder but nothing worked, so i looked at the merged values.xml and turns out that my ids.xml was defining a wrong id that was being merged to the values.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="downloading_package" type="id">Baixando pacote de sincronizaĆ§Ć£o</item>
</resources>
And apparently this was working before the update... for my case i deleted the ids.xml file (it was useless in the project)
I wish i could know why before the update everything was working
the <item> in values.xml at line 900 ...might be of resource-type id.
the correct syntax would be (just as the error message tells):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="id_name" />
</resources>
see the documentation.
For me I had the layout width and layout height in the layout view and the LinearLayout view for databinding. This threw a duplicate error.
I fixed this by removing the layout_width and layout_height attributes in <layout>.
I'm just finishing this issue a few minutes ago, try to modify or delete id.xml, if you think you don't have it, try to find in another module in your app. I change my value resource from <item name="header_view" type="id">header_view</item> to <item name="header_view" type="id"/> and it worked for me.
just change your attr name which is present in resource -> values -> attr
<attr name="fontFamilys">
<enum name="roboto" value="0" />
<enum name="roboto_condensed" value="1" />
<enum name="roboto_slab" value="2" />
</attr>
for example if you are getting error related to fontFamily just change the the name build the application error will not come
i also meet the problem,you just need to find where the values in,and update it from <item type="id" name="id_name" >id_name</item> to <item type="id" name="id_name" />,now it's ok.
I got that problem when I'd quotation within <string> tag
<string name="vj_dialogue">I'm waiting</string>
when I escaped it with backslash '' . The problem was gone
<string name="vj_dialogue">I\'m waiting</string>
This could also happen if you mistakenly happened to not escape some value (apostrophe with backslash or ampersand with & or something else I am not aware of) under res/values/strings.xml file
to bring it in context here is an example ->
incorrect :
<string name="update_now">Let's Update now</string>
correct :
<string name="update_now">Let\'s Update now</string>
in your build gradle(Project:name) do it like that
classpath 'com.android.tools.build:gradle:3.+'
then rebuild your project .. after that it will show message to update your gradle from 4.6 to 4.10
In my case
<?xml version="1.0" encoding="utf-8"?>
is repeated twice in the XML. Make sure it should be one per a file at the top of the XML.
This is what worked for me:
In my build.gradle file > dependencies, I was implementing a newer version of appcompat library
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-v4:21.0.3'
implementation 'com.android.support:appcompat-v7:21.0.3'
implementation 'androidx.appcompat:appcompat:1.0.2'}
After commenting //implementation 'androidx.appcompat:appcompat:1.0.2' It worked fine. I would suggest, even if it's a different library, Please check the version you are using.
In your xml file, you may forget to add /> to brace your attribute.
Is it possible to translate some strings, but not all, in a separate resource file without Lint complaining about MissingTranslation?
For example: my app's strings are all in res/values/strings.xml. One of the strings is
<string name="postal_code">Postal Code</string>
Since "postal code" is usually called "zip code" in the US, I want to add another resource res/values-en-rUS/strings.xml with contents:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="postal_code">Zip Code</string>
</resources>
However, Lint complains about other strings in values/strings.xml but not in values-en-rUS/strings.xml
I realize you can suppress the warnings by specifying tools:ignore in values/strings.xml. But this is undesirable because the Lint warning is actually useful when translating to another language.
Instead, is it possible to suppress the warning in the values-en-rUS/strings.xml file, as in, telling Lint not to use that file as criteria when looking for missing translations?
A nice way to disable MissingTranslations check is to add the option in module specific build.gradle file .
android {
lintOptions{
disable 'MissingTranslation'
}
//other build tags
}
If the strings are not present in locale specific Strings file, it will take the strings from the default file which generally is strings.xml.
I found a better solution according to this answer: https://stackoverflow.com/a/13797364/190309
Just add ignore="MissingTranslation" to your string.xml, for example:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >
<!-- your strings here; no need now for the translatable attribute -->
</resources>
Lint supports partial regional translations, but needs to know what language the default strings are. That way, it can distinguish a partial regional translation from missing translations in a different locale.
To specify the locale in values/strings.xml:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
Quote from the lint command-line tool, when running lint --show:
By default this detector allows regions of a language to just provide a
subset of the strings and fall back to the standard language strings. [...]
You can tell lint (and other tools) which language is the default language
in your res/values/ folder by specifying tools:locale="languageCode" for
the root <resources> element in your resource file. (The tools prefix
refers to the namespace declaration http://schemas.android.com/tools.)
Source: https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/TranslationDetector.java#88
Add the locale specification to your default language file, and you shouldn't get that error for en-rUS, but will still be informed of any other missing translations.
This seems to not answered yet, so I show you one solution:
In your DEFAULT xml file, you can define strings, that don't need translations like following:
<string name="developer" translatable="false">Developer Name</string>
This string does not need to be translated and lint will not complain about it either...
This is a Swiss knife answer, so you can ignore the missing translations message depending on which situation you are:
Ignore all MissingTranslation message:
Edit your build.gradle file:
android {
lintOptions{
disable 'MissingTranslation'
}
}
Ignore all MissingTranslation messages in a concrete resources.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<!-- your strings here; no need now for the translatable attribute -->
</resources>
Ignore MissingTranslation message for only one string:
<string name="developer" translatable="false">Developer Name</string>
If you are using Eclipse, please look at the toolbar buttons of the Lint warnings view. One of them is called "Ignore in file". This should help, but only if Lint assigns the error to your "US" file. That button simply modifies the lint.xml file in your project, so you can investigate (and undo) that easily.
More details about that file specific suppression are at http://tools.android.com/tips/lint/suppressing-lint-warnings
For the lint configuration file lint.xml:
<issue id="MissingTranslation" severity="ignore" />
If case of using gradle 7 :
lint {
disable("MissingTranslation")
}
I know I can ignore a rule in Lint with attribute tools:ignore
My difficulty is that I want to ignore several rules.
In my case, for Google analytics ga_trackingId, I want to ignore TypographyDashes and MissingTranslation
I tried with no success
<resources tools:ignore="TypographyDashes|MissingTranslation" xmlns:tools="https://schemas.android.com/tools" >
and
<resources tools:ignore="TypographyDashes,MissingTranslation" xmlns:tools="https://schemas.android.com/tools" >
and
<resources tools:ignore="TypographyDashes MissingTranslation" xmlns:tools="https://schemas.android.com/tools" >
I am now out of ideas. How can I specify several values in tools:ignore?
You need to use a comma separated list, but there must not be blanks.
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" // required to work
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="contentDescription,SpUsage" > // comma separated list. NO blanks allowed!
For a list of the valid options you can get a list from the command line or use the eclipse lint error checking list mentioned by throrin19:
lint --list > lint_options.txt
See lint documentation.
The problem here was usage of wrong namespace uri in xml resource file;
xmlns:tools="https://schemas.android.com/tools"
Which should have been http://... protocol instead. This is discussed in more details in issue 43070
Used you eclipse or intelliJ ?
In Eclipse, go to Window -> Preferences -> Android -> Lint Error Checking
And have a fun ;-)
You can put multiple annotations on specific strings to ignore multiple lint checks:
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--suppress MissingTranslation -->
<!--suppress TypographyDashes -->
<string name="some_string">ignore my translation</string>
...
</resources>
http://tools.android.com/tips/lint/suppressing-lint-warnings