https://github.com/shf981862482/SuperAnnotation
在app开发中,难免要做一些sdcard的操作
比如:判断sdcard存在,生成相应目录, 删除文件等;
我们可以通过注解来自动生成如下的文件
外层build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
app build.radle
apply plugin: 'com.android.application'
//注解处理器
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile fileTree(dir: 'libs', include: ['**.*'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
//注解处理器
compile 'com.google.auto.service:auto-service:1.0-rc2'
compile 'com.squareup:javapoet:1.7.0'
compile 'com.sun_multi:annotaion:0.0.3'
apt'com.sun_multi:compiler:0.0.2'
}
SDCardUtil
public final class SuperSDCardUtil {
public static final String mRootFileName = "Super";
public static final String img = "img";
public static final String data = "data";
public static final String temp = "temp";
public static final String msg = "msg";
public static final String sound = "sound";
public static final String soundLocal = "soundLocal";
public static final String download = "download";
static {
createCacheFile();
}
public static String getSDCardImgPath() {
String path = getSDCardPath() + File.separator + img;
return path;
}
public static String getSDCardDataPath() {
String path = getSDCardPath() + File.separator + data;
return path;
}
public static String getSDCardTempPath() {
String path = getSDCardPath() + File.separator + temp;
return path;
}
public static String getSDCardMsgPath() {
String path = getSDCardPath() + File.separator + msg;
return path;
}
public static String getSDCardSoundPath() {
String path = getSDCardPath() + File.separator + sound;
return path;
}
public static String getSDCardSoundLocalPath() {
String path = getSDCardPath() + File.separator + soundLocal;
return path;
}
public static String getSDCardDownloadPath() {
String path = getSDCardPath() + File.separator + download;
return path;
}
public static void createCacheFile() {
initFile(getSDCardImgPath());
initFile(getSDCardDataPath());
initFile(getSDCardTempPath());
initFile(getSDCardMsgPath());
initFile(getSDCardSoundPath());
initFile(getSDCardSoundLocalPath());
initFile(getSDCardDownloadPath());
}
public static boolean isSDCardExistReal() {
boolean isExits = false;
isExits = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
return isExits;
}
public static String getSDCardPath() {
String path = null;
if(isSDCardExistReal()) {
path = Environment.getExternalStorageDirectory().toString() + File.separator +mRootFileName;
}
else {
path = Environment.getDataDirectory().toString() + File.separator +mRootFileName;
}
return path;
}
public static void initFile(String path) {
File file = new File(path);
if(file != null && !file.exists()) {
file.mkdirs();;
}
}
public static void delete(File file) {
if(file.isFile()) {
file.delete();;
return;
}
if(file.isDirectory()) {
File[] childFiles = file.listFiles();
if (childFiles == null || childFiles.length == 0) {
file.delete();
return;
}
for (int i = 0; i < childFiles.length; i++) {
delete(childFiles[i]);
}
file.delete();
}
}
public static void clearFile() {
String path=getSDCardPath();
delete(new File(path));
createCacheFile();
}
}
那么可见以上的方法足够用了、但是如果我们切换到了别的项目,如果产品说图片文件目录需要是imgsbchanpin,难道要复制一份java文件,改一下常量所对应的目录名称吗?
no,no,no 作为懒人我们要想办法搞一套自动化的东西
自动成代码
我们会想起一些android框架、比如dagger2,databinding等
dagger2的原理就是是在编译时根据annotation来生成class文件
配置好环境
在根目录下的build.gradle添加apt
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
app下的build.gradle添加注解处理器插件 javapoet等
//注解处理器
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
//注解处理器
compile 'com.google.auto.service:auto-service:1.0-rc2'
compile 'com.squareup:javapoet:1.7.0'
}
自定义注解
我新建了一个java的module
因为annotation和javapoet暂时支持jdk1.7,如果你用的1.8需要在生成的java module中的
build.gradle添加如下代码
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
然后创建自己annotation
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;