【问题标题】:How to read a config file which is under app folder如何读取应用程序文件夹下的配置文件
【发布时间】:2018-06-07 05:52:06
【问题描述】:

我在 app 文件夹中有一个配置文件 config.properties。我在build.gradle 中用于构建配置。 我需要用 java 代码读取这个文件。但我可以弄清楚我应该使用path。我如何在 java 代码中读取这个文件。下面的代码给了我FileNotFoundException

 try {
        Properties properties = new Properties();
        File inputStream=new File("/config.properties");
        properties.load(new FileInputStream(inputStream));
        return properties.getProperty("BASE_URL");
    }catch (IOException e){
        e.printStackTrace();
    }

我在build.gradle 中使用相同的文件并且它运行良好。如下。

 defaultConfig {
    Properties versionProps = new Properties()
    versionProps.load(new FileInputStream(file('config.properties')))
    def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
    def properties_versionName = versionProps['VERSION_NAME']
    def properties_appid= versionProps['APPLICATION_ID']


    applicationId properties_appid
    minSdkVersion 14
    targetSdkVersion 26
    versionCode properties_versionCode
    versionName properties_versionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

build.gradle 部分工作正常,我可以分配属性。但我需要在 java 类中读取相同的文件。 File 应该使用什么路径。

配置文件如下:

VERSION_NAME=1.0.0
VERSION_CODE=1
APPLICATION_ID=com.app.drecula
BASE_URL=https://reqres.in/

【问题讨论】:

  • 您是否尝试在您的应用程序中读取它?
  • 是的,该文件位于与google_play_service.json平行的app文件夹下
  • 我能问一下你想从配置文件中得到什么吗? ?

标签: android gradle android-file


【解决方案1】:

理想情况下,您应该将应用程序特定文件放在资产文件夹下,并将您的 gradle 构建配置和应用程序特定配置分开。在assets/configs 文件夹下添加应用程序特定的配置属性文件。那么你可以这样读:

  final Properties properties = new Properties();
  final AssetManager assetManager = getAssets();
  final InputStream inputStream= assetManager.open("configs/config.properties");
  properties.load(inputStream);

如果您仍想继续,那么唯一的方法是将文件放入 assets 文件夹并在您的 build.gradle 中使用

versionProps.load(new FileInputStream(file('/src/main/assets/config/config.properties')))

【讨论】:

  • 我说它在 build.gradle 工作。我需要在 java 类中阅读它。
  • 我无法将文件放入 assets 中。如果我将文件放入资产中,那么我将如何加载build.gradle
  • @Diazdiaz 您无法同时读取文件。如果您希望该文件成为 apk 的一部分并使用它,则必须将其放入 assets。将相同的文件用于 gradle 构建配置和应用程序配置并不是一个好主意。你应该把它们分开。
  • 这就是需要。我需要在assets 的任何其他地方都有一个文件。就不可能有一个文件吗?
  • @Diazdiaz 你可以从这个SO查看我的答案使用这个,你可以将内容写入 BuildConfig 文件并在你的应用程序中使用它
【解决方案2】:

好的,让我们回到基础。首先在build.gradle 中创建属性,然后使用自定义字段创建buildConfigField

productFlavors {
    play {
        dimension "MyDimension"

        Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(file('config.properties')))
        def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
        def properties_versionName = versionProps['VERSION_NAME']

        //...

        buildConfigField "int", "MY_VERSION_CODE", "$properties_versionCode"
        buildConfigField "String", "MY_VERSION_NAME", "\"$properties_versionName\""

    }
}

重建项目后,字段将能够从BuildConfig.{FIELD} 调用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //...

    Log.d(TAG, "Version Name: " + BuildConfig.MY_VERSION_NAME);
    Log.d(TAG, "Version Code: " + BuildConfig.MY_VERSION_CODE);

    //...
}

更多信息here

【讨论】:

  • 这么简单吗?我太笨了。我已经看过这个解决方案但不明白。谢谢 。现在我懂了 。打算写在石头上。
猜你喜欢
  • 2013-11-12
  • 2020-07-12
  • 2022-09-27
  • 2016-05-27
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多