2

My main activity starts with the following:

package com.example.alexander.bootintervals;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.example.alexander.libraryproject.MyLogger;

public class MainActivity extends AppCompatActivity {
   ...

Now I am using some code from in other package, for example in package com.example.alexander.libraryproject. How can I get the name of the main app/package of where the main activity is defined? Is there a way to extract the string com.example.alexander.bootintervals from a code called which resides in com.example.alexander.libraryproject?

Alex
  • 41,580
  • 88
  • 260
  • 469

4 Answers4

1

You could use this to get the full package name

String packageName = BuildConfig.APPLICATION_ID
Justin Conroy
  • 376
  • 1
  • 12
  • Answer updated to use BuildConfig, which is context-less. – Justin Conroy Oct 10 '16 at 12:34
  • Not quite what I want. With that, I get the name of the library, but not of the main code calling the library. I get like `com.example.alexander.libraryproject` but what I want is `com.example.alexander.bootintervals` ... – Alex Oct 10 '16 at 12:41
  • I am not sure how to do that elegantly. Not sure if it is possible without `Context`. You could however, put a `public static String libraryName = "com.example.alexander.libraryproject"` in your class definition and call it that way. I know it isn't elegant but it would work... – Justin Conroy Oct 10 '16 at 12:46
  • What class do you mean? The one in 'LibraryProject'? – Alex Oct 10 '16 at 12:56
  • I mean for every class inside of `libraryproject` and `bootintervals` (in your example you showed the `MyLogger` class), put in the static String variable that can be called from anywhere. – Justin Conroy Oct 10 '16 at 12:59
  • But the 'MyLogger' class can be called from many DIFFERENT other modules/packages/MainActivities, with different names. So putting in something in a class in `libraryproject` does not help in any way. Maybe this was not clear? – Alex Oct 10 '16 at 13:00
0

Try MainActivity.class.getCanonicalName() and ignore the last period and the class name that follows it.

nandsito
  • 3,782
  • 2
  • 19
  • 26
  • No does not work. Neither `MainClass` is defined, nor the name `MainActivity`, because I am trying this code in the library. There is no main activity – Alex Oct 10 '16 at 12:29
0

so simple:

   String full_package_name = MainActivity.class.getCanonicalName();

if you dont have any context in you module do as below: 1- define an interface like below in your module (lets say X module):

public interface Provider{
  public String getPackageName();
}

2- in your App modules (lets say A and B) you need to send an implementation of this interface to the module X like below:

public class ProviderImpl implements Provider{
  @Override
  public String getPackageName()
  {
        return BuildConfig.APPLICATION_ID;
  }
 }

then send the implementation to the module X some how:

ModuleX obj=new ModuleX(new ProviderImpl());

you can add whatever you want to this interface and make your Modules implement it.

then in your module X just call getPackageName() method of the implementation and get your packageName ;)

  • I have no main activity in the library. Not that simple!! – Alex Oct 10 '16 at 12:39
  • 1
    why dont you send the packagename to the library ? do you have any context in your module ? if you have there is another way to do so –  Oct 10 '16 at 12:49
  • Then I have to rewrite the library. I hoped that android would provide a way so a code knows actually what program it is running in. I mean, this can be done easily with code running in Linux,. Every Process has an ID and some information associated with it, but I guess Android is much much more complicated than that. Waybe what I want is just impossible... – Alex Oct 10 '16 at 12:53
  • 1
    if the module is going to be used by your own project you can define meta data in manifest and read in module (even pure java module) !! –  Oct 10 '16 at 12:56
  • are you talking about the manifest related to the package containing the 'MainActivity'? – Alex Oct 10 '16 at 12:57
  • 1
    no what android is doing is so logical. you can access MainActivity from your library but it makes circular dependency. the best way is passing a context if its android module or passing the packagename as string to library. –  Oct 10 '16 at 12:58
  • there is alot you can do but you must tell me the exact case you have. are both application and module yours ? or you have a module that is going to be used by others ? –  Oct 10 '16 at 13:01
  • Ok, then I have to pass the module name or something to the library. I will probably define an interface in the main code to do that – Alex Oct 10 '16 at 13:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125340/discussion-between-alex-and-newbie-androider). – Alex Oct 10 '16 at 13:02
  • 1
    look i updated the answer this is the neatest way to do this but you can send an string variable instead of implementations too but thats not the best practice ;) –  Oct 10 '16 at 13:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125348/discussion-between-the-androider-and-alex). –  Oct 10 '16 at 14:11
0

you can add static function getInstance() in MainActivity that return the context of it and you can use it to get it's context and calling function that gives you package name as following:

public class MainActivity extends AppCompatActivity {
    private static MainActivity instance;

    public MainActivity () {
        instance = this;
    }

    public static MyApplication getInstance() {
         return instance;
    }

and use it as following:

MainActivity.getInstance.getPackageName();
Asmaa Rashad
  • 593
  • 5
  • 28
  • The library function does not know of any `MainActivity`. It cannot work... – Alex Oct 10 '16 at 12:42
  • are you have more MainActivity in libraryProject and another one in bootintervals ? – Asmaa Rashad Oct 10 '16 at 12:44
  • No, I do not have a main activity in 'libraryProject'. It contains only classes I am using in different projects. Only 'bootinterval' contains a MainActivity – Alex Oct 10 '16 at 12:45
  • The library function does not know of any MainActivity..what do you mean of that, where is your problem exactly after trying this solution – Asmaa Rashad Oct 10 '16 at 12:48
  • The problem is, that AndroidStudio does not know a class named `MainActivity`. The term `MainActivity` is marked in red letters in the editor of AndroidStudio in the code, that is in `LibraryProject`. The project `LibraryProject` does not contain a class `MainActivity`. – Alex Oct 10 '16 at 12:50
  • Ok , try to import package `com.example.alexander.bootintervals` in your project class to see `MainActivity` – Asmaa Rashad Oct 10 '16 at 13:01