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"/>

 

Android Studio 如何打JAR包(修订版)的更多相关文章

  1. Android Studio 如何打JAR包

    Android Studio 如何打JAR包 在eclipse中我们知道如何将一个项目导出为jar包,供其它项目使用.  在AS中可以通过修改gradle才处理.  我们新建一个项目MakeJar,在 ...

  2. Android Studio下导出jar包和aar包

    Android Studio下导出jar包和aar包 jar包和aar包的区别 步骤 1. 创建Android工程 创建工程比较简单,不错复述 2. 创建一个Library(Module) 创建了一个 ...

  3. Android Studio导入第三方jar包或依赖工程的方法

    Android Studio导入第三方jar包或依赖工程的方法   一 导入jar包的方法 1.打开自己的工程,将需要导入的jar包copy到libs文件夹下 2.在导入的jar包处单击菜单 Add ...

  4. Android Studio 怎样打JAR包

    Android Studio 怎样打JAR包 在eclipse中我们知道怎样将一个项目导出为jar包,供其他项目使用. 在AS中能够通过改动gradle才处理. 我们新建一个项目MakeJar,在项目 ...

  5. Android Studio 导入系统 jar包

    1.当前需要导入系统jar包的module所对应build.gradle中添加如下依赖: provided files('libs/classes-full-debug.jar') 也可以图形化设置: ...

  6. (转载) android studio library生成jar包和aar的方法总结

    android studio library生成jar包和aar的方法总结 标签: android学习文档jar和aar的使用与生成gradle 2016-11-25 10:39 1782人阅读 评论 ...

  7. Android Studio下打jar包

    在我们使用Eclipse时,我们常常使用的第三方类库文件大多都是jar包形式,用起来很方便.但是jar包只能打包class文件,对于Android UI类库而言,我们常常需要打包资源文件,对于界面不多 ...

  8. Eclipse / android studio 添加第三方jar包 步骤

    eclipse 将第三方包放到libs文件夹后并没有引用. 基本步骤分为3步,具体介绍如下: 打开自己的Eclipse,在自己的Android工程上名上右键->Build Path ->C ...

  9. 关于Android Studio中第三方jar包的Javadoc绑定

    原文地址:http://blog.csdn.net/a739697044/article/details/28116189   现在刚开始从Eclipse转用Android Studio,现在在尝试使 ...

随机推荐

  1. 阿里云部署Docker(3)----指令学习

    通过上两节的学习http://blog.csdn.net/minimicall/article/details/40119177 和http://blog.csdn.net/minimicall/ar ...

  2. 关于python中矩阵的实现和矩阵的转置

    python中矩阵的实现是靠序列,,, 序列有很多形式, 其实矩阵是现实生活中的东西,把现实生活中的结构转换到程序中. 就需要有个实现的方法,而这种路径是多种多样的. 下面给出一个把矩阵转换成pyth ...

  3. JS学习笔记 - 面向对象

    类.对象类:模子对象:产品(成品) 蛋糕(对象) 模子(类) Array 类 arr 对象 Array.push(); 错 arr.push(); 对 new arr(); 错 原型prototype ...

  4. Day3:集合

    一.集合的定义及特性 1.集合的特性 1.1   去重,把一个列表变成集合,就自动去重了 1.2   关系测试,测试两组数据之间的交集.差集等关系 #!/usr/bin/env python # -* ...

  5. report_timing

    report_timing   -max_path   2 会报告两条路径,但不一定是最差的路径 report_timing  -nworst  2  -max_path   2 会报告两条最差的路径

  6. StackExchange.Redis 官方文档(五) Keys, Values and Channels

    原文:StackExchange.Redis 官方文档(五) Keys, Values and Channels Keys, Values and Channels 在使用redis的过程中,要注意到 ...

  7. 管理aix的密码策略

    aix 中 /etc/security/user 存放用户的概要 常用参数参数如下 1.account_locked      defines whether the account is locke ...

  8. 小米R2D samba共享配置

    编辑samba配置文件 vi /etc/config/samba 需要注意的是,samba有自己的配置文件 /etc/samba/smb.conf,但是修改这个文件是不生效的,这个配置文件会在重启路由 ...

  9. php课程 8-32 如何使用gd库进行图片裁剪和缩放

    php课程 8-32 如何使用gd库进行图片裁剪和缩放 一.总结 一句话总结:图片缩放到图片裁剪就是改变原图截取的位置以及截取的宽高. 1.电商网站那么多的图片,如果全部加载卡得慢的很,所以他们是怎么 ...

  10. iOS_02_什么是ios开发

    什么是ios开发? * 已知:ios是iphone,ipad等手持设备操作系统. * ios开发就是开发运行在ios系统上的应用或者游戏软件,比如手机QQ,微博或者游戏,说白了,就是开发手机软件:当然 ...