Android Studio单个工程针对不同的App进行资源文件的筛选打包

在工程下新建buildSrc文件夹

添加文件 /buildSrc/src/main/groovy/com.keyidabj.gradle/MyPlugin

com.keyidabj.gradle可以为任意

MyPlugin内容

package com.keyidabj.gradle;

import org.gradle.api.*
import java.nio.file.Files;

class MyPlugin implements Plugin<Project> {

File fileResBuild;

void apply(Project project) {
System.out.println("========================");
System.out.println("hello gradle plugin!");


fileResBuild = new File(project.getProjectDir(),"src/main/res-build");
File fileResParentBuild = new File(project.getProjectDir(),"src/main/res-parent");
File fileResTeacherBuild = new File(project.getProjectDir(),"src/main/res-teacher");
if(!fileResBuild.exists()){
fileResBuild.mkdir();
}
if(!fileResParentBuild.exists()){
fileResParentBuild.mkdir();
}
if(!fileResTeacherBuild.exists()){
fileResTeacherBuild.mkdir();
}

delFolder(fileResBuild.getAbsolutePath());

String appType = null;


int parentTaskNum = 0,teacherTaskNum = 0;

Map<Project, Set<Task>> map = project.getRootProject().getAllTasks(true);
for(Project pro:map.keySet()){
//System.out.println("---- pro : "+pro.getName());
//println("---- pro : "+pro.getName());
Set<Task> tasks = map.get(pro);
//for(Task t:tasks){
// println(t.toString());
//}
if(pro.getName().equals("app-parent")){
parentTaskNum = tasks.size();
}else if(pro.getName().equals("app-teacher")){
teacherTaskNum = tasks.size();
}
}

//Release
if(teacherTaskNum == 0){
appType = "parent";
}else if(parentTaskNum == 0){
appType = "teacher";
}else if(parentTaskNum < teacherTaskNum){
appType = "parent";
}else if(parentTaskNum > teacherTaskNum){
appType = "teacher";
}

System.out.println("parentTaskNum : "+parentTaskNum);
System.out.println("teacherTaskNum : "+teacherTaskNum);
System.out.println("appType : "+appType);
//String appType = project.rootProject.findProperty("appType");

File projectDir = project.getRootProject().getProjectDir();
File emptyImgFile = new File(projectDir,"buildSrc/src/main/res/empty.png");

//System.out.println(emptyImgFile.getAbsolutePath());
//System.out.println(emptyImgFile.exists());

if(appType.equals("parent")){
System.out.println("parent task");
cpEmptyImgFile(emptyImgFile,fileResTeacherBuild);
cpImgFile(fileResParentBuild);
}else if(appType.equals("teacher")){
System.out.println("teacher task");
cpEmptyImgFile(emptyImgFile,fileResParentBuild);
cpImgFile(fileResTeacherBuild);
}

System.out.println("========================");
}

void cpImgFile(File fromFileDir){
for(File dir : fromFileDir.listFiles()){
for(File file : dir.listFiles()){
if(file.getName().contains(".png") ||
file.getName().contains(".jpg") ||
file.getName().contains(".jpeg")
){
File toFile = new File(fileResBuild,dir.getName()+"/"+file.getName());
//System.out.println(emptyImgFile.getAbsolutePath());
//System.out.println(toFile.getAbsolutePath());
if(!toFile.getParentFile().exists()){
toFile.getParentFile().mkdirs();
}
Files.copy(file.toPath(),toFile.toPath())
}
}
}
}

void cpEmptyImgFile(File emptyImgFile,File fromFileDir){
for(File dir : fromFileDir.listFiles()){
for(File file : dir.listFiles()){
if(file.getName().contains(".png") ||
file.getName().contains(".jpg") ||
file.getName().contains(".jpeg")
){
File toFile = new File(fileResBuild,dir.getName()+"/"+file.getName());
//System.out.println(emptyImgFile.getAbsolutePath());
//System.out.println(toFile.getAbsolutePath());
if(!toFile.getParentFile().exists()){
toFile.getParentFile().mkdirs();
}
Files.copy(emptyImgFile.toPath(),toFile.toPath())
}
}
}
}

void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}

boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}

File writename;
BufferedWriter out;
void println(String str){
if(writename == null){
writename = new File(fileResBuild,"/log.txt");
if(!writename.getParentFile().exists()){
writename.getParentFile().mkdirs();
}
if(!writename.exists()){
writename.createNewFile();
}
out = new BufferedWriter(new FileWriter(writename));
}
out.writeLine(str);
//out.flush();
//out.close();
}
}


添加空的图片 /buildSrc/src/main/groovy/com.keyidabj.gradle/MyPlugin

点击这里下载图片


添加文件 /buildSrc/src/build.gradle

apply plugin: 'groovy'
apply plugin: 'maven'

dependencies {
compile gradleApi()
compile localGroovy()
}

repositories {
jcenter()
}


在需要进行资源筛选的module里面的build.gradle里面修改

添加 apply plugin: com.keyidabj.gradle.MyPlugin

添加 res.srcDirs = ['src/main/res', 'src/main/res-build']

apply plugin: 'com.android.library'

apply plugin: com.keyidabj.gradle.MyPlugin

android {
compileSdkVersion 27

defaultConfig {
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

sourceSets {
main {
jniLibs.srcDirs = ['libs']
res.srcDirs = ['src/main/res', 'src/main/res-build']
}
}
}

repositories{
flatDir {
dirs 'libs'
}
}

dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation "com.github.bumptech.glide:glide:3.7.0"

implementation project(':kyd_framework_lib')
implementation project(':kyd_user_lib')
implementation project(':lib_photo_picker')
}