1、在build.gradle中加入读取1.json文件代码,放在android{}外面
String str = ""
try {
File file = new File("${project.rootDir}/1.json")
Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8")
int ch = 0
StringBuilder sb = new StringBuilder()
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
reader.close()
str = sb.toString()
JSONObject jsonObject = new JSONObject(str)
println(str)
return jsonObject
} catch (IOException | JSONException e) {
e.printStackTrace()
} }
2、在android{} 中引用
JSONObject json = getVersion()
int code = json.getInt("versionCode")
String versionName2 = json.getString("versionName")
compileSdkVersion 31
defaultConfig {
applicationId ""
minSdkVersion 19
targetSdkVersion 31
versionCode code
versionName versionName2
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }