android 简单打jar包
Posted 跳动的米
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 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包的主要内容,如果未能解决你的问题,请参考以下文章