How to pass String from several modules?

I have 3 modules: mod1 , mod2 and mod3 . The mod1 module has the dependencies mod2 and mod3 .

I want to have one line (in particular, it will be UserAgent, because all 3 HTTP-talk modules on some server and all 3 should have the same set of user agents), which should be shared between these 3 modules .

+5
source share
3 answers

If this "one line" that you want to make common to all other modules is in the strings.xml file (not in the String in the .java file), then you can create a general module, mode0 , including your String values ​​that you want distribute strings.xml for other modules in it. Then you can add mode0 as a dependency on other modules. This may not be the best way to do this, but hopefully it will be useful!

0
source

One way to do this is to make a public class with methods for writing and reading information.

  public static String readnote(Context context, String str) { if (context == null) throw new RuntimeException ("Context is null"); SharedPreferences sp = context.getSharedPreferences("somenameyouchoose", 0); String mycontent = sp.getString(str); return mycontent; } public static String writenote(Context context, String str) { if (context == null) throw new RuntimeException ("Context is null"); SharedPreferences sp = context.getSharedPreferences("somenameyouchoose", 0); SharedPreferences.Editor editor = sp.edit(); editor.putString(str); editor.commit(); return str; } 

Then you can write (this, "myUserAgent") and readnote (this, "myUserAgent").

If you use intent, another aproach should use aim.putExtra.

0
source

Add an assembly configuration field to enter your subproject of the top-level assembly file.

It will be something like this:

 subprojects { afterEvaluate { project -> if (project.hasProperty("android")) { android.defaultConfig { buildConfigField 'String', 'USER_AGENT', "\"Android-agent\"" } } } } 

Then, in your BuildConfig files of each module, you will see:

 public static final String USER_AGENT = "Android-agent"; 

PS Perhaps this is not the best implementation of detecting subprojects in android, but the best I know.

0
source

Source: https://habr.com/ru/post/1242376/


All Articles