injection container for

1. Annotations

  • ApplicationScoped – specifies that a bean is application scoped.
  • Inject – injectable instance fields or constructors.
  • PostConstruct – method that needs to be run after instantiating an application scoped bean.

2. Dependency injection

Using annotation processors, GhostFish compiles a list of application scoped beans in asset/beans.txt file.

During this stage, it also adds code allowing dependency injection to every non-bean class containing at least 1 @Inject annotation.

3. Example

3.1 Add GhostFish dependency to your gradle file:

implementation "com.danielpuiu:ghostfish:1.1.0"
annotationProcessor "com.danielpuiu:ghostfish-compiler:1.1.0"

3.2 Add GhostFish bind call to your application

Subclass Android application and add the following code to onCreate method:

public void onCreate() {


Don’t forget to add this class to your manifest file right under application tag


3.3 Proguard

If you use Proguard and minified, then make sure to add the following rule to your proguard rules file:

-keep @com.danielpuiu.ghostfish.annotations.ApplicationScoped class * {*;}

3.4 Sample code

3.4.1 Service class

public class SampleService {

    public String helloWorld() {
        return "Hello, world!";

    private void init() {
        // code

3.4.2 Activity class

public class MainActivity extends AppCompatActivity {

    private SampleService sampleService;
    protected void onCreate(Bundle savedInstanceState) {
        TextView textView = findViewById(;

4 Developed By

Daniel PUIU

5 License

Copyright Daniel PUIU

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Source link


Please enter your comment!
Please enter your name here