|
1 |
| -import java.util.jar.JarEntry |
2 |
| -import java.util.jar.JarOutputStream |
3 |
| -import java.util.zip.Deflater |
4 | 1 |
|
5 | 2 | apply plugin: 'com.android.library'
|
6 | 3 |
|
@@ -53,81 +50,36 @@ dependencies {
|
53 | 50 | compile fileTree(dir: 'libs', include: ['*.jar'])
|
54 | 51 | }
|
55 | 52 |
|
56 |
| -def manifestFile = android.sourceSets.main.manifest.srcFile |
57 |
| - |
58 |
| -def packageName = new XmlParser().parse(manifestFile).attribute('package') |
59 |
| - |
60 |
| -//定义一个生成Jar的t方法 |
61 |
| -def jarTask(String outputJarPath, String packageNamePath) { |
62 |
| - |
63 |
| - final String buildDirPath = buildDir.absolutePath; |
64 |
| - final String intermediate = 'intermediates' + File.separator + 'classes' + File.separator + 'release' + File.separator; |
65 |
| - final String filter1 = 'R.class'; |
66 |
| - final String filter2 = 'R$'; |
67 |
| - |
68 |
| - println 'jarTask... ' + buildDirPath |
69 |
| - |
70 |
| - try { |
71 |
| - |
72 |
| - File outputJar = new File(outputJarPath); |
73 |
| - if (outputJar.exists()) { |
74 |
| - outputJar.delete(); |
75 |
| - } |
76 |
| - |
77 |
| - JarOutputStream jos = new JarOutputStream(new FileOutputStream(outputJar)); |
78 |
| - jos.setLevel(Deflater.BEST_COMPRESSION); |
79 |
| - BufferedInputStream bis = null; |
80 |
| - byte[] cache = new byte[1024]; |
81 |
| - |
82 |
| - File[] file = new File(buildDirPath, intermediate + packageNamePath).listFiles( |
83 |
| - new FileFilter() { |
84 |
| - @Override |
85 |
| - public boolean accept(File pathname) { |
86 |
| - if (filter1.equals(pathname.getName()) || pathname.getName().startsWith(filter2)) { |
87 |
| - return true; |
88 |
| - } |
89 |
| - return false; |
90 |
| - } |
91 |
| - }); |
92 |
| - |
93 |
| - for(int i=0; i< file.length; i++) { |
94 |
| - bis = new BufferedInputStream(new FileInputStream(file[i]), 1024); |
95 |
| - println file[i].getAbsolutePath().replace(buildDirPath + File.separator + intermediate, "") |
96 |
| - jos.putNextEntry(new JarEntry(file[i].getAbsolutePath().replace(buildDirPath + File.separator + intermediate, ""))); |
97 |
| - int count; |
98 |
| - while((count = bis.read(cache, 0, 1024)) != -1) { |
99 |
| - jos.write(cache, 0, count); |
100 |
| - } |
101 |
| - jos.closeEntry(); |
102 |
| - bis.close(); |
103 |
| - } |
104 |
| - |
105 |
| - jos.flush(); |
106 |
| - jos.close(); |
107 |
| - |
108 |
| - } catch(Exception ex) { |
109 |
| - ex.printStackTrace(); |
110 |
| - } |
111 |
| -} |
112 |
| - |
113 | 53 | build.doLast {
|
114 | 54 |
|
115 | 55 | //测试自定义task, 观察编译log里面是否有输出
|
116 | 56 | helloTask.execute()
|
117 | 57 |
|
118 |
| - //生成R的jar包 |
119 |
| - jarTask(buildDir.absolutePath + File.separator + 'outputs' + File.separator + 'rClasses.jar', packageName.replace('.', File.separator)) |
| 58 | + //导出R的Jar包 |
| 59 | + exportJar.execute() |
| 60 | + |
| 61 | +} |
120 | 62 |
|
| 63 | +task exportJar(type: Jar) { |
| 64 | + //指定生成的jar名 |
| 65 | + baseName 'rClasses' |
| 66 | + from buildDir.absolutePath + '/intermediates/classes/debug/' |
| 67 | + include '**/R.class' |
| 68 | + include '**/R$*.class' |
| 69 | + //打包到jar后的目录结构 |
| 70 | + //into("com/xxx/xxx") |
| 71 | + destinationDir = file(buildDir.absolutePath + '/outputs/') |
121 | 72 | }
|
122 | 73 |
|
| 74 | + |
123 | 75 | // 自定义task的用法
|
124 | 76 | task helloTask(type: HelloGradleTask) {
|
125 |
| - helloStr = 'hello BBB from greeting' |
| 77 | + helloStr = 'hello from ovrride greeting ' + android.sourceSets.main.manifest.srcFile |
126 | 78 | }
|
127 | 79 | // 自定义task的用法
|
128 | 80 | class HelloGradleTask extends DefaultTask {
|
129 | 81 |
|
130 |
| - def String helloStr = 'hello AAA from HelloGradleTask' |
| 82 | + def String helloStr = 'hello from Default HelloGradleTask ' |
131 | 83 |
|
132 | 84 | @TaskAction
|
133 | 85 | def hello() {
|
|
0 commit comments