Commit 1612af77d208064a7e2532f0fbc21d322a9140b0

Authored by 张道锋
1 parent f172a56a

no message

.idea/gradle.xml
@@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
10 <option value="$PROJECT_DIR$" /> 10 <option value="$PROJECT_DIR$" />
11 <option value="$PROJECT_DIR$/app" /> 11 <option value="$PROJECT_DIR$/app" />
12 <option value="$PROJECT_DIR$/mvpsdk" /> 12 <option value="$PROJECT_DIR$/mvpsdk" />
  13 + <option value="$PROJECT_DIR$/processor" />
13 <option value="$PROJECT_DIR$/roundedimageview-2.2.1" /> 14 <option value="$PROJECT_DIR$/roundedimageview-2.2.1" />
14 </set> 15 </set>
15 </option> 16 </option>
.idea/modules.xml
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 <module fileurl="file://$PROJECT_DIR$/parentWorkHolper.iml" filepath="$PROJECT_DIR$/parentWorkHolper.iml" /> 7 <module fileurl="file://$PROJECT_DIR$/parentWorkHolper.iml" filepath="$PROJECT_DIR$/parentWorkHolper.iml" />
8 <module fileurl="file://E:\parentwork\parentWorkHolper.iml" filepath="E:\parentwork\parentWorkHolper.iml" /> 8 <module fileurl="file://E:\parentwork\parentWorkHolper.iml" filepath="E:\parentwork\parentWorkHolper.iml" />
9 <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" /> 9 <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" />
  10 + <module fileurl="file://$PROJECT_DIR$/processor/processor.iml" filepath="$PROJECT_DIR$/processor/processor.iml" />
10 <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" /> 11 <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" />
11 </modules> 12 </modules>
12 </component> 13 </component>
app/build.gradle
@@ -8,6 +8,7 @@ android { @@ -8,6 +8,7 @@ android {
8 keyPassword '123456' 8 keyPassword '123456'
9 storeFile file('../app/parent.jks') 9 storeFile file('../app/parent.jks')
10 storePassword '123456' 10 storePassword '123456'
  11 +
11 } 12 }
12 } 13 }
13 compileSdkVersion 26 14 compileSdkVersion 26
@@ -18,6 +19,11 @@ android { @@ -18,6 +19,11 @@ android {
18 versionCode 1 19 versionCode 1
19 versionName "1.0" 20 versionName "1.0"
20 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 21 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  22 + javaCompileOptions {
  23 + annotationProcessorOptions {
  24 + includeCompileClasspath = true
  25 + }
  26 + }
21 } 27 }
22 buildTypes { 28 buildTypes {
23 release { 29 release {
@@ -31,7 +37,16 @@ android { @@ -31,7 +37,16 @@ android {
31 } 37 }
32 productFlavors { 38 productFlavors {
33 } 39 }
  40 + compileOptions {
  41 + sourceCompatibility JavaVersion.VERSION_1_7
  42 + targetCompatibility JavaVersion.VERSION_1_7
  43 + }
  44 +}
  45 +task processorTask(type: Copy) {
  46 + from '../processor/build/libs/processor.jar' into 'libs/'
34 } 47 }
  48 +processorTask.dependsOn(':processor:build')
  49 +preBuild.dependsOn(processorTask)
35 greendao { 50 greendao {
36 schemaVersion 1//数据库版本升级 51 schemaVersion 1//数据库版本升级
37 } 52 }
@@ -46,6 +61,10 @@ dependencies { @@ -46,6 +61,10 @@ dependencies {
46 implementation project(':mvpsdk') 61 implementation project(':mvpsdk')
47 implementation project(':roundedimageview-2.2.1') 62 implementation project(':roundedimageview-2.2.1')
48 implementation files('libs/AMap_Location_V3.8.0_20180201.jar') 63 implementation files('libs/AMap_Location_V3.8.0_20180201.jar')
49 - implementation 'org.greenrobot:greendao:3.2.2' // add library  
50 - compile "me.leolin:ShortcutBadger:1.1.19@aar" 64 + implementation 'org.greenrobot:greendao:3.2.2'
  65 + // add library
  66 + compile 'me.leolin:ShortcutBadger:1.1.19@aar'
  67 + annotationProcessor 'com.google.dagger:dagger-compiler:2.12'
  68 + //implementation project(':processor')
  69 + compile files('libs/processor.jar')
51 } 70 }
app/libs/processor.jar 0 → 100644
No preview for this file type
app/src/main/assets/migrations/2.sql 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +ALTER table test INSERT ADD COLUMN age INTEGER
app/src/main/assets/migrations/3.sql 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +ALTER table test INSERT ADD COLUMN name TEXT
app/src/main/java/com/shunzhi/parent/annotation/AddColumn.java
@@ -1,19 +0,0 @@ @@ -1,19 +0,0 @@
1 -package com.shunzhi.parent.annotation;  
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 -/**  
9 - * Created by 10501 on 2018/3/28.<br/>  
10 - * 升级数据库版本使用,在新加字段时添加此注解<br/>  
11 - * 只写了int类型和String类型的,其他需要的话自己写吧<br/>  
12 - * 如果我没写完,谁想用谁写吧  
13 - *  
14 - */  
15 -@Retention(RetentionPolicy.RUNTIME)  
16 -@Target(ElementType.FIELD)  
17 -public @interface AddColumn {  
18 - int version();  
19 -}  
app/src/main/java/com/shunzhi/parent/annotation/VersionHelper.java
@@ -1,50 +0,0 @@ @@ -1,50 +0,0 @@
1 -package com.shunzhi.parent.annotation;  
2 -  
3 -import android.text.TextUtils;  
4 -import android.util.SparseArray;  
5 -  
6 -import org.greenrobot.greendao.annotation.Entity;  
7 -  
8 -import java.lang.reflect.Field;  
9 -import java.util.ArrayList;  
10 -import java.util.List;  
11 -  
12 -import timber.log.Timber;  
13 -  
14 -/**  
15 - * Created by 10501 on 2018/3/28.  
16 - */  
17 -  
18 -public class VersionHelper {  
19 - private static final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s";  
20 - private static final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'";  
21 -  
22 - public static SparseArray<List<String>> init(Class clazz,String dbName) {  
23 - SparseArray<List<String>> array = new SparseArray<>();  
24 - Field[] fields = clazz.getDeclaredFields();  
25 - for (Field field : fields) {  
26 - AddColumn column = field.getAnnotation(AddColumn.class);  
27 - if (column != null) {  
28 - Timber.i("---==== :fieldName= %s version :%s fieldClass : %s", field.getName()  
29 - , column.version(), field.getType());  
30 - Integer version = column.version();  
31 - List<String> sqlList = array.get(version);  
32 - if (sqlList == null) {  
33 - sqlList = new ArrayList<>();  
34 - array.put(version, sqlList);  
35 - }  
36 - if (field.getType().getSimpleName().equalsIgnoreCase("string")) {  
37 - String sql = String.format(sql_insert, dbName, field.getName(), "TEXT");  
38 - sqlList.add(sql);  
39 - } else if ("integer".equalsIgnoreCase(field.getType().getSimpleName())) {  
40 - String sql = String.format(sql_insert, dbName, field.getName(), "INTEGER");  
41 - sqlList.add(sql);  
42 - } else {  
43 - //其他类型使用到的时候再写  
44 - Timber.i("类型检测失败");  
45 - }  
46 - }  
47 - }  
48 - return array;  
49 - }  
50 -}  
app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java
1 package com.shunzhi.parent.annotation; 1 package com.shunzhi.parent.annotation;
2 2
  3 +import org.shunzhi.processorlibs.AddColumn;
  4 +
3 /** 5 /**
4 * Created by 10501 on 2018/3/28. 6 * Created by 10501 on 2018/3/28.
5 */ 7 */
6 -  
7 public class VersionTest { 8 public class VersionTest {
8 - @AddColumn(version = 1) 9 + @AddColumn(version = 3,tableName = "test",type = "String",isDebug = false)
9 private String name; 10 private String name;
10 11
11 - @AddColumn(version = 2) 12 + @AddColumn(version = 3,tableName = "test",type = "Integer",isDebug = false)
12 Integer age; 13 Integer age;
13 14
14 String area; 15 String area;
processor/.gitignore 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +/build
processor/build.gradle 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +apply plugin: 'java-library'
  2 +
  3 +dependencies {
  4 + implementation fileTree(dir: 'libs', include: ['*.jar'])
  5 + implementation 'org.greenrobot:greendao:3.2.2'
  6 +}
  7 +
  8 +sourceCompatibility = "1.7"
  9 +targetCompatibility = "1.7"
processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +package org.shunzhi.processorlibs;
  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 +
  9 +@Retention(RetentionPolicy.SOURCE)
  10 +@Target({ElementType.FIELD})
  11 +public @interface AddColumn {
  12 + int version();
  13 +
  14 + String tableName();
  15 +
  16 + String type();
  17 +
  18 + boolean isDebug() default true;
  19 +}
processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java 0 → 100644
@@ -0,0 +1,96 @@ @@ -0,0 +1,96 @@
  1 +package org.shunzhi.processorlibs;
  2 +
  3 +import org.greenrobot.greendao.annotation.Entity;
  4 +
  5 +import java.io.BufferedWriter;
  6 +import java.io.File;
  7 +import java.io.FileOutputStream;
  8 +import java.io.FileWriter;
  9 +import java.io.IOException;
  10 +import java.io.OutputStreamWriter;
  11 +import java.io.PrintStream;
  12 +import java.util.ArrayList;
  13 +import java.util.HashMap;
  14 +import java.util.List;
  15 +import java.util.Map;
  16 +import java.util.Set;
  17 +
  18 +import javax.annotation.processing.AbstractProcessor;
  19 +import javax.annotation.processing.RoundEnvironment;
  20 +import javax.annotation.processing.SupportedAnnotationTypes;
  21 +import javax.annotation.processing.SupportedSourceVersion;
  22 +import javax.lang.model.SourceVersion;
  23 +import javax.lang.model.element.Element;
  24 +import javax.lang.model.element.TypeElement;
  25 +
  26 +@SupportedAnnotationTypes({"org.shunzhi.processorlibs.AddColumn"})
  27 +@SupportedSourceVersion(SourceVersion.RELEASE_7)
  28 +public class VersionProcessor extends AbstractProcessor {
  29 + private final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s";
  30 + private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'";
  31 +
  32 + String path = "app\\src\\main\\assets\\migrations".replace("\\", File.separator);
  33 +
  34 + @Override
  35 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  36 +// File file = new File("test");
  37 +// if (!file.exists())file.mkdir();
  38 + int i = 0;
  39 + BufferedWriter testBw = null;
  40 +
  41 + HashMap<Integer, List<String>> map = new HashMap<>();
  42 +
  43 + try {
  44 + for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) {
  45 + File file = new File("sql.txt");
  46 + if (!file.exists()) file.createNewFile();
  47 + testBw = new BufferedWriter(new OutputStreamWriter(new PrintStream(file)));
  48 + AddColumn addColumn = element.getAnnotation(AddColumn.class);
  49 + int version = addColumn.version();
  50 + String tableName = addColumn.tableName();
  51 + String type = addColumn.type();
  52 + List<String> list = map.get(version);
  53 + boolean isDebug = addColumn.isDebug();
  54 + if (list == null) list = new ArrayList<>();
  55 + String sql = "null";
  56 + if (type.equalsIgnoreCase("String")) {
  57 + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "TEXT");
  58 + if (!isDebug) list.add(sql);
  59 + } else if (type.equalsIgnoreCase("Integer")) {
  60 + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER");
  61 + if (!isDebug) list.add(sql);
  62 + }
  63 + testBw.write(version + " ");
  64 + testBw.write(sql);
  65 + testBw.newLine();
  66 + testBw.flush();
  67 + }
  68 + } catch (Exception e) {
  69 + e.printStackTrace();
  70 + } finally {
  71 + try {
  72 + if (testBw != null) testBw.close();
  73 + } catch (IOException e) {
  74 + e.printStackTrace();
  75 + }
  76 + }
  77 +
  78 + for (Integer version : map.keySet()) {
  79 + try {
  80 + File tmp = new File("tmp");
  81 + String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql";
  82 + File file = new File(tmp.getParentFile(), path);
  83 + BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  84 + for (String str : map.get(version)) {
  85 + bw.write(str);
  86 + bw.newLine();
  87 + bw.flush();
  88 + }
  89 + bw.close();
  90 + } catch (IOException e) {
  91 + e.printStackTrace();
  92 + }
  93 + }
  94 + return true;
  95 + }
  96 +}
processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +org.shunzhi.processorlibs.VersionProcessor
0 \ No newline at end of file 2 \ No newline at end of file
settings.gradle
1 -include ':app', ':mvpsdk', ':roundedimageview-2.2.1' 1 +include ':app', ':mvpsdk', ':roundedimageview-2.2.1', ':processor'
sql.txt 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +3 ALTER table test INSERT ADD COLUMN age INTEGER