Package name alias or nickname

2019-04-07 09:45发布

问题:

Is it possible to create a nickname for a package name, that I can refer in xml file?

I don't want to write the whole package name e.g. com.example.go.deck.today instead can I create an alias and refer that alias?

I'd like to refer an alias or nick instead of the package name in an android/layout XML file.

回答1:

Android by itself does not provide such facility. However you can use the ant to do it for you. You could write a custom build.xml, customizing the target <target name="-code-gen"> to do token replacement before the R class is generated. You could add the rule :

<replace file="targetFile" token="@PACKAGE_ALIAS@" value="com.example.go.deck.today"/> 

this would replace every occurrence of @PACKAGE_ALIAS@ by com.example.go.deck.today in the targetFile.

I am not an ant specialist but based on past experience I would say that it would not be hard to write a rule that could scan all file in the res dir.



回答2:

No, sorry, this is not possible, at least not directly within the Android toolset.



回答3:

Though its not directly possible to refer to a package name, however, for using a ‘resource’ in a xml or code is :

package.R.resource_type.resource_name

and

@[package:]resource_type/resource_name

NOTE: package here does not mean ‘java package’ but package == application, so if you are accessing a resource which is defined in your own app you can skip it. Thats why we use @strings/name or directly R.strings.name. Similarly to use resources defined by android we will use @android:string/name or android.R.string.name