简体   繁体   中英

How do I use SVGs in android correctly?

I know that official support for SVG didn't start until api level 23, but how do I use it in an app with the min sdk at 17?

I have had it working before, and I don't know what changed. I am using AndroidSVG library.

I get this error when I try to build a production APK

Error:(163) Error: Expected resource of type drawable [ResourceType]

Here is one of the 150 lines of code that I get this error in

socialTypeImage.setImageResource(R.raw.icon_other_blue);

I don't know what other info to provide, so if you need more, please let me know.

Thanks

I don't know if my problem is a duplicate, but if it is, none of those solutions work.

buildscript {
repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    maven { url 'http://repository.codehaus.org' }
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.1.0-alpha1'
    //classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.14.7'
    classpath 'com.google.code.ksoap2-android:ksoap2-android:3.1.1'
    classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2'
    classpath 'io.fabric.tools:gradle:1.+'
}
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
maven { url "https://repo.commonsware.com.s3.amazonaws.com" }
maven { url "http://repository.codehaus.org" }
maven { url 'https://maven.fabric.io/public' }
}

import groovyx.net.http.HTTPBuilder

def getBuildNumber(projectName) {
    def http = new HTTPBuilder('http://eco-crossbar-620.appspot.com')
try {
    def resp = http.get(path: "/${projectName}")
    println "NEW BUILD NUMBER: ${resp.BODY}"
    resp.BODY
} catch (ignored) {
    println "ERROR RETRIEVING BUILD NUMBER"
    0
}
}

def getWorkingBranch() {
def workingBranch = "build"
try {
    workingBranch = """git --git-dir=${rootDir}/.git
                           --work-tree=${rootDir}/
                           rev-parse --abbrev-ref HEAD""".execute().text.trim()
} catch (ignored) {
    // git unavailable or incorrectly configured
}
println "Working branch: " + workingBranch
return workingBranch.replaceAll("/", "-")
}

android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 23
buildToolsVersion '23.0.2'

def build = getBuildNumber("lookcounter-android")

defaultConfig {
    applicationId "com.lookcounter"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 67
    versionName "0.1.$versionCode"
    vectorDrawables.useSupportLibrary = true
    buildConfigField "String", "DEFAULT_USERNAME", "\"\""
    buildConfigField "String", "DEFAULT_PASSWORD", "\"\""
    resValue "string", "base64_encoded_public_key", "key"
}

buildTypes {
    release {
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "DEFAULT_USERNAME", "\"\""
        buildConfigField "String", "DEFAULT_PASSWORD", "\"\""
        debuggable false
        jniDebuggable false
        renderscriptDebuggable false
        pseudoLocalesEnabled false
    }
    debug {
        minifyEnabled false
        debuggable true
        jniDebuggable true
        renderscriptDebuggable true
        pseudoLocalesEnabled true
        zipAlignEnabled false
    }
}

signingConfigs {
    dev {
        storeFile file("../LookCounter.keystore")
        storePassword "izpa55word"
        keyAlias "lookcounterkey"
    }
    prod {
        storeFile file("../LookCounter.keystore")
        keyAlias "lookcounterprodkey"
    }
}

productFlavors {
    production {
        buildConfigField "boolean", "DEV_BUILD", "false"
        resValue "string", "app_name", "Lookcounter"
        signingConfig signingConfigs.prod
        versionCode 2
        minSdkVersion 17
        targetSdkVersion 23
    }
    dev {
        buildConfigField "boolean", "DEV_BUILD", "true"
        resValue "string", "app_name", "(DEV) Lookcounter"
        signingConfig signingConfigs.dev            
        applicationId 'com.lookcounter'
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}

android.applicationVariants.all { variant ->
    if (variant.buildType.name.equals("release")) {
        variant.assemble.doLast {
            def apkName
            def dirName = System.getProperty("user.home") + "/Desktop/lookcounter/apk"
            if (variant.name.contains("production")) {
                dirName += "PlayStore"
                apkName = "Lookcounter_v${versionCode}_PlayStore_${versionName}.apk"
            } else {
                dirName += "dev"
                def branchName = getWorkingBranch()
                if (branchName.equals("develop")) {
                    apkName = "Lookcounter_v${versionCode}_DEV_${build}.apk"
                } else {
                    apkName = "Lookcounter_v${versionCode}_DEV_${branchName}_${build}.apk"
                }
            }
            copy {
                def source = variant.outputs.get(0).getOutputFile()
                from source
                into dirName
                include source.name
                rename source.name, apkName
                println "Output APK copied to $dirName/$apkName"
            }
        }
    }
}
}

repositories {
maven {
    url "https://jitpack.io"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile "com.dev.sacot41:scviewpager:0.0.4"
compile project(':androidSVG')
compile files('../libraries/PriorityDeque-1.0.jar')
compile project(':cropper')
testCompile 'junit:junit:4.12'
// Possibly Keep
compile('com.doomonafireball.betterpickers:library:1.6.0') {
    exclude group: 'com.android.support', module: 'support-v4'
}
// KEEP
compile('com.nostra13.universalimageloader:universal-image-loader:1.9.5') {
    exclude group: 'com.android.support', module: 'support-v7'
}
compile('com.twitter.sdk.android:tweet-composer:0.7.3@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.jakewharton:butterknife:5.1.2'
compile 'org.apache.commons:commons-lang3:3.1'
compile 'com.github.clans:fab:1.6.2'
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.google.code.ksoap2-android:ksoap2-android:3.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:support-vector-drawable:23.2.0'
compile 'com.android.support:animated-vector-drawable:23.2.0'
}

You can't use SVGs directly as a Resource. The Android resource loading system doesn't know what an SVG is. What you need to use is something like:

SVG svg = SVG.getFromResource(R.raw.icon_other_blue);
socialTypeImage.setImageDrawable(new PictureDrawable(svg.toPictureDrawable()));

Or you can use the SVGImageView widget class that is provided. Documentation here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM