summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:02:51 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-07-07 05:02:51 +0000
commit69cba3dac188fd2d9be1f5618c254f777e58f8ee (patch)
treeb87e743b5021b2215ec626c84de22e9822be7883
parent9f544a53372f1116ccba859be84366b2ab41750b (diff)
parentd72f92961db54c1639acc8fd593155b0490f9f39 (diff)
downloadjarjar-android14-mainline-permission-release.tar.gz
Change-Id: I2fcdeead0aaf9f2ac6942b6ad0928c9c12c41ecd
-rw-r--r--Android.bp4
l---------LICENSE1
-rw-r--r--METADATA11
-rw-r--r--res/com/tonicsystems/jarjar/help.txt4
-rw-r--r--src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java8
-rw-r--r--src/android/com/android/jarjar/StripAnnotation.java26
-rw-r--r--src/android/com/android/jarjar/StripAnnotationsJarTransformer.java121
-rw-r--r--src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java10
-rw-r--r--src/main/com/tonicsystems/jarjar/MainProcessor.java14
-rw-r--r--src/main/com/tonicsystems/jarjar/RulesFileParser.java6
-rw-r--r--src/main/com/tonicsystems/jarjar/StringReader.java8
-rw-r--r--src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java2
12 files changed, 200 insertions, 15 deletions
diff --git a/Android.bp b/Android.bp
index be83e4b..b75f82b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -47,8 +47,8 @@ java_library_host {
java_resource_dirs: ["res"],
static_libs: [
- "asm-9.2",
- "asm-commons-9.2",
+ "ow2-asm",
+ "ow2-asm-commons",
],
libs: [
diff --git a/LICENSE b/LICENSE
new file mode 120000
index 0000000..85de3d4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1 @@
+LICENSE.txt \ No newline at end of file
diff --git a/METADATA b/METADATA
index d97975c..11534d0 100644
--- a/METADATA
+++ b/METADATA
@@ -1,3 +1,14 @@
+name: "jarjar"
+description:
+ "Utility that makes it easy to repackage Java libraries and embed them into "
+ "your own distribution"
+
third_party {
+ url {
+ type: GIT
+ value: "https://github.com/google/jarjar.git"
+ }
+ version: "1.4"
+ last_upgrade_date { year: 2014 month: 6 day: 17 }
license_type: NOTICE
}
diff --git a/res/com/tonicsystems/jarjar/help.txt b/res/com/tonicsystems/jarjar/help.txt
index 8410909..6f14db6 100644
--- a/res/com/tonicsystems/jarjar/help.txt
+++ b/res/com/tonicsystems/jarjar/help.txt
@@ -49,6 +49,7 @@ Rules file format:
rule <pattern> <result>
zap <pattern>
keep <pattern>
+ strip-annotations <annotation>
The standard rule ("rule") is used to rename classes. All references
to the renamed classes will also be updated. If a class name is
@@ -73,3 +74,6 @@ Rules file format:
via dependency analysis are discarded when writing the output
jar. This is the last step in the process, after renaming and zapping.
+ The "strip-annotations" rule will remove all the references to a certain
+ annotation from a jar file. As no pattern matching is performed, the
+ annotations have to be provided as one per line.
diff --git a/src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java b/src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java
index f9e5e14..052e358 100644
--- a/src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java
+++ b/src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java
@@ -33,6 +33,8 @@ import java.util.function.Supplier;
*/
public final class RemoveAndroidCompatAnnotationsJarTransformer extends JarTransformer {
+ private static int ASM_VERSION = Opcodes.ASM9;
+
private static final Set<String> REMOVE_ANNOTATIONS = Set.of(
"Landroid/compat/annotation/UnsupportedAppUsage;");
@@ -51,7 +53,7 @@ public final class RemoveAndroidCompatAnnotationsJarTransformer extends JarTrans
private boolean isClassRemapped;
AnnotationRemover(ClassVisitor cv) {
- super(Opcodes.ASM7, cv);
+ super(ASM_VERSION, cv);
}
@Override
@@ -74,7 +76,7 @@ public final class RemoveAndroidCompatAnnotationsJarTransformer extends JarTrans
Object value) {
FieldVisitor superVisitor =
super.visitField(access, name, descriptor, signature, value);
- return new FieldVisitor(Opcodes.ASM7, superVisitor) {
+ return new FieldVisitor(ASM_VERSION, superVisitor) {
@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
return visitAnnotationCommon(descriptor,
@@ -89,7 +91,7 @@ public final class RemoveAndroidCompatAnnotationsJarTransformer extends JarTrans
String signature, String[] exceptions) {
MethodVisitor superVisitor =
super.visitMethod(access, name, descriptor, signature, exceptions);
- return new MethodVisitor(Opcodes.ASM7, superVisitor) {
+ return new MethodVisitor(ASM_VERSION, superVisitor) {
@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
return visitAnnotationCommon(descriptor,
diff --git a/src/android/com/android/jarjar/StripAnnotation.java b/src/android/com/android/jarjar/StripAnnotation.java
new file mode 100644
index 0000000..debd469
--- /dev/null
+++ b/src/android/com/android/jarjar/StripAnnotation.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.jarjar;
+
+import com.tonicsystems.jarjar.PatternElement;
+
+/**
+ * Configuration element for stripping annotations in a jar file.
+ */
+public class StripAnnotation extends PatternElement
+{
+}
diff --git a/src/android/com/android/jarjar/StripAnnotationsJarTransformer.java b/src/android/com/android/jarjar/StripAnnotationsJarTransformer.java
new file mode 100644
index 0000000..d547fac
--- /dev/null
+++ b/src/android/com/android/jarjar/StripAnnotationsJarTransformer.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.jarjar;
+
+import com.tonicsystems.jarjar.util.JarTransformer;
+
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+/**
+ * A transformer that strips annotations from all classes based on custom rules.
+ */
+public final class StripAnnotationsJarTransformer extends JarTransformer {
+
+ private static int ASM_VERSION = Opcodes.ASM9;
+
+ private final List<String> stripAnnotationList;
+
+ public StripAnnotationsJarTransformer(List<StripAnnotation> stripAnnotationList) {
+ this.stripAnnotationList = getAnnotationList(stripAnnotationList);
+ }
+
+ private static List<String> getAnnotationList(List<StripAnnotation> stripAnnotationList) {
+ return stripAnnotationList.stream().map(el -> getClassName(el)).collect(Collectors.toList());
+ }
+
+ private static String getClassName(StripAnnotation element) {
+ return "L" + element.getPattern().replace('.', '/') + ";";
+ }
+
+ @Override
+ protected ClassVisitor transform(ClassVisitor classVisitor) {
+ return new AnnotationRemover(classVisitor);
+ }
+
+ private class AnnotationRemover extends ClassVisitor {
+
+ AnnotationRemover(ClassVisitor cv) {
+ super(ASM_VERSION, cv);
+ }
+
+ @Override
+ public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
+ return visitAnnotationCommon(descriptor,
+ () -> super.visitAnnotation(descriptor, visible));
+ }
+
+ @Override
+ public FieldVisitor visitField(int access, String name, String descriptor, String signature,
+ Object value) {
+ FieldVisitor superVisitor =
+ super.visitField(access, name, descriptor, signature, value);
+ return new FieldVisitor(ASM_VERSION, superVisitor) {
+ @Override
+ public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
+ return visitAnnotationCommon(descriptor,
+ () -> super.visitAnnotation(descriptor, visible));
+
+ }
+ };
+ }
+
+ @Override
+ public MethodVisitor visitMethod(int access, String name, String descriptor,
+ String signature, String[] exceptions) {
+ MethodVisitor superVisitor =
+ super.visitMethod(access, name, descriptor, signature, exceptions);
+ return new MethodVisitor(ASM_VERSION, superVisitor) {
+ @Override
+ public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
+ return visitAnnotationCommon(descriptor,
+ () -> super.visitAnnotation(descriptor, visible));
+ }
+
+ @Override
+ public AnnotationVisitor visitParameterAnnotation(int parameter,
+ String descriptor, boolean visible) {
+ return visitAnnotationCommon(descriptor,
+ () -> super.visitParameterAnnotation(parameter, descriptor, visible));
+ }
+ };
+ }
+
+ /**
+ * Create an {@link AnnotationVisitor} that removes any annotations from {@link
+ * #stripAnnotationList}.
+ */
+ private AnnotationVisitor visitAnnotationCommon(String annotation,
+ Supplier<AnnotationVisitor> defaultVisitorSupplier) {
+ if (stripAnnotationList.contains(annotation)) {
+ return null;
+ }
+ // Only get() the default AnnotationVisitor if the annotation is to be included.
+ // Invoking super.visitAnnotation(descriptor, visible) causes the annotation to be
+ // included in the output even if the resulting AnnotationVisitor is not returned or
+ // used.
+ return defaultVisitorSupplier.get();
+ }
+ }
+}
diff --git a/src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java b/src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java
index c7614a1..9a05516 100644
--- a/src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java
+++ b/src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java
@@ -22,29 +22,29 @@ import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
/**
- * An ASM3 EmptyVisitor replacement
+ * An EmptyVisitor replacement
* @author <a href="mailto:blackdrag@gmx.org">Jochen "blackdrag" Theodorou</a>
*/
public class EmptyClassVisitor extends ClassVisitor {
public EmptyClassVisitor() {
- super(Opcodes.ASM7);
+ super(Opcodes.ASM9);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
- return new MethodVisitor(Opcodes.ASM7) {};
+ return new MethodVisitor(Opcodes.ASM9) {};
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
- return new AnnotationVisitor(Opcodes.ASM7) {};
+ return new AnnotationVisitor(Opcodes.ASM9) {};
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
- return new FieldVisitor(Opcodes.ASM7) {};
+ return new FieldVisitor(Opcodes.ASM9) {};
}
}
diff --git a/src/main/com/tonicsystems/jarjar/MainProcessor.java b/src/main/com/tonicsystems/jarjar/MainProcessor.java
index 2778cc5..a0ef4c7 100644
--- a/src/main/com/tonicsystems/jarjar/MainProcessor.java
+++ b/src/main/com/tonicsystems/jarjar/MainProcessor.java
@@ -16,12 +16,14 @@
package com.tonicsystems.jarjar;
+import com.android.jarjar.StripAnnotation;
import com.tonicsystems.jarjar.util.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import com.android.jarjar.RemoveAndroidCompatAnnotationsJarTransformer;
+import com.android.jarjar.StripAnnotationsJarTransformer;
class MainProcessor implements JarProcessor
{
@@ -42,6 +44,9 @@ class MainProcessor implements JarProcessor
List<Zap> zapList = new ArrayList<Zap>();
List<Rule> ruleList = new ArrayList<Rule>();
List<Keep> keepList = new ArrayList<Keep>();
+ // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
+ List<StripAnnotation> stripAnnotationList = new ArrayList<StripAnnotation>();
+ // ANDROID-END: b/222743634 Strip annotations from system module stubs
for (PatternElement pattern : patterns) {
if (pattern instanceof Zap) {
zapList.add((Zap) pattern);
@@ -49,7 +54,11 @@ class MainProcessor implements JarProcessor
ruleList.add((Rule) pattern);
} else if (pattern instanceof Keep) {
keepList.add((Keep) pattern);
+ // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
+ } else if (pattern instanceof StripAnnotation) {
+ stripAnnotationList.add((StripAnnotation) pattern);
}
+ // ANDROID-END: b/222743634 Strip annotations from system module stubs
}
PackageRemapper pr = new PackageRemapper(ruleList, verbose);
@@ -65,6 +74,11 @@ class MainProcessor implements JarProcessor
if (removeAndroidCompatAnnotations)
processors.add(new RemoveAndroidCompatAnnotationsJarTransformer(pr));
// ANDROID-END: b/146418363 Add an Android-specific transformer to strip compat annotation
+ // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
+ if (!stripAnnotationList.isEmpty()) {
+ processors.add(new StripAnnotationsJarTransformer(stripAnnotationList));
+ }
+ // ANDROID-END: b/222743634 Strip annotations from system module stubs
processors.add(new JarTransformerChain(new RemappingClassTransformer[]{ new RemappingClassTransformer(pr) }));
processors.add(new ResourceProcessor(pr));
chain = new JarProcessorChain(processors.toArray(new JarProcessor[processors.size()]));
diff --git a/src/main/com/tonicsystems/jarjar/RulesFileParser.java b/src/main/com/tonicsystems/jarjar/RulesFileParser.java
index f54f3b9..c8c6ea4 100644
--- a/src/main/com/tonicsystems/jarjar/RulesFileParser.java
+++ b/src/main/com/tonicsystems/jarjar/RulesFileParser.java
@@ -16,6 +16,8 @@
package com.tonicsystems.jarjar;
+import com.android.jarjar.StripAnnotation;
+
import java.io.*;
import java.util.*;
@@ -62,6 +64,10 @@ class RulesFileParser
element = new Zap();
} else if (type.equals("keep")) {
element = new Keep();
+ // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
+ } else if (type.equals("strip-annotation")) {
+ element = new StripAnnotation();
+ // ANDROID-END: b/222743634 Strip annotations from system module stubs
} else {
error(c, parts);
}
diff --git a/src/main/com/tonicsystems/jarjar/StringReader.java b/src/main/com/tonicsystems/jarjar/StringReader.java
index 164b0a2..c3cc273 100644
--- a/src/main/com/tonicsystems/jarjar/StringReader.java
+++ b/src/main/com/tonicsystems/jarjar/StringReader.java
@@ -24,7 +24,7 @@ abstract class StringReader extends ClassVisitor
private String className;
public StringReader() {
- super(Opcodes.ASM7);
+ super(Opcodes.ASM9);
}
abstract public void visitString(String className, String value, int line);
@@ -42,7 +42,7 @@ abstract class StringReader extends ClassVisitor
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
handleObject(value);
- return new FieldVisitor(Opcodes.ASM7){
+ return new FieldVisitor(Opcodes.ASM9){
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return StringReader.this.visitAnnotation(desc, visible);
@@ -52,7 +52,7 @@ abstract class StringReader extends ClassVisitor
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
- return new AnnotationVisitor(Opcodes.ASM7) {
+ return new AnnotationVisitor(Opcodes.ASM9) {
@Override
public void visit(String name, Object value) {
handleObject(value);
@@ -71,7 +71,7 @@ abstract class StringReader extends ClassVisitor
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
- MethodVisitor mv = new MethodVisitor(Opcodes.ASM7){
+ MethodVisitor mv = new MethodVisitor(Opcodes.ASM9){
@Override
public void visitLdcInsn(Object cst) {
handleObject(cst);
diff --git a/src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java b/src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java
index db6ec4f..cd2cc9b 100644
--- a/src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java
+++ b/src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java
@@ -25,7 +25,7 @@ public class GetNameClassWriter extends ClassVisitor
private String className;
public GetNameClassWriter(int flags) {
- super(Opcodes.ASM7,new ClassWriter(flags));
+ super(Opcodes.ASM9,new ClassWriter(flags));
}
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {