adwiv / android-fat-aar

Gradle script that allows you to merge and embed dependencies in generted aar file
The Unlicense
1.46k stars 435 forks source link

generateRJava function failing due to non existent dependencies. #55

Open siddpande opened 7 years ago

siddpande commented 7 years ago

Hi,

I am facing an issue when trying to assemble a release variant due to the generateRJava in fat-aar.gradle. Below is the exception: C:\Mobile 3.7 Android\testproj\build\generated\source\r\release\com\eye\R.java:1264: error: cannot find symbol public static int Theme_popupMenuStyle = com.company.mobile.android.sdk.activity.R.styleable.Theme_popupMenuStyle; ^ symbol: variable Theme_popupMenuStyle location: class styleable Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 100 errors :testproj:compileReleaseJavaWithJavac FAILED

FAILURE: Build failed with an exception.

BUILD FAILED

I think that this might be because fat aar is generating the R.java for the aar's that I am trying to embed. This is creating the dependencies that are not being resolved. Also when I comment out the code in generateRJava everything works fine.

Another observation, there are 2 aar's that we are trying to embed, one of them doesnt have a R.java file, I saw this when I opened the exploded AAR. Maybe this is causing some issue.

One more question: In the below part of the code, the R.java is getting generated. This is where the project expects the variable that is in the aar to be in my project also. Why is this being done?

// Generate the R.java file and map to current project's R.java // This will recreate the class file def rTxt = file("$aarPath/R.txt") def rMap = new ConfigObject()

if (rTxt.exists()) {
  rTxt.eachLine {
    line ->
      //noinspection GroovyUnusedAssignment
      def (type, subclass, name, value) = line.tokenize(' ')
      rMap[subclass].putAt(name, type)
  }
}

def sb = "package $aarPackageName;" << '\n' << '\n'
sb << 'public final class R {' << '\n'

rMap.each {
  subclass, values ->
    sb << "  public static final class $subclass {" << '\n'
    values.each {
      name, type ->
        sb << "    public static $type $name = ${libPackageName}.R.${subclass}.${name};" << '\n'
    }
    sb << "    }" << '\n'
}

sb << '}' << '\n'
maxim-petlyuk commented 6 years ago

Sorry, any update on this?

jolly336 commented 6 years ago

@siddpande Hi,did you solved this already?any idea?Thanks~

sipande commented 6 years ago

No, had to move away from fat arr due to this issue.

goolong commented 6 years ago

you can filter none exist id,hope this will help you `task generateRJava << { println "Running FAT-AAR Task :generateRJava"

// Now generate the R.java file for each embedded dependency
def mainManifestFile = android.sourceSets.main.manifest.srcFile;
def libPackageName = "";

if(mainManifestFile.exists()) {

    libPackageName = new XmlParser().parse(mainManifestFile).@package
}

def rClassFile = file("$generated_rsrc_dir/com/didi/unified/pay/R.java")
def rClassMap = new ConfigObject()

def subClassName = null

if (rClassFile.exists()) {
    rClassFile.eachLine {
        line ->
            line = line.trim()
            if(line.contains("public static final class ")) {
                def subline = line.substring(("public static final class ").length())
                subClassName = subline.substring(0, subline.indexOf(" "))
            } else if (line.contains("public static int ")) {
                def subline = line.substring(("public static int ").length())
                def name = subline.substring(0, subline.indexOf("="))
                rClassMap[subClassName].putAt(name, 1)
            }
    }
}

embeddedAarDirs.each { aarPath ->

    def manifestFile = file("$aarPath/AndroidManifest.xml");
    if(!manifestFile.exists()) {
        manifestFile = file("./src/main/AndroidManifest.xml");
    }

    if(manifestFile.exists()) {
        def aarManifest = new XmlParser().parse(manifestFile);
        def aarPackageName = aarManifest.@package

        String packagePath = aarPackageName.replace('.', '/')

        // Generate the R.java file and map to current project's R.java
        // This will recreate the class file
        def rTxt = file("$aarPath/R.txt")
        def rMap = new ConfigObject()

        if (rTxt.exists()) {
            rTxt.eachLine {
                line ->
                    //noinspection GroovyUnusedAssignment
                    def (type, subclass, name, value) = line.tokenize(' ')
                    try {
                        if (rClassMap[subclass].containsKey(name)) {
                            rMap[subclass].putAt(name, type)
                        }
                    } catch (Exception e) {
                        e.printStackTrace()
                    }
            }
        }

        def sb = "package $aarPackageName;" << '\n' << '\n'
        sb << 'public final class R {' << '\n'

        rMap.each {
            subclass, values ->
                sb << "  public static final class $subclass {" << '\n'
                values.each {
                    name, type ->
                        sb << "    public static $type $name = ${libPackageName}.R.${subclass}.${name};" << '\n'
                }
                sb << "    }" << '\n'
        }

        sb << '}' << '\n'

        mkdir("$generated_rsrc_dir/$packagePath")
        file("$generated_rsrc_dir/$packagePath/R.java").write(sb.toString())

        embeddedRClasses += "$packagePath/R.class"
        embeddedRClasses += "$packagePath/R\$*.class"
    }

}

}`