Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

library子工程找不到R.java的问题 #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void setFiler(Filer filer) {
this.mFiler = filer;
this.mRootFile = getRootFile();
this.findPackageName();
this.mRJava = getR();
this.mRJava = isLibrary() ? getRLibrary() : getR();
this.mAttrs = new Attr2FuncReader(new File(mRootFile, "X2C_CONFIG.xml")).parse();
}

Expand Down Expand Up @@ -256,9 +256,8 @@ public void startElement(String uri, String localName, String qName, Attributes

}

private HashMap<String, Integer> getR() {
private HashMap<String, Integer> parseRJava(File rFile) {
HashMap<String, Integer> map = new HashMap<>();
File rFile = getRFile();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(rFile));
Expand All @@ -268,6 +267,9 @@ private HashMap<String, Integer> getR() {
if (line.contains("public static final class layout")) {
layoutStarted = true;
} else if (layoutStarted) {
if (line.contains("private layout() {}") || line.trim().isEmpty()) {
continue;
}
if (line.contains("}")) {
break;
} else {
Expand All @@ -288,6 +290,11 @@ private HashMap<String, Integer> getR() {
return map;
}

private HashMap<String, Integer> getR() {
File rFile = getRFile();
return parseRJava(rFile);
}

private File getRFile() {
String sep = File.separator;
File rFile = null;
Expand Down Expand Up @@ -320,6 +327,88 @@ private File getRFile() {
return rFile;
}

private HashMap<String, Integer> parseRTxt(File rFile) {
HashMap<String, Integer> map = new HashMap<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(rFile));
String line;
boolean layoutStarted = false;
while ((line = reader.readLine()) != null) {
if (line.contains("int layout ")) {
layoutStarted = true;
line = line.substring(line.indexOf("layout ") + 7).trim();
String[] lineSplit = line.split(" ");
int id = Integer.decode(lineSplit[1]);
map.put(lineSplit[0], id);
mRJavaId.put(id, lineSplit[0]);
} else if (layoutStarted) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util.close(reader);
}
return map;
}

private HashMap<String, Integer> getRLibrary() {
File rFile = getRFileLibrary();
if (rFile.getName().endsWith("R.java")) {
return parseRJava(rFile);
} else {
return parseRTxt(rFile);
}
}

private File getRFileLibrary() {
String sep = File.separator;
File rFile = null;
try {
JavaFileObject filerSourceFile = mFiler.createSourceFile("test");
String path = filerSourceFile.toUri().getPath();
path = path.substring(0, path.indexOf("build"));
String rtxtPath = path + "build" + sep + "intermediates" + sep + "symbols" + sep + "debug" + sep + "R.txt";
rFile = new File(rtxtPath);
if (rFile.exists()) {
return rFile;
}

rFile = searchRTxtPath(path + "build");
if (rFile != null && rFile.exists()) {
return rFile;
}
} catch (IOException e) {
e.printStackTrace();
}
Log.e("X2C not find R.txt!!!, path = " + rFile);
return rFile;
}

private File searchRTxtPath(String path) {
File[] subFiles = new File(path).listFiles();
if (subFiles == null) {
return null;
}

for (File subfile : subFiles) {
if (subfile.isDirectory()) {
File rFile = searchRTxtPath(subfile.getAbsolutePath());
if (rFile != null) {
return rFile;
}
} else {
String filename = subfile.getName();
if (filename.endsWith("R.txt") || filename.endsWith("R.java")) {
return subfile;
}
}
}
return null;
}

public HashMap<String, Attr> getAttrs() {
return mAttrs;
}
Expand Down