How Android handle multiple R.java?

后端 未结 2 1666
清酒与你
清酒与你 2021-02-08 11:34

I\'m making couple of Android library apps for a project. To simplify the question, let\'s say I have two libraries(utilLib, screenLib) in this

相关标签:
2条回答
  • 2021-02-08 12:07
    Log.d(TAG, "version: " + version); // 0.1 show be here
    

    Reason quoting from official dev guide Managing Projects - Library Projects:

    When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.


    Log.d(TAG, "Hello: " + hello); // <---- ? ('UtilLib Hello' or 'ScreenLib Hello')
    

    This is determined by library project priority.

    Quoting from official dev guide Managing Project -Library Projects:

    • Resource conflicts

      Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

    Quoting from official dev guide From Eclipse with ADT - Referencing a library project:

    If you are adding references to multiple libraries, note that you can set their relative priority (and merge order) by selecting a library and using the Up and Down controls. The tools merge the referenced libraries with your application starting from lowest priority (bottom of the list) to highest (top of the list). If more than one library defines the same resource ID, the tools select the resource from the library with higher priority. The application itself has highest priority and its resources are always used in preference to identical resource IDs defined in libraries.

    Quoting from official dev guide From the Command Line - Referencing a Library Project:

    If you are adding references to multiple libraries, note that you can set their relative priority (and merge order) by manually editing the project.properties file and adjusting the each reference's .n index as appropriate. For example, assume these references:

     android.library.reference.1=path/to/library_projectA
     android.library.reference.2=path/to/library_projectB
     android.library.reference.3=path/to/library_projectC
    

    You can reorder the references to give highest priority to library_projectC in this way:

     android.library.reference.2=path/to/library_projectA
     android.library.reference.3=path/to/library_projectB
     android.library.reference.1=path/to/library_projectC
    

    I can't find any word that can explain this more clear than official dev guide.

    0 讨论(0)
  • 2021-02-08 12:25

    you will have to import com.screenLib.R and com.utilLib.R
    In your code it should probably throw an error "cannot resolve hello"

    Lemmme know if this is right!!!!

    0 讨论(0)
提交回复
热议问题