37

Where are the schemas (DTD or XML schema) for the XML files used on Android like AndroidManifest.xml or the layouts?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

5 Answers5

15

The schemas don't exist as an xml file. Schemas are dependent upon what UI classes your program uses. There's a slightly better discussion here.

Will
  • 19,789
  • 10
  • 43
  • 45
  • 9
    What does it mean to write: `xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"` then? – Casebash Apr 22 '10 at 02:24
  • I guess it is just a comment then :-( . – Martin Feb 05 '12 at 08:15
  • 2
    @Casebash it means that the namespace `app` is referencing the resources of `com.example.android.apis` package, then trying to resolve an attribute declared in say `attrs.xml` of your app. (Also the referenced libraries are in the same namespace of your app) You'd have to define `xsi:schemaLocation` to give the namespace an XSD Schema for validation. Some tools have internal predefined schema file mappings for certain namespaces (xml, xsd, ...). – TWiStErRob Aug 07 '14 at 12:38
  • Contrast to that: xaml has its standards and schema file: https://blogs.windows.com/buildingapps/2017/05/19/introducing-xaml-standard-net-standard-2-0/#L84ymmk3d05puI6l.97 – minus one Feb 06 '18 at 15:45
  • @Will unfortunately the link in your answer seems to be broken now – user3738870 Jun 05 '19 at 11:59
  • Down voting as the provided link is being reported as a spam url. Secondly, a schema is an XML (or the older DTD) whose purpose is to validate another XML. The XML schema is out there, but only hidden. Perhaps, is why gradle builds is so fragile as they have absolute whim to change the schema as they wish since nobody knows that it is. – daparic May 22 '20 at 11:21
7

Generally, defining a namespace in XML doesn't has to be a real existing URL but just a world wide unique String (therefore one prefers using their own URLs). Of course, it's nice if this URL contains the XML-schema (or worse, DTD). It would be also very nice if someone would create Android Ressource Schemata. I could help him as a bachelor thesis in CS. - Prof. Solymosi, Berlin

  • do you mean the URI is just a symbol maybe only means to the owned parser? if we need a DTD schema file for validation or completion, we have to write it ourselves? – fatfatson Mar 16 '19 at 02:48
  • You cannot write, at your own desires, the content of the schema. The schema is `another` XML that goes hand-in-hand to validate `the` XML. Older XML schemas are not in XML format but in DTD, i think use of DTD is no longer with us today – daparic May 22 '20 at 11:13
3

The XML schema doesn't seem to be documented, but there is a useful list of all the layout objects and their permitted attributes here:

http://developer.android.com/reference/android/R.styleable.html#lfields

Paul
  • 511
  • 3
  • 5
2

Been searching around the same subject to find out how android studio does the autocomplete & stuff in XML, and I too was hopeful to find some XSD or something, but:

In Android we use a mixture of static and dynamic DOM definitions:

1-Some files are defined using classes and annotations, see for example Manifest (note: to get correct information you should most likely use the merged manifest, but that's outside of the scope of this doc).

2-Other information is read from resources, using naming conventions to find a styleable that contains attrs relevant to a given XML tag. For example if we recognize a tag as corresponding to a View subclass in a layout file (e.g. “TextView”), we find the corresponding styleable, look at the attrs it contains and register DOM extensions for the given tag that correspond to these attr resources. See AttributeProcessingUtil and SubtagsProcessingUtil for code that reads styleables and AndroidDomExtender for the extension that plugs into the DOM system.

3-Sometimes the styleable is determined statically, but the attrs are read dynamically to stay up to date with the platform version used in the project. This is done using the @Styleable annotation.

https://android.googlesource.com/platform/tools/adt/idea/+/refs/heads/mirror-goog-studio-master-dev/android/src/org/jetbrains/android/dom/README.md

For example this is how a shape drawable XML is defined in source codes of android studio:

@DefinesXml
@Styleable("GradientDrawable")
public interface Shape extends DrawableDomElement {
  @Styleable("DrawableCorners")
  List<DrawableDomElement> getCornerses();

  @Styleable("GradientDrawableGradient")
  List<DrawableDomElement> getGradients();

  @Styleable("GradientDrawablePadding")
  List<DrawableDomElement> getPaddings();

  @Styleable("GradientDrawableSize")
  List<DrawableDomElement> getSizes();

  @Styleable("GradientDrawableSolid")
  List<DrawableDomElement> getSolids();

  @Styleable("GradientDrawableStroke")
  List<DrawableDomElement> getStrokes();
}

The styleables (like GradientDrawablePadding) are defined in android's attrs.xml

https://android.googlesource.com/platform/tools/adt/idea/+/refs/heads/mirror-goog-studio-master-dev/android/src/org/jetbrains/android/dom/drawable/Shape.java

navid
  • 1,022
  • 9
  • 20
0

If you've had a problem with a schema like http://schemas.android.com/apk/res/android being recognised, you can add it under Settings (or Preferences) > Languages & Frameworks > Schemas and DTDs. Click the + sign, under 'Ignored schemas and DTDs' and add the schema URL. You should no longer get code errors/warnings.

Al.
  • 173
  • 1
  • 11