Search code examples
javaandroidjsonandroid-gradle-pluginorg.json

Don't use android's built in org.json


I've written a library that uses org.json (A) from json.org, under the assumption that Android used the same (in android it's also called org.json (B), just that it misses some relatively critical features). Now what I'd like to do is set up my gradle, so that my project uses org.json (A) instead of org.json (B). I've tried adding the following to my app.gradle:

android {
    blabla
    defaultConfig {
        blabla
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets{
        main{
            java{
                exclude 'org/json/**'
            }
        }
    }

}

However, this does not seem to work. I still have Android's org.json (B) available to me, and when typing new JSONObject() it still uses Android's implementation. How could I fix this?


Solution

  • How could I fix this?

    Change yours from org.json to org.something.else. You cannot replace system-supplied packages with ones from an app, even for your own process. You do not control the runtime classpath, and the firmware always wins.

    Or, switch from org.json to something that performs better, such as Gson or Jackson.