2017-05-09 985 views
0

我知道Jar和AAR之間的主要區別在於AAR包含佈局,繪圖等資源。我想創建一個包含Bitmap類的Jar文件。這是否可能?如何導入jar文件中的android.graphics.Bitmap

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.widget.ImageView; 

,當我在JAR文件類import andorid.graphics.Bitmap Android Studio中 顯示錯誤:無法解析符號 '位圖',「BitmapFactory「Context'and .. enter image description here

我按照以下步驟構建JAR文件:

文件>新建>新建模塊> Java庫

我知道如何使用圖書館,我也知道在那個AA Andorid的API類,比如可用的位圖R(也在AAR文件中的可用資源文件) 我的問題是在JAR文件中使用android api類。

+0

'android.graphics.Bitmap'是一個Android框架類。 JAR和AAR都不會「包含」它。 「包含Bitmap類」是什麼意思? – CommonsWare

+0

你可以用自己的類創建你的jar,可以使用'Bitmap'或任何Android API類。 – Aryan

+0

@CommonsWare意味着當我在jar文件和orid studio中的類中使用android.graphics.Bitmap時顯示錯誤:無法解析符號'位圖' –

回答

0

您可以使用自己的類創建自己的.jar,該類可以使用Bitmap或任何Android API類。

這是一個文件BitmapHelper.java

package com.its.test; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

public class BitmapHelper { 

    public static void getBitmap(String fileName){ 
     Bitmap bitmap= BitmapFactory.decodeFile(fileName); 
    } 
} 

Jar文件僅僅是具有壓縮文件數量.class文件,所以BitmapHelper.class文件是在我的.jar

當我們需要使用.jar只是將它加入在構建路徑中進行編譯。

或者在gradle這個項目把你.jarlibs文件夾內,並以這種方式

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
} 

編譯如果在本地/遠程Maven回購或其他任何使用group nameartifactversion你可以編譯部署了.jar那件神器。

The main difference between a Jar and a AAR is that AARs include resources such as layouts, drawables etc. This makes it a lot easier to create self-contained visual components. For example if you have multiple apps that use the same login screen, with Jars you could share classes but not the layout, styles, etc., you still had to duplicate them. With AARs everything is bundled in one neat package.

編輯

Java庫是.jar不同,圖書館是罐子的集合。您可以輕鬆地將.jars整合到android項目中,但如果您想添加Java庫,則需要定義純Java庫和android之間的綁定。

你可以檢查這個答案。

https://stackoverflow.com/a/42099343/3445320