You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android纯Java包使用JSON库遭遇NoSuchMethodError异常求助

Fixing Android org.json Library Conflict in Pure Java Modules

Hey, I’ve dealt with this exact headache before—let’s break down why this is happening and how to get your pure Java package using JSON smoothly again.

What’s Causing the Error?

Android ships with its own version of the org.json library embedded in core-libart.jar. When you add the official org.json dependency to your pure Java module, your code compiles against the newer library you imported, but at runtime, Android prioritizes its older, stripped-down system version. That’s why you’re seeing NoSuchMethodError—the system’s JSONObject doesn’t have the methods your code is trying to call (those weird g()/b() names are just ProGuard/R8-obfuscated method signatures from your imported library).

Solutions to Try

1. Relocate the JSON Library Package (Best for Minimal Code Changes)

The cleanest fix is to rename the package of your imported org.json library so it doesn’t clash with Android’s built-in one. You can do this easily with Gradle’s Shadow plugin, which "shadows" the library under a new package name:

Step 1: Add the Shadow Plugin to Your Pure Java Module’s build.gradle

plugins {
    id 'java-library'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

shadowJar {
    // Replace 'com.yourapp.shadowed.json' with a unique package name for your app
    relocate 'org.json', 'com.yourapp.shadowed.json'
}

dependencies {
    implementation 'org.json:json:20230618' // Use the latest stable version
}

Step 2: Update Your Imports

In your pure Java code, swap out the old imports for the shadowed package:

// Before
import org.json.JSONObject;

// After
import com.yourapp.shadowed.json.JSONObject;

Now your code will use the renamed, imported library at runtime instead of Android’s built-in version.

2. Switch to an Alternative JSON Library

If you don’t want to mess with package relocation, you can replace org.json with a different library that doesn’t share the same package name. Popular options include:

  • Gson: Lightweight and easy to use for simple parsing/serialization
    import com.google.gson.JsonObject;
    import com.google.gson.Gson;
    
    public JsonObject getExtraFlag() {
        return new Gson().fromJson(mExtraFlag, JsonObject.class);
    }
    
  • Jackson: More powerful for complex use cases, but slightly heavier

Since your usage is just simple getters/setters for ORMLite, Gson would be a quick drop-in replacement.

3. Adjust ProGuard/R8 Rules (If Obfuscation Is an Issue)

If your project uses code obfuscation, it might be renaming methods in your imported org.json library, leading to mismatches at runtime. Add these rules to your ProGuard config to preserve the JSON library’s methods:

-keep class org.json.** { *; }
-keep interface org.json.** { *; }

Note: This only fixes obfuscation-related issues—you’ll still run into the core library conflict unless you combine it with package relocation or a different library.

Final Notes

Since your pure Java module only interacts with Android via strings/longs, package relocation is the most straightforward fix. It lets you keep using the org.json API you’re already familiar with while avoiding the system library clash.

内容的提问来源于stack exchange,提问作者rayano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:25:05