简体   繁体   中英

How to build a jar without the r.java?

We can't put resource ids (R.java) directly in a jar file. If we do there is no way to guarantee they will be unique. But how to compile the jar without the R.java ? in my code I do often call like R.style.xxx to obtain the id of xxx but if I don't have the r.java then it's will not compile :(

i construct my jar like this

%JDK_PATH%\javac -cp^
 %ANDROID_PLATFORM%\android.jar;^
 -d source\output^
 source\java\com\mylib\util\r.java^
 source\java\com\mylib\view\myCustomView.java

r.java just a dumy file like this one :

package com.mylib;

public final class R {

  public static final class dimen {

     public static final int al_floating_toolbar_horizontal_margin=0;
     public static final int al_floating_toolbar_vertical_margin=0;
     public static final int al_floating_toolbar_preferred_width=0;
     public static final int al_floating_toolbar_minimum_overflow_height=0;
     public static final int al_floating_toolbar_maximum_overflow_height=0;
     public static final int al_content_rect_bottom_clip_allowance=0;
     public static final int al_floating_toolbar_height=0;
     public static final int al_floating_toolbar_menu_button_minimum_width=0;

  }

}

then i delete all the R.class after and then i create the jar :

%JDK_PATH1_7%\jar cf lib\jar\mylib.jar -C source\output com\mylib\

If we do there is no way to guarantee they will be unique

More importantly, there is no guarantee that they will be correct . Those aren't just random numbers. They tie into how the APK is packaged and how to read the resources.

But how to compile the jar without the R.java ?

If this is a library module, you can eliminate all resources from the project (and all references to them from Java). Then, you can create a JAR using a custom Gradle task .

Your only alternative is to avoid all R references in the Java code, using getResources().getIdentifier() to look them up at runtime. Then, ship the JAR, with the resources outside of the JAR. Apps using the JAR would need to merge the library's resources with the app's own resources. Frankly, I would rather that you publish an AAR, as that is easier and safer for both the publisher and the consumer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM