在开发过程中,多环境配置是经常遇到的,比如在Android开发过程中,在不同环境上请求服务器的URL是不同的,使用Gradle进行管理,是非常方便的。

首先查看工程目录结构:



使用AndroidStudio开发的看到这个熟悉吧。main就是目前开发的环境。dev为测试环境。product,staging为其他环境,当然还可以有其他更多环境。

1、将请求的URL定义到Constant常量类中:

public class Constant {

public static final String URL= “http://XXXXX“;

}

在dev,product,staging等环境中添加Constant类,并且设置不同URL即可。

设置后目录结构如下:我的app包名为com.example.XX.myapplication



这里需要注意的是不要在main环境中添加Constant类,否则类就重复了,Gradle编译时会报:dumplicate class XXX

使用时就和普通类使用方式一样!

Gradle配置:

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "19.1.0" lintOptions {
abortOnError false
} defaultConfig {
applicationId "com.example.teamlab.myapplication"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
signingConfigs {
debug {
storeFile file("src/main/keystore/debug.keystore")
storePassword "android"
keyPassword "android"
}
release {
storeFile file("src/main/keystore/debug.keystore")
storePassword "android"
keyPassword "android"
}
staging {
storeFile file("src/main/keystore/debug.keystore")
storePassword "android"
keyPassword "android"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
dev {
applicationId 'com.example.teamlab.myapplication.dev'
signingConfig signingConfigs.debug
}
staging {
signingConfig signingConfigs.debug
applicationId 'com.example.teamlab.myapplication.staging'
}
product {
applicationId 'com.example.teamlab.myapplication'
signingConfig signingConfigs.debug
}
}
packagingOptions {
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}
} dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.+'
compile 'com.android.support:support-v4:21.+'
compile 'cn.pedant.sweetalert:library:1.3'
compile 'com.mcxiaoke.volley:library:1.0.+'
androidTestCompile 'junit:junit:4.10'
androidTestCompile 'org.robolectric:robolectric:2.3+'
androidTestCompile 'com.squareup:fest-android:1.0.+'
compile project(':slidingmenu')
}

Gradle 多环境URL请求设置的更多相关文章

  1. QWebEngineView拦截Url请求设置

    QWebEngineView拦截Url请求设置 QWebEngineUrlRequestInterceptor类提供了一个拦截URL请求抽象基类,可通过实现QWebEngineUrlRequestIn ...

  2. IIS 7.5 + asp.net MVC4 设置路由处理URL请求

    使用asp.net MVC4开发的网站,在本地的VS012环境下运行,一切正常.但当发布到Windows 2008 R2(IIS7.5 + Framework4.5)上时,访问相关网页时,出现有下面的 ...

  3. Postman系列四:Postman接口请求设置环境变量和全局变量、测试沙箱和测试断言、测试集运行与导入数据文件

    一:Postman中接口请求设置环境变量和全局变量 全局变量和环境变量可以通过Pre-request Script和Tests设置,会在下面测试沙箱和测试断言中讲到. 全局变量的设置:官网参考http ...

  4. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  5. java.net.URL请求远程文件下载

    1:浏览器请求下载 public void listStockcodeUplaod(HttpServletRequest req, HttpServletResponse res) throws Ex ...

  6. {Django基础七之Ajax} 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解)

    Django基础七之Ajax 本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 一 Ajax简介 ...

  7. IOS网络篇1之截取本地URL请求(NSURLProtocol)

    本文转载至 http://blog.csdn.net/u014011807/article/details/39894247 NSURLProtocol 是iOS中非常重要的一个部分,我们经常会在以下 ...

  8. django系列6--Ajax04 请求设置(设置csrf_token)

    Ajax请求设置csrf_token 官方文档 csrf_token: https://docs.djangoproject.com/en/1.11/ref/csrf/ CSRF 跨站请求攻击,简单地 ...

  9. 转发URL请求

    如何用Netty实现一个轻量级的HTTP代理服务器 - Newland - 博客园 https://www.cnblogs.com/jietang/p/8926325.html 现在迫切希望有一个HT ...

随机推荐

  1. 基于 Python 的自动文本提取:抽象法和生成法的比较

    我们将现有的 提取方法(Extractive)(如LexRank,LSA,Luhn和Gensim现有的TextRank摘要模块)与含有51个文章摘要对的Opinosis数据集进行比较.我们还尝试使用T ...

  2. 带你玩linux集成环境

    据我现在知道的,在linux一键部署集成环境的应用有: “宝塔面板” “LNMP" "XRDP" 参考:http://www.ampnmp.com/ 具体什么安装就直接百 ...

  3. QtConcurrent::run 运行类的成员函数

    https://stackoverflow.com/questions/2152355/is-it-possible-to-use-qtconcurrentrun-with-a-function-me ...

  4. Zabbix数据库表分区

    zabbix的监控主机数量将近300,且运行了一年时间了,最近zabbix server服务监控历史数据等服务不断自身告警.查询性能也变得很低 关于历史数据的两个参数,在zabbix server的配 ...

  5. Scala教程之:面向对象的scala

    文章目录 面向对象的scala Unified Types Classes Traits 面向对象的scala 我们知道Scala是一种JVM语言,可以合java无缝衔接,这也就大大的扩展了scala ...

  6. JDK14的新特性

    文章目录 虽然JDK13在今年的9月17号才发布,但是丝毫不会影响到下一个版本JDK14的开发工作.听说官方定的新功能马上就要官宣了,我们这里不妨来提前推断一下. 在9月17号的发布中,Oracle提 ...

  7. 【Linux常见命令】tee命令

    tee - read from standard input and write to standard output and files tee命令用于读取标准输入的数据,并将其内容输出成文件. t ...

  8. 15个Nodejs应用场景

    15个Nodejs应用场景 我们已经对Nodejs有了初步的了解,接下来看看Nodejs的应用场景. 2.1 Web开发:Express + EJS + Mongoose/MySQL express  ...

  9. Nodejs的介绍

    Nodejs的介绍 Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速,可扩展的网络应用程序的平台.Node.js使用事件驱动,非阻塞I/O模型,轻量.高效,可以完美 ...

  10. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...