2

I have an odd case here, I use build tools 28 and I'm targeting SDK version 28, but with min level 16.

Below code worked fine when I was using target level 10. (At least up to Android 8)

On Android 6,7,8,9 code from the end works, but not on android 4.1 (not tested on Android 5)

Here is the exception (lines of code are a bit off in the example cause I had to cut some comments):

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.some.appname.ConfigurationLoader$1
    at com.some.appname.ConfigurationLoader.load(ConfigurationLoader.java:46)
    at com.some.appname.SomeApplication.onCreate(SomeApplication.java:80)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
    at android.app.ActivityThread.access$1300(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

Problematic part of code is this one:

StartElementListener ol = new StartElementListener() {

    @Override
    public void start(Attributes attributes) {
        int nameIndex = attributes.getIndex("name");
        int valueIndex = attributes.getIndex("value");

        String name = attributes.getValue(nameIndex);
        String value = attributes.getValue(valueIndex);

        configuration.put(name, value);
    }
};

Here is entire class:

package com.some.appname;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.content.Context;
import android.sax.Element;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

/* package */class ConfigurationLoader {

    private static final String TAG = "ConfigurationLoader";

    /**
     * Loads options from xml file
     * 
     * @param context
     *            Context in which file is stored
     * @return Loaded configuration
     * @throws IOException
     *             If io errors occurs
     * @throws SAXException
     *             If config.xml is invalid
     */
    /* package */static Configuration load(Context context) throws IOException,
            SAXException {
        Log.i(TAG, "Loading configuration");

        final Configuration configuration = Configuration.getConfiguration();

        RootElement root = new RootElement("configuration");
        Element option = root.getChild("option");

        StartElementListener ol = new StartElementListener() {

            @Override
            public void start(Attributes attributes) {
                int nameIndex = attributes.getIndex("name");
                int valueIndex = attributes.getIndex("value");

                String name = attributes.getValue(nameIndex);
                String value = attributes.getValue(valueIndex);

                configuration.put(name, value);
            }
        };

        option.setStartElementListener(ol);

        InputStream in = context.getResources().openRawResource(R.raw.config);
        Xml.parse(in, Xml.Encoding.UTF_8, root.getContentHandler());

        return configuration;
    }
}
dr4cul4
  • 258
  • 4
  • 14

0 Answers0