Добавление реализации poi-ooxml выдает ошибку Невозможно создать экземпляр appComponentFactory - PullRequest
0 голосов
/ 27 октября 2019

Я пытаюсь прочитать данные из файла xlsx и загрузить их в Firebase Firestore. Я успешно прочитал данные из файла xls и загрузил его.
При использовании реализации "org.apache.poi: poi-ooxml: $ poi_version" приложение успешно создается, но при запуске вылетает, что приводит к исключению времени выполнения: "невозможно создать экземпляр appComponentFactory" вместе с "невозможно получить поставщик com. google.firebase.provider.FirebaseInitProvider».

Logcat:

 2019-10-27 12:12:17.840 29250-29250/com.unic.unicproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.unic.unicproject, PID: 29250
    java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/lib/arm64, /system/lib64, /vendor/lib64]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:6775)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6317)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6232)
        at android.app.ActivityThread.access$1200(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7078)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6759)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6317) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6232) 
        at android.app.ActivityThread.access$1200(ActivityThread.java:237) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7078) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 
        Suppressed: java.io.IOException: Failed to open dex files from /data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/base.apk because: Bad encoded_array value: Failure to verify dex file '/data/app/com.unic.unicproject-ESVzX0-aaE-8H2n0qEbudQ==/base.apk': Bad encoded_value method type size 7
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:354)
        at dalvik.system.DexFile.<init>(DexFile.java:101)
        at dalvik.system.DexFile.<init>(DexFile.java:75)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
        at dalvik.system.DexPathList.<init>(DexPathList.java:164)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:766)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:849)
        at android.app.LoadedApk.getResources(LoadedApk.java:1090)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2563)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6128)
                ... 8 more

build.gradle приложения

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.unic.unicproject"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-firestore:21.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.apache.poi:poi:4.1.1'
    implementation 'org.apache.poi:poi-ooxml:4.1.1'
    implementation 'org.jetbrains:annotations-java5:15.0'

}

MainActivity.java:

package com.unic.unicproject;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.firestore.FirebaseFirestore;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnUpload,btnRetrieve,btnSelect;
    //btnSelect selects file from storage
    //btnUpload uploads data to Firebase
    //btnRetrieve returns data from Firebase and displays it in CardView (not implemented yet)
    private FirebaseFirestore db;
    private HSSFWorkbook workbook;
    private File excelFile;
    private int noOfSheets;
    private static final int EXCEL_FILE = 101;
    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSelect = findViewById(R.id.btnSelect);
        btnSelect.setOnClickListener(this);
        btnUpload = findViewById(R.id.btnUpload);
        btnUpload.setOnClickListener(this);
        btnRetrieve = findViewById(R.id.btnRetrieve);
        btnRetrieve.setOnClickListener(this);

        db = FirebaseFirestore.getInstance();
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnSelect:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent,EXCEL_FILE);
                break;
            case R.id.btnUpload:
                getData();
                break;
        }
    }


    public void getData(){
        try {
            FileInputStream fis = new FileInputStream(excelFile);
            //XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);
            HSSFWorkbook workbook= new HSSFWorkbook(fis);
            noOfSheets = workbook.getNumberOfSheets();
            HSSFSheet[] sheet = new HSSFSheet[noOfSheets];
            for(int i=0;i<noOfSheets;i++){
                sheet[i]=workbook.getSheetAt(i);
                ArrayList<String> Header = new ArrayList<>();
                Iterator<Row> rowIterator = sheet[i].rowIterator();
                if(rowIterator.hasNext()){
                    HSSFRow header = (HSSFRow)rowIterator.next();
                    Iterator<Cell> headIterator = header.cellIterator();
                    do {
                        HSSFCell cell = (HSSFCell)headIterator.next();
                        Header.add(cell.toString());

                    }while(headIterator.hasNext());
                    do {
                        HashMap<String,String> dataMap = new HashMap<>();
                        HSSFRow row = (HSSFRow) rowIterator.next();
                        Iterator<Cell> cellIterator = row.cellIterator();
                        int j=0;
                        do {
                            HSSFCell cell = (HSSFCell) cellIterator.next();
                            dataMap.put(Header.get(j),cell.toString());
                            j++;
                        }while(cellIterator.hasNext());

                        db.collection("products").add(dataMap);
                    }while(rowIterator.hasNext());

                }
            }
        } catch (Exception e){
            Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==EXCEL_FILE&&resultCode==RESULT_OK){
            Uri uri = data.getData();
            getFilePath(uri);
        }
    }
    private void getFilePath(@NotNull Uri uri){
        if(isDocumentUri(uri)) {
            String loc = uri.getPath().split("/")[2].split(":")[0];
            String[] trim = uri.getPath().split(":");
            if(loc.equals("home"))
                excelFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), trim[1]);
            else if (loc.equals("primary"))
                excelFile = new File("/storage/emulated/0/"+trim[1]);
        } else if(isDownloadsUri(uri)){
            Toast.makeText(this, uri.getPath(), Toast.LENGTH_SHORT).show();
            String[] trim = uri.getPath().split(":");
            excelFile = new File(trim[1]);
        } else {
            //Toast.makeText(this, uri.getPath(), Toast.LENGTH_SHORT).show();
        }
    }

    private boolean isDocumentUri(@NotNull Uri uri){
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }
    private boolean isDownloadsUri(@NotNull Uri uri){
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
}

Thisкод работал нормально только с реализацией org.apache.poi: poi для книг xls. Но это нужно обновить, чтобы прочитать xlsx, так как я должен загружать изображения в хранилище Firebase после чтения их из файла Excel.

...