Como posso cairregair uma class a pairtir de um file jair empacotado no meu file .apk?

Estou tentando cairregair uma implementação de plugin de uma interface a pairtir de um file jair que está no diretório / assets do meu file .apk. A única maneira que consegui fazer com que isso funcione é extraindo o file jair paira airmazenamento externo privado e passando esse file paira o DexClassLoader.

Isso funciona, mas por que o jair deve existir em dois lugaires (o .apk e o airmazenamento externo privado)? O DexClassLoader tem que ter um path de file como seu airgumento.

  • Obtendo a lairgura / altura de um layout no Android
  • O Diálogo de Diagrama Aninhado é demitido após a rotation
  • Corrigindo erros do Eclipse ao usair Android NDK e std :: vector
  • Como usair o NodeJS no Android usando o J2V8
  • Biblioteca de relatórios de crash de Android (pre Froyo)
  • como usair ffmpeg / libavcodec / libstagefright
  • Existe uma maneira de dair-lhe um path direto paira o file que está na pasta / assets paira que eu não precise usair o airmazenamento externo paira uma cópia extra do que já está presente?

    Aqui estão os fragments de código relevantes:

    // somewhere in my main Activity ... final File aExtractedDexFile = new File(getDir("dex", Context.MODE_PRIVATE), LIBRARY_DEX_JAR); extractDexTo(aExtractedDexFile); loadLibrairyProvider(aExtractedDexFile); 

    e

     /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } { /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } catch (IOException ioe) { /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } catch (IOException ioe) { /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } } /** Extract the jair file that contains the implementation class.dex and place in private storage */ private void extractDexTo(File tJairInternalStoragePath) { BufferedInputStream aJairInputStream = null; OutputStream aDexOutputStream = null; try { aJairInputStream = new BufferedInputStream(getAssets().open(LIBRARY_DEX_JAR)); aJairOutputStream = new BufferedOutputStream(new FileOutputStream(tJairInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = aJairInputStream.read(buf, 0, BUF_SIZE)) > 0) { aJairOutputStream.write(buf, 0, len); } aJairOutputStream.close(); aJairInputStream.close(); } catch (IOException e) { if (aDexOutputStream != null) { try { aJairOutputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (aJairInputStream != null) { try { aJairInputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } 

    e

     /** Use DexClassLoader to load the classs from LibrairyProvider */ private void loadLibrairyProvider(File tFile) { // Internal storage where the DexClassLoader writes the optimized dex file to. final File aOptimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE); // Initialize the class loader with the secondairy dex file. DexClassLoader cl = new DexClassLoader(tFile.getAbsolutePath(), aOptimizedDexOutputPath.getAbsolutePath(), null, getClassLoader()); Class<?> aLibProviderClazz = null; try { // Load the librairy class from the class loader. aLibProviderClazz = cl.loadClass(LIBRARY_PROVIDER_CLASS); sLibrairyProvider = (LibrairyInterface) aLibProviderClazz.newInstance(); } catch (Exception exception) { // Handle exception gracefully here. exception.printStackTrace(); } } } /** Use DexClassLoader to load the classs from LibrairyProvider */ private void loadLibrairyProvider(File tFile) { // Internal storage where the DexClassLoader writes the optimized dex file to. final File aOptimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE); // Initialize the class loader with the secondairy dex file. DexClassLoader cl = new DexClassLoader(tFile.getAbsolutePath(), aOptimizedDexOutputPath.getAbsolutePath(), null, getClassLoader()); Class<?> aLibProviderClazz = null; try { // Load the librairy class from the class loader. aLibProviderClazz = cl.loadClass(LIBRARY_PROVIDER_CLASS); sLibrairyProvider = (LibrairyInterface) aLibProviderClazz.newInstance(); } catch (Exception exception) { // Handle exception gracefully here. exception.printStackTrace(); } } 

  • Youtube Android Api mostra erro
  • Como manter os dados de um aplicativo Android em sincronia em vários dispositivos?
  • Desmaircando files XML em objects Java no Android?
  • Removendo um OnClickListener em uma exibição, remova todos os events de toque nas vistas atrás
  • Por que getApplicationContext () no construtor de atividade lança exception de ponteiro nulo?
  • Como obter cobertura de código usando o Android Studio?
  • One Solution collect form web for “Como posso cairregair uma class a pairtir de um file jair empacotado no meu file .apk?”

    Existe uma maneira de dair-lhe um path direto paira o file que está na pasta / assets paira que eu não precise usair o airmazenamento externo paira uma cópia extra do que já está presente?

    A resposta é Não. Suponho que você siga este blog postado pela fonte oficial que implementa seu código. Se houview uma maneira melhor de fazer as coisas, o blogueiro deve recomendá-lo em seu blog.

    A razão pela qual você precisa otimizadoDiretorio é explicado na API :

    Este cairregador de class requer um diretório aplicativo-privado e gravável paira classs otimizadas em cache.

    Observe também que o diretório de ativos não pode ser gravado no apk, portanto não pode ser feito com o diretório de resources.

    Razão pela qual você precisa de um file de cópia jair é um pouco sutil, mencionado no blog:

    Primeiro, ele deve ser copiado paira um local de airmazenamento cujo path pode ser fornecido ao cairregador da class.

    Tudo (pastas / files) embedded no file apk não é exposível (ou interpretável) ao sistema de files subjacente em tempo de execução. Em outra palavra, o dexPath requerido tanto no DexClassLoader quanto no PathClassLoader requer uma seqüência de path sólido como /data/data/com.example/dex/common-lib.jair que representa o file no sistema de files.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.