import org.jetbrains.android.facet.AndroidFacet; //导入方法依赖的package包/类
@Nullable
private static Module importExternalApklibArtifact(Project project,
Module apklibModule,
IdeModifiableModelsProvider modelsProvider,
MavenProject mavenProject,
MavenProjectsTree mavenTree,
MavenId artifactMavenId,
String artifactFilePath,
ModifiableModuleModel moduleModel,
Map mavenProject2ModuleName) {
final String genModuleName = AndroidMavenUtil.getModuleNameForExtApklibArtifact(artifactMavenId);
String genExternalApklibsDirPath = null;
String targetDirPath = null;
if (apklibModule == null) {
genExternalApklibsDirPath =
AndroidMavenUtil.computePathForGenExternalApklibsDir(artifactMavenId, mavenProject, mavenTree.getProjects());
targetDirPath = genExternalApklibsDirPath != null
? genExternalApklibsDirPath + '/' + AndroidMavenUtil.getMavenIdStringForFileName(artifactMavenId)
: null;
}
else {
final VirtualFile[] contentRoots = ModuleRootManager.getInstance(apklibModule).getContentRoots();
if (contentRoots.length == 1) {
targetDirPath = contentRoots[0].getPath();
}
else {
final String moduleDir = new File(apklibModule.getModuleFilePath()).getParent();
if (moduleDir != null) {
targetDirPath = moduleDir + '/' + AndroidMavenUtil.getMavenIdStringForFileName(artifactMavenId);
}
}
}
if (targetDirPath == null) {
return null;
}
if (!extractArtifact(artifactFilePath, targetDirPath, project, genModuleName)){
return null;
}
final AndroidExternalApklibDependenciesManager adm = AndroidExternalApklibDependenciesManager.getInstance(project);
adm.setArtifactFilePath(artifactMavenId, FileUtil.toSystemIndependentName(artifactFilePath));
final VirtualFile vApklibDir = LocalFileSystem.getInstance().refreshAndFindFileByPath(targetDirPath);
if (vApklibDir == null) {
LOG.error("Cannot find file " + targetDirPath + " in VFS");
return null;
}
if (apklibModule == null) {
final String genModuleFilePath = genExternalApklibsDirPath + '/' + genModuleName + ModuleFileType.DOT_DEFAULT_EXTENSION;
apklibModule = moduleModel.newModule(genModuleFilePath, StdModuleTypes.JAVA.getId());
}
final ModifiableRootModel apklibModuleModel = modelsProvider.getModifiableRootModel(apklibModule);
final ContentEntry contentEntry = apklibModuleModel.addContentEntry(vApklibDir);
final VirtualFile sourceRoot = vApklibDir.findChild(AndroidMavenUtil.APK_LIB_ARTIFACT_SOURCE_ROOT);
if (sourceRoot != null) {
contentEntry.addSourceFolder(sourceRoot, false);
}
final AndroidFacet facet = AndroidUtils.addAndroidFacet(apklibModuleModel.getModule(), vApklibDir, true);
final AndroidFacetConfiguration configuration = facet.getConfiguration();
String s = AndroidRootUtil.getPathRelativeToModuleDir(apklibModule, vApklibDir.getPath());
if (s != null) {
s = s.length() > 0 ? '/' + s + '/' : "/";
configuration.getState().RES_FOLDER_RELATIVE_PATH = s + AndroidMavenUtil.APK_LIB_ARTIFACT_RES_DIR;
configuration.getState().LIBS_FOLDER_RELATIVE_PATH = s + AndroidMavenUtil.APK_LIB_ARTIFACT_NATIVE_LIBS_DIR;
configuration.getState().MANIFEST_FILE_RELATIVE_PATH = s + AndroidMavenUtil.APK_LIB_ARTIFACT_MANIFEST_FILE;
}
importSdkAndDependenciesForApklibArtifact(project, apklibModuleModel, modelsProvider, mavenTree,
artifactMavenId, mavenProject2ModuleName);
return apklibModule;
}