首页 > 代码库 > android 简单打jar包
android 简单打jar包
先建议一个moduel,先写一个下载图片代码:
public class LoadTest extends AsyncTask<Void,Void,byte[]>{
public static LoadTest loadtest;
private byte[] data;
private String url;
public setDataL listence;
public void setDataListence(setDataL l){
this.listence = l;
}
public byte[] getload(){
return data;
}
public void setUrl(String s){
this.url = s;
}
@Override
protected byte[] doInBackground(Void... params) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
URL urlimage = new URL(url);
HttpURLConnection urlconn = (HttpURLConnection) urlimage.openConnection();
urlconn.setConnectTimeout(5000);
urlconn.setRequestMethod("GET");
int sumLenght = urlconn.getContentLength();
InputStream errorStream = urlconn.getErrorStream();
int code = urlconn.getResponseCode();
if (HttpURLConnection.HTTP_OK == code){
InputStream is = urlconn.getInputStream();
byte[] buff = new byte[512];
int len;
while ((len = is.read(buff))!= -1){
outputStream.write(buff,0,len);
}
is.close();
outputStream.flush();
data = http://www.mamicode.com/outputStream.toByteArray();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(byte[] datainfo) {
if (null != datainfo){
listence.getData(datainfo);
}else {
super.onPostExecute(datainfo);
}
}
public interface setDataL{
void getData(byte[] data);
}
}
打开moduel的gradle:
写入代码:
task makeJar(type: Copy) {
delete ‘build/libs/myjar.jar‘
from(‘build/intermediates/bundles/release/‘)
into(‘build/libs/‘)
include(‘classes.jar‘)
rename(‘classes.jar‘, ‘myjar.jar‘)
}
// 在终端执行生成JAR包
// ./gradlew build
//build.finalizedBy makeJar
// 或者
makeJar.dependsOn(build)
// ./gradlew makeJar
打开终端机Terminal输入命令:gradview makeJar
生成的jar在\build\intermediates\bundles\release目录下。可以看到一个java标志如图:
这样就可以啦,导入其他的项目几可以使用了
demo下载地址:https://github.com/tiaodong/OkHttpTest
测试demo:https://github.com/tiaodong/JarTest
android 简单打jar包
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。