file:///android_res/drawable not working when usin

2019-02-12 07:05发布

My problem is that after I added flavors to my project the flavor with a package name different from the actual source seems to have problems with accessing the files in android_res/drawable.

More specifically I have a html-view that calls the following image.

<img src="file:///android_res/drawable/image.png">

The image shows up in the production flavor that has the package name which is the same as the directory of the java-files but it doesn't work for the beta flavor:

productFlavors {
    production {
        packageName "com.company.myapp"
    }

    beta {
        packageName "com.company.beta"
    }
}

where the actual directory with the java files is

java/com/company/myapp

It is rather strange that this happens since the resources are not even in that folder.

2条回答
Anthone
2楼-- · 2019-02-12 07:26

packageName is not supported in gradle. Gradle has a distinction between applicationId and packageName (see http://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename) which means that should be able to have the resources package name differ from the package identifier for your application.

Unfortunately that does not seem to be supported by the file:///android_res URL scheme in a webview.

I was able to workaround it in my case by creating an R class in the other package with the correct inner class and with a public static final int field that was initialized with the value from the real R file. This is a hack and doesn't survive renaming the resource and is not convenient if you have lots of resources you need to access.

So in your case that would look like adding this to your beta flavor source files directory:

package com.company.beta;

class R
{
    class drawable
    {
        public static final int image = com.company.myapp.R.drawable.image;
    }
}
查看更多
神经病院院长
3楼-- · 2019-02-12 07:32

UPDATED ANSWER

Have you set up your flavor folders properly?

If you have two flavors (flavorOne, flavorTwo) you need to set up the source folders accordingly.

Your build.gradle file should look something like this:

productFlavors {
        flavorOne {
            applicationId "com.package.name"
            versionCode 1
            versionName "1.0"
        }
        flavorTwo {
            applicationId "com.package.name.two"
            versionCode 1
            versionName "1.0"
        }
}

So, here is what the folder structure would look like for those two flavors:

src
---|main
--------|java
-------------|com.package.name
--------|res
-------------|drawable

---|flavorOne
--------|java
-------------|com.package.name
--------|res
-------------|drawable

---|flavorTwo
--------|java
-------------|com.package.name
--------|res
-------------|drawable

Put the drawables for each flavor in it's associated folder. So you would have the image.png file for flavorOne in that drawable folder and the image.png for flavorTwo in the flavorTwo drawable folder.

This way localization will work for each flavor.

You should NOT be creating 'R' classes. These are autogenerated.

OLD ANSWER

Not really a solution to your exact problem but another approach.

You could move your image files to the assets folder instead and use this

<img src="file:///android_asset/image.png">
查看更多
登录 后发表回答