Android 4.1 с инструментами сборки 28 приводит к NoClassDefFoundError в нечетном месте - PullRequest
0 голосов
/ 08 февраля 2019

У меня есть странный случай, я использую инструменты сборки 28 и нацеливаюсь на SDK версии 28, но с минимальным уровнем 16.

Ниже код работал нормально, когда я использовал целевой уровень 10. (Вминимум до Android 8)

На Android 6,7,8,9 код с конца работает, но не на Android 4.1 (не тестировался на Android 5)

Вот исключение (строки кода немного не совпадают в этом примере, потому что мне пришлось урезать некоторые комментарии):

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)

Вот проблемная часть кода:

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);
    }
};

Вот весь класс:

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;
    }
}
...