android-fat-aar icon indicating copy to clipboard operation
android-fat-aar copied to clipboard

generateRJava function failing due to non existent dependencies.

Open siddpande opened this issue 8 years ago • 4 comments

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.

  • What went wrong: Execution failed for task ':testproj:compileReleaseJavaWithJavac'.

Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

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'

siddpande avatar Jan 23 '17 05:01 siddpande

Sorry, any update on this?

maxim-petlyuk avatar Sep 25 '17 16:09 maxim-petlyuk

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

jolly336 avatar Mar 14 '18 16:03 jolly336

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

sipande avatar Mar 14 '18 17:03 sipande

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"
    }

}

}`

goolong avatar May 23 '18 04:05 goolong