我编写了一个使用 org.json
的库(A) 来自 json.org,假设 Android 使用相同(在 android 中也称为 org.json
(B),只是它缺少一些相对关键的功能)。现在我想做的是设置我的 gradle,以便我的项目使用 org.json
(A) 而不是 org.json
(乙)。我尝试将以下内容添加到我的 app.gradle:
android {
blabla
defaultConfig {
blabla
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets{
main{
java{
exclude 'org/json/**'
}
}
}
}
但是,这似乎不起作用。我还有安卓的
org.json
(B) 对我可用,并且在键入 new JSONObject()
时它仍然使用Android的实现。我该如何解决这个问题?
最佳答案
您可以搬迁您的org.json
依赖于另一个包名。
这可以通过 gradle shadow 插件来完成。
插件项目:https://github.com/johnrengelman/shadow
插件文档:https://imperceptiblethoughts.com/shadow/
我的工作示例:
提示:我在我的 Android 项目中使用了一个 java 库项目“lib”,它依赖于 org.json
。 .
lib/build.gradle
apply plugin: 'java-library'
apply plugin: 'com.github.johnrengelman.shadow'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
}
}
shadowJar {
relocate 'org.json', 'shadow.org.json'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('org.json:json:20180813')
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
应用程序/build.gradle
apply plugin: 'com.android.application'
android {
[..]
}
dependencies {
[..]
implementation project(path: ':lib', configuration: 'shadow')
}
关于java - 不要使用android内置的org.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33803776/