├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── ViewOptAnnotation ├── build.gradle ├── build │ ├── .transforms │ │ ├── 04729f32573dc21985ccd9e1601f45a3.bin │ │ ├── 04729f32573dc21985ccd9e1601f45a3 │ │ │ └── ViewOptAnnotation │ │ │ │ └── classes.dex │ │ ├── 2a0f8de15f7f0cf782dff8a264d601a6.bin │ │ └── 2da72a2ce8fff911ef15a5c207d59eb3.bin │ ├── classes │ │ └── java │ │ │ └── main │ │ │ └── com │ │ │ └── zhy │ │ │ └── demo │ │ │ └── viewopt │ │ │ └── ViewOptHost.class │ ├── libs │ │ └── ViewOptAnnotation.jar │ └── tmp │ │ └── jar │ │ └── MANIFEST.MF └── src │ └── main │ └── java │ └── com │ └── zhy │ └── demo │ └── viewopt │ └── ViewOptHost.java ├── ViewOptProcessor ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── example │ └── viewoptprocessor │ ├── ClassValidator.java │ ├── ViewCreatorClassGenerator.java │ └── ViewCreatorProcessor.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── testviewopt │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ ├── example │ │ │ └── testviewopt │ │ │ │ ├── BaseActivity.java │ │ │ │ ├── MainActivity.java │ │ │ │ └── view │ │ │ │ ├── MyMainView1.java │ │ │ │ ├── MyMainView2.java │ │ │ │ ├── MyMainView3.java │ │ │ │ └── MyMainView4.java │ │ │ └── zhy │ │ │ └── demo │ │ │ └── viewopt │ │ │ └── ViewOpt.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_main1.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── testviewopt │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lib1 ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── lib1 │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── example │ └── lib1 │ └── ExampleUnitTest.java ├── lib2 ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── lib2 │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── lib2 │ │ │ ├── Lib2Activity.java │ │ │ └── Lib2View.java │ └── res │ │ ├── layout │ │ └── activity_lib2.xml │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── example │ └── lib2 │ └── ExampleUnitTest.java ├── settings.gradle ├── view_opt.gradle └── viewoptapi ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── example │ └── viewoptapi │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── com │ │ └── zhy │ │ └── demo │ │ └── viewopt │ │ └── IViewCreator.java └── res │ └── values │ └── strings.xml └── test └── java └── com └── example └── viewoptapi └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | xmlns:android 11 | 12 | ^$ 13 | 14 | 15 | 16 |
17 |
18 | 19 | 20 | 21 | xmlns:.* 22 | 23 | ^$ 24 | 25 | 26 | BY_NAME 27 | 28 |
29 |
30 | 31 | 32 | 33 | .*:id 34 | 35 | http://schemas.android.com/apk/res/android 36 | 37 | 38 | 39 |
40 |
41 | 42 | 43 | 44 | .*:name 45 | 46 | http://schemas.android.com/apk/res/android 47 | 48 | 49 | 50 |
51 |
52 | 53 | 54 | 55 | name 56 | 57 | ^$ 58 | 59 | 60 | 61 |
62 |
63 | 64 | 65 | 66 | style 67 | 68 | ^$ 69 | 70 | 71 | 72 |
73 |
74 | 75 | 76 | 77 | .* 78 | 79 | ^$ 80 | 81 | 82 | BY_NAME 83 | 84 |
85 |
86 | 87 | 88 | 89 | .* 90 | 91 | http://schemas.android.com/apk/res/android 92 | 93 | 94 | ANDROID_ATTRIBUTE_ORDER 95 | 96 |
97 |
98 | 99 | 100 | 101 | .* 102 | 103 | .* 104 | 105 | 106 | BY_NAME 107 | 108 |
109 |
110 |
111 |
112 |
113 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /ViewOptAnnotation/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | 3 | dependencies { 4 | implementation fileTree(dir: 'libs', include: ['*.jar']) 5 | } 6 | 7 | sourceCompatibility = "7" 8 | targetCompatibility = "7" 9 | -------------------------------------------------------------------------------- /ViewOptAnnotation/build/.transforms/04729f32573dc21985ccd9e1601f45a3.bin: -------------------------------------------------------------------------------- 1 | o/ViewOptAnnotation 2 | -------------------------------------------------------------------------------- /ViewOptAnnotation/build/.transforms/04729f32573dc21985ccd9e1601f45a3/ViewOptAnnotation/classes.dex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/ViewOptAnnotation/build/.transforms/04729f32573dc21985ccd9e1601f45a3/ViewOptAnnotation/classes.dex -------------------------------------------------------------------------------- /ViewOptAnnotation/build/.transforms/2a0f8de15f7f0cf782dff8a264d601a6.bin: -------------------------------------------------------------------------------- 1 | i/ 2 | -------------------------------------------------------------------------------- /ViewOptAnnotation/build/.transforms/2da72a2ce8fff911ef15a5c207d59eb3.bin: -------------------------------------------------------------------------------- 1 | i/ 2 | -------------------------------------------------------------------------------- /ViewOptAnnotation/build/classes/java/main/com/zhy/demo/viewopt/ViewOptHost.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/ViewOptAnnotation/build/classes/java/main/com/zhy/demo/viewopt/ViewOptHost.class -------------------------------------------------------------------------------- /ViewOptAnnotation/build/libs/ViewOptAnnotation.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/ViewOptAnnotation/build/libs/ViewOptAnnotation.jar -------------------------------------------------------------------------------- /ViewOptAnnotation/build/tmp/jar/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | 3 | -------------------------------------------------------------------------------- /ViewOptAnnotation/src/main/java/com/zhy/demo/viewopt/ViewOptHost.java: -------------------------------------------------------------------------------- 1 | package com.zhy.demo.viewopt; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target({ElementType.TYPE}) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface ViewOptHost { 11 | } 12 | -------------------------------------------------------------------------------- /ViewOptProcessor/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /ViewOptProcessor/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | 3 | dependencies { 4 | implementation fileTree(dir: 'libs', include: ['*.jar']) 5 | 6 | implementation 'com.google.auto.service:auto-service:1.0-rc2' 7 | implementation 'com.squareup:javapoet:1.8.0' 8 | annotationProcessor 'com.google.auto.service:auto-service:1.0-rc2' 9 | implementation project(':ViewOptAnnotation') 10 | 11 | } 12 | 13 | sourceCompatibility = "7" 14 | targetCompatibility = "7" 15 | -------------------------------------------------------------------------------- /ViewOptProcessor/src/main/java/com/example/viewoptprocessor/ClassValidator.java: -------------------------------------------------------------------------------- 1 | package com.example.viewoptprocessor; 2 | 3 | import javax.lang.model.element.TypeElement; 4 | 5 | final class ClassValidator { 6 | static String getClassName(TypeElement type, String packageName) { 7 | int packageLen = packageName.length() + 1; 8 | return type.getQualifiedName().toString().substring(packageLen) 9 | .replace('.', '$'); 10 | } 11 | } -------------------------------------------------------------------------------- /ViewOptProcessor/src/main/java/com/example/viewoptprocessor/ViewCreatorClassGenerator.java: -------------------------------------------------------------------------------- 1 | package com.example.viewoptprocessor; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.io.Writer; 8 | import java.util.HashSet; 9 | 10 | import javax.annotation.processing.Messager; 11 | import javax.annotation.processing.ProcessingEnvironment; 12 | import javax.lang.model.element.PackageElement; 13 | import javax.lang.model.element.TypeElement; 14 | import javax.tools.Diagnostic; 15 | import javax.tools.JavaFileObject; 16 | 17 | public class ViewCreatorClassGenerator { 18 | 19 | private TypeElement mTypeElement; 20 | 21 | private String mPackageName; 22 | private String mClassName; 23 | private ProcessingEnvironment mProcessingEnv; 24 | private Messager mMessager; 25 | 26 | private static final String sProxyInterfaceName = "IViewCreator"; 27 | 28 | 29 | public ViewCreatorClassGenerator(ProcessingEnvironment processingEnv, TypeElement classElement, Messager messager) { 30 | mProcessingEnv = processingEnv; 31 | mMessager = messager; 32 | mTypeElement = classElement; 33 | PackageElement packageElement = processingEnv.getElementUtils().getPackageOf(classElement); 34 | String packageName = packageElement.getQualifiedName().toString(); 35 | //classname 36 | String className = ClassValidator.getClassName(classElement, packageName); 37 | 38 | mPackageName = packageName; 39 | mClassName = className + "__ViewCreator__Proxy"; 40 | } 41 | 42 | public void getJavaClassFile() { 43 | 44 | Writer writer = null; 45 | try { 46 | JavaFileObject jfo = mProcessingEnv.getFiler().createSourceFile( 47 | mClassName, 48 | mTypeElement); 49 | 50 | String classPath = jfo.toUri().getPath(); 51 | 52 | String buildDirStr = "/app/build/"; 53 | String buildDirFullPath = classPath.substring(0, classPath.indexOf(buildDirStr) + buildDirStr.length()); 54 | File customViewFile = new File(buildDirFullPath + "tmp_custom_views/custom_view_final.txt"); 55 | 56 | HashSet customViewClassNameSet = new HashSet<>(); 57 | putClassListData(customViewClassNameSet, customViewFile); 58 | 59 | String generateClassInfoStr = generateClassInfoStr(customViewClassNameSet); 60 | 61 | writer = jfo.openWriter(); 62 | writer.write(generateClassInfoStr); 63 | writer.flush(); 64 | 65 | mMessager.printMessage(Diagnostic.Kind.NOTE, "generate file path : " + classPath); 66 | 67 | } catch (Exception e) { 68 | e.printStackTrace(); 69 | } finally { 70 | if (writer != null) { 71 | try { 72 | writer.close(); 73 | } catch (IOException e) { 74 | // ignore 75 | } 76 | } 77 | } 78 | } 79 | 80 | private String generateClassInfoStr(HashSet customViewClassNameSet) { 81 | 82 | StringBuilder builder = new StringBuilder(); 83 | builder.append("// Generated code. Do not modify!\n"); 84 | builder.append("package ").append(mPackageName).append(";\n\n"); 85 | builder.append("import com.zhy.demo.viewopt.*;\n"); 86 | builder.append("import android.content.Context;\n"); 87 | builder.append("import android.util.AttributeSet;\n"); 88 | builder.append("import android.view.*;\n"); 89 | builder.append("import android.widget.*;\n"); 90 | builder.append("import android.webkit.*;\n"); 91 | builder.append("import android.app.*;\n"); 92 | 93 | 94 | 95 | builder.append('\n'); 96 | 97 | builder.append("public class ").append(mClassName).append(" implements " + sProxyInterfaceName); 98 | builder.append(" {\n"); 99 | 100 | generateMethodStr(builder, customViewClassNameSet); 101 | builder.append('\n'); 102 | 103 | builder.append("}\n"); 104 | return builder.toString(); 105 | 106 | } 107 | 108 | private void generateMethodStr(StringBuilder builder, HashSet customViewClassNameSet) { 109 | 110 | builder.append("@Override\n "); 111 | builder.append("public View createView(String name, Context context, AttributeSet attrs ) {\n"); 112 | 113 | 114 | builder.append("switch(name)"); 115 | builder.append("{\n"); // switch start 116 | 117 | for (String className : customViewClassNameSet) { 118 | if (className == null || className.trim().length() == 0) { 119 | continue; 120 | } 121 | builder.append("case \"" + className + "\" :\n"); 122 | builder.append("return new " + className + "(context,attrs);\n"); 123 | } 124 | 125 | builder.append("}\n"); //switch end 126 | 127 | builder.append("return null;\n"); 128 | builder.append(" }\n"); // method end 129 | 130 | } 131 | 132 | private void putClassListData(HashSet customViewClassNameSet, File customViewFile) { 133 | if (customViewFile.exists()) { 134 | FileReader fr = null; 135 | BufferedReader br = null; 136 | try { 137 | fr = new FileReader(customViewFile); 138 | br = new BufferedReader(fr); 139 | String line = null; 140 | while ((line = br.readLine()) != null) { 141 | if (line.trim().length() <= 0) { 142 | continue; 143 | } 144 | customViewClassNameSet.add(line.trim()); 145 | 146 | } 147 | } catch (IOException e) { 148 | e.printStackTrace(); 149 | } finally { 150 | try { 151 | if (br != null) { 152 | br.close(); 153 | } 154 | } catch (IOException e) { 155 | // ignore 156 | } 157 | try { 158 | if (fr != null) { 159 | fr.close(); 160 | } 161 | } catch (IOException e) { 162 | // ignore 163 | } 164 | } 165 | } 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /ViewOptProcessor/src/main/java/com/example/viewoptprocessor/ViewCreatorProcessor.java: -------------------------------------------------------------------------------- 1 | package com.example.viewoptprocessor; 2 | 3 | import com.zhy.demo.viewopt.ViewOptHost; 4 | import com.google.auto.service.AutoService; 5 | 6 | import java.util.LinkedHashSet; 7 | import java.util.Set; 8 | 9 | import javax.annotation.processing.AbstractProcessor; 10 | import javax.annotation.processing.Messager; 11 | import javax.annotation.processing.ProcessingEnvironment; 12 | import javax.annotation.processing.Processor; 13 | import javax.annotation.processing.RoundEnvironment; 14 | import javax.lang.model.element.Element; 15 | import javax.lang.model.element.TypeElement; 16 | 17 | @AutoService(Processor.class) 18 | public class ViewCreatorProcessor extends AbstractProcessor { 19 | 20 | private Messager mMessager; 21 | 22 | 23 | @Override 24 | public synchronized void init(ProcessingEnvironment processingEnvironment) { 25 | super.init(processingEnvironment); 26 | mMessager = processingEnv.getMessager(); 27 | } 28 | 29 | @Override 30 | public boolean process(Set set, RoundEnvironment roundEnvironment) { 31 | 32 | Set classElements = roundEnvironment.getElementsAnnotatedWith(ViewOptHost.class); 33 | 34 | for (Element element : classElements) { 35 | TypeElement classElement = (TypeElement) element; 36 | ViewCreatorClassGenerator viewCreatorClassGenerator = new ViewCreatorClassGenerator(processingEnv, classElement, mMessager); 37 | viewCreatorClassGenerator.getJavaClassFile(); 38 | break; 39 | } 40 | return true; 41 | 42 | } 43 | 44 | @Override 45 | public Set getSupportedAnnotationTypes() { 46 | Set types = new LinkedHashSet<>(); 47 | types.add(ViewOptHost.class.getCanonicalName()); 48 | return types; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | import groovy.util.slurpersupport.GPathResult 2 | import groovy.util.slurpersupport.Node 3 | 4 | apply plugin: 'com.android.application' 5 | 6 | android { 7 | compileSdkVersion 29 8 | buildToolsVersion "29.0.2" 9 | defaultConfig { 10 | applicationId "com.example.testviewopt" 11 | minSdkVersion 15 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'androidx.appcompat:appcompat:1.0.2' 28 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 31 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 32 | implementation project(":ViewOptAnnotation") 33 | annotationProcessor project(":ViewOptProcessor") 34 | implementation project(":viewoptapi") 35 | implementation project(":lib1") 36 | 37 | } 38 | 39 | 40 | apply from: rootProject.file('view_opt.gradle') 41 | 42 | 43 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/testviewopt/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.example.testviewopt", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import com.zhy.demo.viewopt.ViewOpt; 8 | 9 | import androidx.appcompat.app.AppCompatActivity; 10 | 11 | public class BaseActivity extends AppCompatActivity { 12 | 13 | @Override 14 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 15 | View view = ViewOpt.createView(name, context, attrs); 16 | if (view != null) { 17 | return view; 18 | } 19 | return super.onCreateView(parent, name, context, attrs); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | import android.os.Bundle; 6 | 7 | public class MainActivity extends BaseActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/view/MyMainView1.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt.view; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import androidx.annotation.Nullable; 8 | 9 | public class MyMainView1 extends View { 10 | 11 | 12 | public MyMainView1(Context context, @Nullable AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/view/MyMainView2.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt.view; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import androidx.annotation.Nullable; 8 | 9 | public class MyMainView2 extends View { 10 | 11 | 12 | public MyMainView2(Context context, @Nullable AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/view/MyMainView3.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt.view; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import androidx.annotation.Nullable; 8 | 9 | public class MyMainView3 extends View { 10 | 11 | 12 | public MyMainView3(Context context, @Nullable AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/testviewopt/view/MyMainView4.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt.view; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import androidx.annotation.Nullable; 8 | 9 | public class MyMainView4 extends View { 10 | 11 | 12 | public MyMainView4(Context context, @Nullable AttributeSet attrs) { 13 | super(context, attrs); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhy/demo/viewopt/ViewOpt.java: -------------------------------------------------------------------------------- 1 | package com.zhy.demo.viewopt; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.util.Log; 6 | import android.view.View; 7 | 8 | 9 | @ViewOptHost 10 | public class ViewOpt { 11 | 12 | private static volatile IViewCreator sIViewCreator; 13 | 14 | static { 15 | try { 16 | String ifsName = ViewOpt.class.getName(); 17 | String proxyClassName = String.format("%s__ViewCreator__Proxy", ifsName); 18 | Class proxyClass = Class.forName(proxyClassName); 19 | Object proxyInstance = proxyClass.newInstance(); 20 | if (proxyInstance instanceof IViewCreator) { 21 | sIViewCreator = (IViewCreator) proxyInstance; 22 | } 23 | } catch (Throwable e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | 28 | public static View createView(String name, Context context, AttributeSet attrs) { 29 | 30 | 31 | try { 32 | if (sIViewCreator != null) { 33 | View view = sIViewCreator.createView(name, context, attrs); 34 | if (view != null) { 35 | Log.d("lmj", name + " 拦截生成"); 36 | } 37 | return view; 38 | } 39 | } catch (Throwable ex) { 40 | ex.printStackTrace(); 41 | } 42 | 43 | return null; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main1.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 21 | 22 | 23 | 26 | 27 | 30 | 31 | 32 | 33 | 34 | 37 | 38 | 39 | 42 | 43 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TestViewOpt 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/testviewopt/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.testviewopt; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.5.3' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 31 13:02:19 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /lib1/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib1/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.2" 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'androidx.appcompat:appcompat:1.0.2' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 33 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 34 | implementation project(":lib2") 35 | 36 | } 37 | 38 | 39 | project.afterEvaluate { 40 | android.libraryVariants.all { variant -> 41 | def variantName = variant.name 42 | def depSet = new HashSet() 43 | tasks.all { 44 | if ("assemble${variantName.capitalize()}".equalsIgnoreCase(it.name)) { 45 | project.configurations.each { conf -> 46 | if (conf.name == "implementation") { 47 | println(" ${it.name} ========> " + conf.dependencies.size()) 48 | conf.dependencies.each { dep -> 49 | println(" ${project.name} depends ========> " + dep.name) 50 | depSet.add(dep) 51 | project.dependencies.add("api", dep) 52 | } 53 | depSet.each { 54 | conf.dependencies.remove(it) 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /lib1/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/lib1/consumer-rules.pro -------------------------------------------------------------------------------- /lib1/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /lib1/src/androidTest/java/com/example/lib1/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.lib1; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.example.lib1.test", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib1/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /lib1/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | lib1 3 | 4 | -------------------------------------------------------------------------------- /lib1/src/test/java/com/example/lib1/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.lib1; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /lib2/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /lib2/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.2" 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'androidx.appcompat:appcompat:1.0.2' 31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 35 | } 36 | -------------------------------------------------------------------------------- /lib2/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/lib2/consumer-rules.pro -------------------------------------------------------------------------------- /lib2/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /lib2/src/androidTest/java/com/example/lib2/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.lib2; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.example.lib2.test", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib2/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /lib2/src/main/java/com/example/lib2/Lib2Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.lib2; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | import android.os.Bundle; 6 | 7 | public class Lib2Activity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_lib2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lib2/src/main/java/com/example/lib2/Lib2View.java: -------------------------------------------------------------------------------- 1 | package com.example.lib2; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | import androidx.annotation.Nullable; 8 | 9 | public class Lib2View extends View { 10 | public Lib2View(Context context, @Nullable AttributeSet attrs) { 11 | super(context, attrs); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /lib2/src/main/res/layout/activity_lib2.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /lib2/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | lib2 3 | 4 | -------------------------------------------------------------------------------- /lib2/src/test/java/com/example/lib2/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.lib2; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':lib1', ':ViewOptAnnotation', ':ViewOptProcessor', ':viewoptapi', ':lib2' 2 | rootProject.name='TestViewOpt' 3 | -------------------------------------------------------------------------------- /view_opt.gradle: -------------------------------------------------------------------------------- 1 | import groovy.util.slurpersupport.GPathResult 2 | import groovy.util.slurpersupport.Node 3 | 4 | project.afterEvaluate { 5 | def mergeDebugResourcesTask = project.tasks.findByName("mergeDebugResources") 6 | if (mergeDebugResourcesTask != null) { 7 | def resParseDebugTask = project.tasks.create("ResParseDebugTask", ResParseTask.class) 8 | resParseDebugTask.isDebug = true 9 | mergeDebugResourcesTask.finalizedBy(resParseDebugTask); 10 | } 11 | 12 | } 13 | 14 | 15 | class ResParseTask extends DefaultTask { 16 | File viewNameListFile 17 | boolean isDebug 18 | HashSet viewSet = new HashSet<>() 19 | // 自己根据输出几个添加 20 | List ignoreViewNameList = Arrays.asList("include", "fragment", "merge", "view","DateTimeView") 21 | 22 | @TaskAction 23 | void doTask() { 24 | 25 | File distDir = new File(project.buildDir, "tmp_custom_views") 26 | if (!distDir.exists()) { 27 | distDir.mkdirs() 28 | } 29 | viewNameListFile = new File(distDir, "custom_view_final.txt") 30 | if (viewNameListFile.exists()) { 31 | viewNameListFile.delete() 32 | } 33 | viewNameListFile.createNewFile() 34 | viewSet.clear() 35 | viewSet.addAll(ignoreViewNameList) 36 | 37 | try { 38 | File resMergeFile = new File(project.buildDir, "/intermediates/incremental/merge" + (isDebug ? "Debug" : "Release") + "Resources/merger.xml") 39 | 40 | println("resMergeFile: ${resMergeFile.getAbsolutePath()} === ${resMergeFile.exists()}") 41 | 42 | if (!resMergeFile.exists()) { 43 | return 44 | } 45 | 46 | XmlSlurper slurper = new XmlSlurper() 47 | GPathResult result = slurper.parse(resMergeFile) 48 | if (result.children() != null) { 49 | result.childNodes().forEachRemaining({ o -> 50 | if (o instanceof Node) { 51 | parseNode(o) 52 | } 53 | }) 54 | } 55 | 56 | 57 | } catch (Throwable e) { 58 | e.printStackTrace() 59 | } 60 | 61 | } 62 | 63 | void parseNode(Node node) { 64 | if (node == null) { 65 | return 66 | } 67 | if (node.name() == "file" && node.attributes.get("type") == "layout") { 68 | String layoutPath = node.attributes.get("path") 69 | try { 70 | XmlSlurper slurper = new XmlSlurper() 71 | GPathResult result = slurper.parse(layoutPath) 72 | 73 | String viewName = result.name(); 74 | if (viewSet.add(viewName)) { 75 | viewNameListFile.append("${viewName}\n") 76 | } 77 | if (result.children() != null) { 78 | result.childNodes().forEachRemaining({ o -> 79 | if (o instanceof Node) { 80 | parseLayoutNode(o) 81 | } 82 | }) 83 | } 84 | } catch (Throwable e) { 85 | e.printStackTrace(); 86 | } 87 | 88 | } else { 89 | node.childNodes().forEachRemaining({ o -> 90 | if (o instanceof Node) { 91 | parseNode(o) 92 | } 93 | }) 94 | } 95 | 96 | } 97 | 98 | void parseLayoutNode(Node node) { 99 | if (node == null) { 100 | return 101 | } 102 | String viewName = node.name() 103 | if (viewSet.add(viewName)) { 104 | viewNameListFile.append("${viewName}\n") 105 | } 106 | if (node.childNodes().size() <= 0) { 107 | return 108 | } 109 | node.childNodes().forEachRemaining({ o -> 110 | if (o instanceof Node) { 111 | parseLayoutNode(o) 112 | } 113 | }) 114 | } 115 | 116 | 117 | } 118 | -------------------------------------------------------------------------------- /viewoptapi/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /viewoptapi/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.2" 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 15 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | 30 | implementation 'androidx.appcompat:appcompat:1.0.2' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 33 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 34 | } 35 | -------------------------------------------------------------------------------- /viewoptapi/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/ViewOptDemo/24a80e66c195f5c47dadaee92eba9abd69978cc0/viewoptapi/consumer-rules.pro -------------------------------------------------------------------------------- /viewoptapi/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /viewoptapi/src/androidTest/java/com/example/viewoptapi/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.viewoptapi; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.example.viewoptapi.test", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /viewoptapi/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /viewoptapi/src/main/java/com/zhy/demo/viewopt/IViewCreator.java: -------------------------------------------------------------------------------- 1 | package com.zhy.demo.viewopt; 2 | 3 | import android.content.Context; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | public interface IViewCreator { 8 | 9 | View createView(String name, Context context, AttributeSet attrs); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /viewoptapi/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ViewOptApi 3 | 4 | -------------------------------------------------------------------------------- /viewoptapi/src/test/java/com/example/viewoptapi/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.viewoptapi; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } --------------------------------------------------------------------------------