Android Studio 如何打JAR包(修訂版)


AndroidStudio項目打包成jar

 

前言:在eclipse中我們知道如何將一個項目導出為jar包,現在普遍AndroidStuido開發,這里一步一步詳加介紹AS項目打包成jar,jar和arr的使用

在這里先補充一下我在編譯時遇到的問題:

① Android 打包jarUnable to start the daemon process.

    解決辦法:(這個問題百度有答案,這里直接寫出來,免得大家再找)

        找到項目下的gradle.properties文件中org.gradle.jvmargs配置信息,可根據自己電腦內存大小進行配置,4G內存建議‍
        org.gradle.jvmargs=-Xmx600m

報錯:Unsupported major.minor version 52.0 (jar包對不同JDK版本的兼容性問題:

    這個是因為,jdk的版本問題,如果之前用的1.6或者1.7,換成1.8以上的就可以了

   ③  在下面的步驟配置中,build/intermediates/bundles/release/路徑下沒有release文件夾。

    解決辦法:

    接下來按照下面的步驟就可以編譯成功了。

一.作用:為了供其它項目使用,將項目打包成jar

 

.步驟(AndroidStudio):

  1.像平常一個樣新建一個項目(步驟省略)

  2.(在步驟1的基礎上)點擊File-->New-->New Module—>選擇Android Library-->點擊Next(如下圖:)

   定義好Library的工程名:librarydemo(如下圖:)

    創建完成:

    (步驟1建的)項目中會自動引入該Model (app下的build.gradle):(這個可以手動配置,在open moudle 配置依賴也可以

  3.生成jar:

    創建之前我先在librarydemo工程中建一個測試類,方便測試調用:

    在librarydemo工程下的build.gradle中加上:

    做如下操作:

(注:你的as從來沒有生成過jar,那么第一次就會很慢,會去下載一些文件)

    查看jar是否成功生成:

三、使用jar和arr:

   在AndroidStudio中,創建的Model項目下:

  二者區別:

    jar: 只包含了class文件與清單文件 ,不包含資源文件,如圖片等所有res中的文件。

    aar: 包含jar包和資源文件,如圖片等所有res中的文件。

    個人覺得還是用as提供的aar包比較好,編譯完就生成了aar了,不用擔心資源問題,

  1.   aar使用:

    導入aar(jar一樣):

    在app下的build.gradle中添加如下:

    Sync Now一下,看

有資源文件和布局文件

  2.    jar導入:

    導入jar(同arr一樣)-->選中jar-->右鍵-->點擊Add As Library-->點擊Ok

無資源文件

 

有人會想怎么把資源(圖片,布局,string等)打進jar包呢?

例如:jar中有一個activity用到了布局文件和圖片資源,那么怎么辦呢?

解決如下:由於打包出來的jar只有源代碼的.class 文件,不包含資源文件,我們就把jar包中用到的資源放到你使用

該jar的工程里面。然后通過反射即可,這里給出反射類:

復制代碼
public class MResource {
</span><span style="color:rgb(0,0,255);line-height:1.5;">public</span> <span style="color:rgb(0,0,255);line-height:1.5;">static</span> <span style="color:rgb(0,0,255);line-height:1.5;">int</span><span style="line-height:1.5;"> getIdByName(Context context, String className, String resName) {
    String packageName </span>=<span style="line-height:1.5;"> context.getPackageName();
    </span><span style="color:rgb(0,0,255);line-height:1.5;">int</span> id = 0<span style="line-height:1.5;">;
    </span><span style="color:rgb(0,0,255);line-height:1.5;">try</span><span style="line-height:1.5;"> {
        Class r </span>= Class.forName(packageName + ".R"<span style="line-height:1.5;">);
        Class[] classes </span>=<span style="line-height:1.5;"> r.getClasses();
        Class desireClass </span>= <span style="color:rgb(0,0,255);line-height:1.5;">null</span><span style="line-height:1.5;">;
        </span><span style="color:rgb(0,0,255);line-height:1.5;">for</span><span style="line-height:1.5;"> (Class cls : classes) {
            </span><span style="color:rgb(0,0,255);line-height:1.5;">if</span> (cls.getName().split("\\$")[1<span style="line-height:1.5;">].equals(className)) {
                desireClass </span>=<span style="line-height:1.5;"> cls;
                </span><span style="color:rgb(0,0,255);line-height:1.5;">break</span><span style="line-height:1.5;">;
            }
        }
        </span><span style="color:rgb(0,0,255);line-height:1.5;">if</span> (desireClass != <span style="color:rgb(0,0,255);line-height:1.5;">null</span><span style="line-height:1.5;">) {
            id </span>=<span style="line-height:1.5;"> desireClass.getField(resName).getInt(desireClass);
        }
    } </span><span style="color:rgb(0,0,255);line-height:1.5;">catch</span><span style="line-height:1.5;"> (Exception e) {
        e.printStackTrace();
    }
    </span><span style="color:rgb(0,0,255);line-height:1.5;">return</span><span style="line-height:1.5;"> id;
}

}

復制代碼

這里演示項目中打開jar中的activity的實例

這里給出jar中activity的代碼:

復制代碼
public class JarActivity extends AppCompatActivity {

@Override
</span><span style="color:rgb(0,0,255);line-height:1.5;">protected</span> <span style="color:rgb(0,0,255);line-height:1.5;">void</span><span style="line-height:1.5;"> onCreate(@Nullable Bundle savedInstanceState) {
    </span><span style="color:rgb(0,0,255);line-height:1.5;">super</span><span style="line-height:1.5;">.onCreate(savedInstanceState);
    setContentView(MResource.getIdByName(</span><span style="color:rgb(0,0,255);line-height:1.5;">this</span>, "layout", "jar_layout"<span style="line-height:1.5;">));
    ImageView mPlayerLogo </span>= (ImageView) <span style="color:rgb(0,0,255);line-height:1.5;">this</span>.findViewById(MResource.getIdByName(<span style="color:rgb(0,0,255);line-height:1.5;">this</span><span style="line-height:1.5;">,
            </span>"id", "logo"<span style="line-height:1.5;">));
    mPlayerLogo.setImageResource(MResource.getIdByName(</span><span style="color:rgb(0,0,255);line-height:1.5;">this</span>, "drawable", "ic_launcher"<span style="line-height:1.5;">));
}

}

復制代碼

這里:利用反射根據資源名字獲取資源ID加載布局+設置圖片

在項目中打開jar的JarActivity的實現代碼如下:

注:由於jar沒有布局文件和資源文件,所以需要把布局(jar_layout)copy到項目中,aar則不需要

復制代碼
public class MainActivity extends AppCompatActivity {

@Override
</span><span style="color:rgb(0,0,255);line-height:1.5;">protected</span> <span style="color:rgb(0,0,255);line-height:1.5;">void</span><span style="line-height:1.5;"> onCreate(Bundle savedInstanceState) {
    </span><span style="color:rgb(0,0,255);line-height:1.5;">super</span><span style="line-height:1.5;">.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.btn_jar).setOnClickListener(</span><span style="color:rgb(0,0,255);line-height:1.5;">new</span><span style="line-height:1.5;"> View.OnClickListener() {
        @Override
        </span><span style="color:rgb(0,0,255);line-height:1.5;">public</span> <span style="color:rgb(0,0,255);line-height:1.5;">void</span><span style="line-height:1.5;"> onClick(View v) {
            Intent intent </span>= <span style="color:rgb(0,0,255);line-height:1.5;">new</span><span style="line-height:1.5;"> Intent();
            intent.setClassName(getApplication(), </span>"com.zhh.librarydemo.JarActivity"<span style="line-height:1.5;">);
            startActivity(intent);
        }
    });
}

}

復制代碼

jar_layout布局:

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="vertical">

&lt;<span style="line-height:1.5;">ImageView
    android:id</span>="@+id/logo"<span style="line-height:1.5;">
    android:layout_width</span>="wrap_content"<span style="line-height:1.5;">
    android:layout_height</span>="wrap_content" /&gt;

</LinearLayout>

復制代碼

在清單文件中注冊:

 <activity android:name="com.zhh.librarydemo.JarActivity"/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM