简体   繁体   English

Android配置/属性文件的版本名称/代码

[英]Android configuration/Properties file for version Name/Code

What I would like to do is create a configuration/properties/xml file in an Eclipse project that can specify project settings. 我想做的是在Eclipse项目中创建一个configuration / properties / xml文件,该文件可以指定项目设置。

The context is that I have one application meant for 2 users. 上下文是我有一个针对2个用户的应用程序。 But they need to have different version and build numbers and some other parameters specific for that user. 但是他们需要具有不同的版本和内部版本号以及一些特定于该用户的参数。 What I would like is to have two configuration files, and then be able to specify Configuration file 1 or 2. The tricky part is that Version Code/name are specified in the android Manifest. 我想要的是拥有两个配置文件,然后能够指定配置文件1或2。棘手的部分是在Android清单中指定了版本代码/名称。

How is the best way to go about doing this? 如何做到这一点的最佳方法是什么?

Requirements: 要求:

  1. Specify in one part of code which configuration/properties file to use. 在代码的一部分中指定要使用的配置/属性文件。 (ie: Config 1 or 2) (即:配置1或2)
  2. Android Manifest is able to reference this file for Version Code and Version Name. Android Manifest可以引用此文件获取版本代码和版本名称。
  3. I can dynamically reference elements of this file in my code. 我可以在代码中动态引用该文件的元素。

So I found a good way to do this. 所以我找到了一个很好的方法。 I simply made a "configuration" xml file that has a bunch of resources in it. 我只是制作了一个“配置” xml文件,其中包含大量资源。

ie: 即:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="version_code">6</integer>
    <string name="build_version">1.5</string>
        <string name="app_LOGO"> "R.drawable.my_logo"</string>
</resources>

then in my manifest I have 然后在我的清单中

  android:versionCode="@integer/version_code"
  android:versionName="@string/build_version"

and then I made a configuration class. 然后我做了一个配置类。

public class Configuration {

    private int LOGO = 0;


    public Configuration(Context cont){

        String myResourceId = cont.getResources().getString(R.string.app_LOGO);
        String[] resourceParts = myResourceId.split("\\.");
        String packName = cont.getPackageName();
        int logo = cont.getResources().getIdentifier(resourceParts[2],               resourceParts[1], packName);

        setLogo(logo);
    }
    /**
     * @return the logo
     */
    public int getLogo() {
        return LOGO;
    }
    /**
     * set the Logo
     */
    public void setLogo(int logo) {
        LOGO = logo;
    }
}

Where I just call the Configuration class with the context of my activity passed into the constructor inside of my activities onCreate Method. 我只是在调用Configuration类的情况下,将活动的上下文传递到活动onCreate方法内部的构造函数中。 Works like a charm. 奇迹般有效。

Hopefully this helps someone 希望这可以帮助某人

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM