There is an optimization requirement for SDK in the recent project. The SDK will initialize various functions according to the configuration file, and it is hoped that the default configuration file inside SDK can be overwritten by obtaining the corresponding configuration file from the background according to the connected project when packaging.

This optimization requirement is also implemented through gradle plugin.

  1. Obtain the configuration file from the background.
  2. The configuration files inside the SDK are overwritten during packaging.

How to create a network request in Gradle plugin, in the previous article: Android custom Gradle plugin (three) : dynamic dependency has been introduced, here is no longer described.

Modify the files in assets while packaging

In the previous article, Manifest was manipulated in the packaging process. Based on this, I observed the remaining tasks that would be executed during the packaging process and found the Task that operated on assets as shown below:

I choose mergeDebugAssetsTask to fulfill the requirement. This Task literally means merging assets files. Therefore, after executing this Task, find the path of the SDK internal configuration file and operate it, the code is as follows:

Public class FileUtils {/** ** save files to folder ** @param floderFile folder to save files * @param fileName fileName * @param fileContentData */ Public static void saveFileToFolder(File floderFile, String fileName, String fileContentData) { File createFile = new File(floderFile, fileName); LogUtils.println("create file path :" + createFile.getPath()); // Delete the old file if (createfile.exists ()) {Boolean delete = createfile.delete (); LogUtils.println("delete old file success :" + delete); } try { boolean create = createFile.createNewFile(); LogUtils.println("create new file success :" + create); } catch (IOException e) { e.printStackTrace(); } if (createFile.exists()) { FileOutputStream outputStream = null; OutputStreamWriter outputStreamWriter = null; BufferedWriter writer = null; try { outputStream = new FileOutputStream(createFile); outputStreamWriter = new OutputStreamWriter(outputStream); writer = new BufferedWriter(outputStreamWriter); writer.write(fileContentData); } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer ! = null) { writer.close(); } if (outputStreamWriter ! = null) { outputStream.close(); } if (outputStream ! = null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } } } class MyPlugin implements Plugin<Project> { @Override public void apply(Project project) { AfterEvaluate {// Take mergeDebugAssets as an example, and mergeReleaseAssets as well. Task mergeAssetsTask = project.getTasks().getByName("mergeDebugAssets") mergeAssetsTask.doLast { def outputs = mergeAssetsTask.getOutputs() def files = outputs.files.getFiles() boolean findTargetFile = false files.find { def childFile = it.listFiles() childFile.find { findTargetFile = it.toString().contains("mini_game_config.json") if (findTargetFile) { LogUtils.println("writeNetConfigToAssets") FileUtils.saveFileToAppFolder(it, "", "NetConfigContent")} return findTargetFile} return findTargetFile}}}}}Copy the code

The log output is as follows: