Android Studio 生成自定义jar包的步骤详解
作者:紫依卓兰 时间:2023-01-31 05:02:41
想要将一个项目导出为jar包,供其它项目使用,在eclipse中可以直接导出该项目为jar包,而 在AS中可以通过修改gradle才处理。
接下来就介绍下具体的步骤:
1、新建一个项目,项目名随意,eg:MakeJarApplication,在项目中新建一个module类型为android-library ,命名为testLibrary。如图:
项目结构图
2、让app依赖这个库,在app下的build.gradle文件中添加compile project(':testlibrary')
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
compile project(':testlibrary')
}
3、在testlibrary中创建一个TestShow类让外部的app能调用。并在app中调用
(1)TestShow .java
public class TestShow {
public void show(Context context, String msg, TextView textView) {
textView.setText(msg);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
public void test() {
System.out.println("测试信息,test()方法");
}
}
(2)、MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show_tv = ((TextView) findViewById(R.id.show_tv));
((Button) findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TestShow().show(MainActivity.this, "调用jar包中的show方法", show_tv);
}
});
}
4、testlibrary的build.gradle中配置能生成jar文件:
def SDK_BASENAME = "TestSdk";
def SDK_VERSION = "_V1.0";
def sdkDestinationPath = "build";
def zipFile = file('build/intermediates/bundles/default/classes.jar')
task deleteBuild(type: Delete) {
delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
}
task makeJar(type: Jar) {
from zipTree(zipFile)
from fileTree(dir: 'src/main',includes: ['assets/**'])//将assets目录打入jar包
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)
}
makeJar.dependsOn(deleteBuild, build)
其中SDK_BASENAME = "TestSdk"; SDK_VERSION = "_V1.0";
是定义生成jar的名字为TestSdk_V1.0.jar。
5、在右侧的Gradle目录中有个“other”,展开找到“makejar”,然后双击“makejar”,就可以生成jar文件了,生成结果如下:
步骤结果导图
现在就可以复制TestSdk.jar到一个新的module中的libs目录下,然后就可以调用 new TestShow().show(context, "", show_tv);方法了。
6、在项目中调用结果:
总结
以上所述是小编给大家介绍的Android Studio 生成自定义jar包的步骤详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:https://www.jianshu.com/p/c6fabaca33e0
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
完整的医院就诊挂号系统基于Spring MVC + Spring + MyBatis实现
![](https://img.aspxhome.com/file/2023/0/61400_0s.png)
Java中的泛型详解
常用Hash算法(C语言的简单实现)
一文精通Java中的volatile关键字
![](https://img.aspxhome.com/file/2023/2/59572_0s.png)
浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存
C#词法分析器之构造NFA详解
![](https://img.aspxhome.com/file/2023/3/116623_0s.png)
如何用C#实现SAGA分布式事务
![](https://img.aspxhome.com/file/2023/2/67552_0s.png)
Java多线程Thread基础学习
![](https://img.aspxhome.com/file/2023/8/67688_0s.png)
spring定时任务执行两次及tomcat部署缓慢问题的解决方法
IDEA 如何控制编辑左侧的功能图标ICON(操作步骤)
![](https://img.aspxhome.com/file/2023/8/128938_0s.jpg)
Android简单获取经纬度的方法
ListView用法中与滚动相关的需求实现
![](https://img.aspxhome.com/file/2023/1/100091_0s.png)
浅谈c++11线程的互斥量
![](https://img.aspxhome.com/file/2023/6/116586_0s.png)
FloatingActionButton增强版一个按钮跳出多个按钮第三方开源之FloatingActionButton
![](https://img.aspxhome.com/file/2023/9/130379_0s.gif)
ListView实现下拉动态渲染数据
![](https://img.aspxhome.com/file/2023/8/137918_0s.gif)
java中使用zxing批量生成二维码立牌
![](https://img.aspxhome.com/file/2023/1/64371_0s.png)
基于<aop:aspect>与<aop:advisor>的区别
Java操作pdf的工具类itext的处理方法
Java从JDK源码角度对Object进行实例分析
IDEA快速搭建spring boot项目教程(Spring initializr)
![](https://img.aspxhome.com/file/2023/0/58060_0s.png)