Testing on projects these days often involves alternating testing between packaged test and real server projects, and often uninstalling and installing projects is a hassle. To make it easier for your colleagues to test, get ready to fiddle with gradle configuration.

Set a few needs for yourself:

  • Gradle configures multiple Applicationids
  • Gradle configures multiple AppNames
  • Gradle is configured with multiple hosts

To configure Gradle properly, you only need to configure the Gradle file in your app

Android {productFlavors {// Default version, no applicationId, inherit the configuration of defaultConfig online {resValue "string", "app_name", "Online application" // Replace appName, by replacing the resource name in strings, ApplicationId "com.xx.xx" buildConfigField "String", "API_HOST", "\" https://192.168.1.14/\ "" dev_153 {} / / 153 version resValue" string ", "app_name", "Application" applicationId "com.xx.yy" buildConfigField "String", "API_HOST", "\"https://192.168.1.16/\""}}}Copy the code
  • Replace appName. By replacing the resource name in strings, be sure to delete the app_name in strings, otherwise an error will be reported
  • ApplicationId is the name of the package you want to replace
  • Generate the HOST constant with buildConfigField, which is called in Java code with buildConfig.api_host

When configuring multiple Applicationids, note that applicationids are replaced dynamically, as well as where they are used in your project.

  • 7.0 adaptation of FileProvider, AndroidManifest replacement

  • ApplicationId replacement during application installation in 7.0

  • Dynamic PackageName, more than one situation, if you are not a moudle, you can also get applicationId by buildConfig

    public static void startInstall(Context context, File apkfile) { if (! apkfile.exists()) { KLog.d(TAG, “startInstallexists”); return; } KLog.d(TAG, “startInstall”); Intent i = new Intent(Intent.ACTION_VIEW); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); If (build.version.sdk_int >= 24) {if (build.version.sdk_int >= 24) { Parameter 2 Provider host address and consistent parameters in the configuration file of 3 Shared file Uri apkUri = FileProvider. GetUriForFile (context, context.getPackageName() + “.fileprovider”, apkfile); // Dynamic PackageName, more than one situation, if you are not a moudle, You can also get the applicationId by buildConfig. // Add this sentence to temporarily authorize the file represented by the Uri for the target application. i.setDataAndType(apkUri, “application/vnd.android.package-archive”); } else { i.setDataAndType(Uri.fromFile(apkfile), “application/vnd.android.package-archive”); } context.startActivity(i); }


The above is the normal configuration of the project, but because my project is a multi-moudle dependency, the network layer is at the lowest level of the dependency of the whole project. If you configure HOST in app Gradle, it cannot be called in the dependency moudle of the lowest level, because gradle dependencies can only be called forward, and the upper level references the moudle of the bottom level. The bottom moudle cannot reverse use the top moudle’s resources and code, so the app buildconfig. API_HOST cannot be called.

##### I define buildConfig in the app build to pass to the lowermost moudle to switch HOST

 UrlConstant.API_HOST = BuildConfig.API_HOST;
 UrlConstant.H5_HOST = BuildConfig.H5_HOST;
 UrlConstant.SERVICE_HOST = BuildConfig.SERVICE_HOST;
 UrlConstant.SHOP_HOST = BuildConfig.SHOP_HOST;
Copy the code

# # # # reference: http://www.tuicool.com/articles/Nzm6fiI

The above is the configuration I need. If you have multiple channels or other requirements, you can also follow the configuration.

If where to write wrong, also welcome everyone to point out and exchange 😁