本文介绍了具有 gradle 和 android 的不同 API 主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 gradle 为每个构建提供不同的 API 主机.理想情况下,我想通过我的代码访问常量,所以当我进行 gradle 构建时,它会构建 release.apk 以指向 http://example.com 和 debug.apk 指向 http://debug.example.com.

I was wondering if it is possible to provide a different API Host per build using gradle. Ideally I would like to access the constant through my code the same so when I do a gradle build, it builds the release.apk to point to http://example.com and the debug.apk to point to http://debug.example.com.

我使用以下方法实现了这一点:

I have achieved this using the following:

buildTypes {
    debug {
        buildConfig "public final static String API_HOST = \"http://debug.example.com\";"
    }

    release {
        buildConfig "public final static String API_HOST = \"https://example.com\";"
    }
}

不过这看起来很脏

干杯

推荐答案

另一种方法是使用 buildConfigField:

buildTypes {
  debug {
    buildConfigField 'String', 'API_HOST', '"https://debug.example.com"'
  }

  release {
    buildConfigField 'String', 'API_HOST', '"https://example.com"'
  }
}

然后您将通过 BuildConfig.API_HOST

这篇关于具有 gradle 和 android 的不同 API 主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!