-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAndroidUtils.java
More file actions
56 lines (48 loc) · 1.96 KB
/
Copy pathAndroidUtils.java
File metadata and controls
56 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package utils;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.xml.XmlDocument;
import java.io.File;
public class AndroidUtils {
/**
* 获取App对应的包名根目录
*/
public static VirtualFile getAppPackageBaseDir(Project project) {
String path = project.getBasePath() + File.separator +
"app" + File.separator +
"src" + File.separator +
"main" + File.separator +
"java" + File.separator +
getAppPackageName(project).replace(".", File.separator);
return LocalFileSystem.getInstance().findFileByPath(path);
}
public static PsiFile getManifestFile(Project project) {
String path = project.getBasePath() + File.separator +
"app" + File.separator +
"src" + File.separator +
"main" + File.separator +
"AndroidManifest.xml";
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
if(virtualFile == null) return null;
return PsiManager.getInstance(project).findFile(virtualFile);
}
public static String getAppPackageName(Project project) {
PsiFile manifestFile = getManifestFile(project);
XmlDocument xml = (XmlDocument) manifestFile.getFirstChild();
return xml.getRootTag().getAttribute("package").getValue();
}
public static String getFilePackageName(VirtualFile dir) {
if(!dir.isDirectory()) {
// 非目录的取所在文件夹路径
dir = dir.getParent();
}
String path = dir.getPath().replace("/", ".");
String preText = "src.main.java";
int preIndex = path.indexOf(preText) + preText.length() + 1;
path = path.substring(preIndex);
return path;
}
}