diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 05:11:39 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 05:11:39 +0000 |
commit | 67386d6c468c80a616de4d146a0e98b860f6ef19 (patch) | |
tree | b87e743b5021b2215ec626c84de22e9822be7883 | |
parent | e2e0c63fa20701fbbd39a4d3058762ba465fad00 (diff) | |
parent | d72f92961db54c1639acc8fd593155b0490f9f39 (diff) | |
download | jarjar-android14-mainline-uwb-release.tar.gz |
Snap for 10453563 from d72f92961db54c1639acc8fd593155b0490f9f39 to mainline-uwb-releaseaml_uwb_341513070aml_uwb_341511050aml_uwb_341310300aml_uwb_341310030aml_uwb_341111010aml_uwb_341011000android14-mainline-uwb-release
Change-Id: I18e14b54629036c038550a04c7021da1e562cf13
-rw-r--r-- | Android.bp | 4 | ||||
l--------- | LICENSE | 1 | ||||
-rw-r--r-- | METADATA | 11 | ||||
-rw-r--r-- | res/com/tonicsystems/jarjar/help.txt | 4 | ||||
-rw-r--r-- | src/android/com/android/jarjar/RemoveAndroidCompatAnnotationsJarTransformer.java | 8 | ||||
-rw-r--r-- | src/android/com/android/jarjar/StripAnnotation.java | 26 | ||||
-rw-r--r-- | src/android/com/android/jarjar/StripAnnotationsJarTransformer.java | 121 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/EmptyClassVisitor.java | 10 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/MainProcessor.java | 14 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/RulesFileParser.java | 6 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/StringReader.java | 8 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/util/GetNameClassWriter.java | 2 |
12 files changed, 200 insertions, 15 deletions
@@ -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: [ @@ -0,0 +1 @@ +LICENSE.txt
\ No newline at end of file @@ -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) { |