getResourceAsStream returning null despite called file being in same dir as class getResourceAsStream is called in

前端 未结 2 2001
轻奢々
轻奢々 2020-12-20 03:21

I imported an Android sample coded by Amazon involving AWS\'s DynamoDB which I got from here and was presumably written for Eclipse: https://github.com/awslabs/aws-sdk-andro

2条回答
  •  有刺的猬
    2020-12-20 04:05

    For up to a day I was struggling with this as well. And finally I was able to resolve this very neatly. The problem is not in the JAVA but in the all project structure. E.g. in Android Studio the whole project is under src/main/java whereas main is a flavour of the project. So if you've file(-s) to read from in source's package (e.g.) com/my/example/app you have to edit the build.gradle file for read (clazz.getResourceAsStream(file)) to work properly. I.e. under android define sourceSets like this:

    android {
        /* ... Your stuff ... */
    
        sourceSets {
            // Lets have two flavours to make it more clear
            main {
                resources.srcDirs = ['src/main/java']
            }
    
            flavourFoo {
                resources.srcDirs = ['src/flavourFoo/java']
            }
        }
    }
    

    Hope this helps!

提交回复
热议问题