Saltar al contenido

cómo obtener el nombre del paquete en el ejemplo de código de estudio de Android

Ejemplo 1: nombre del paquete de cambio de estudio de Android

In Android Studio, you can do this:
For example, if you want to change com.example.app to my.awesome.game, then:

In your Project pane, click on the little gear icon ( Gears icon )

Uncheck the Compact Empty Middle Packages option

Compact Empty Middle Packages

Your package directory will now be broken up into individual directories

Individually select each directory you want to rename, and:

Right-click it

Select Refactor

Click on Rename

In the pop-up dialog, click on Rename Package instead of Rename Directory

Enter the new name and hit Refactor

Click Do Refactor in the bottom

Allow a minute to let Android Studio update all changes

Note: When renaming com in Android Studio, it might give a warning. In such case, select Rename All

Enter image description here

Now open your Gradle Build File (build.gradle - Usually app or mobile). Update the applicationId in the defaultConfig to your new Package Name and Sync Gradle, if it hasn't already been updated automatically:

Refactor Directories

You may need to change the package= attribute in your manifest.

Clean and Rebuild.

Clean and Rebuild

Done! Anyway, Android Studio needs to make this process a little simpler.

Ejemplo 2: obtener el nombre del paquete de Android

An idea is to have a static variable in your main activity, instantiated to be the package name. Then just reference that variable.

You will have to initialize it in the main activity's onCreate() method:

Global to the class:

public static String PACKAGE_NAME;
Then..

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    PACKAGE_NAME = getApplicationContext().getPackageName();
}
You can then access it via Main.PACKAGE_NAME.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *