Android studio does not find a dependency

1

android studio asks me for a class that is supposedly in a jar, but can not find it I have a class with the following imports:

import com.google.gson.*;
import com.sun.net.httpserver.*;
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

In the build.gradle I have the following

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "bfhsoftware.sonidoambiental"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '26.0.2'
}
repositories {
    maven { url 'https://mvnrepository.com/artifact/com.sun.net.httpserver/http' }
    maven { url 'https://mvnrepository.com/artifact/com.sun/tools' }

}
dependencies {
    compile 'com.sun.net.httpserver:http:20070405'
    compile 'com.sun:tools:1.7.0.13'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation files('lib/Sonidoambiental-1.2-jar-with-dependencies.jar')
    compileOnly files('tools-1.7.0.13.jar')
}

and it gives me the following error:

...
Caused by: java.lang.ClassNotFoundException: Didn't find class "sun.misc.Service" on path: DexPathList[[zip file
...

"sun.misc.service" for what I understand belongs to sun.misc.tools that is in the dependencies, but I do not understand why it does not take it !! I have added by maven and the file tools-1.7.0.13.jar, I do not understand why it does not take it, does anyone know why it can be? thanks

    
asked by Bernardo Harreguy 05.02.2018 в 16:13
source

2 answers

1

The classes of the sun.* package should not be used, Oracle does not guarantee its continuity and they have been replaced. In your case, you should change sun.misc.Service by java.util.ServiceLoader . For example, from

Iterator<MyServiceProvider> = sun.misc.Service.providers(MyServiceProvider.class);

a

Iterator<MyServiceProvider> = java.util.ServiceLoader.load(MyServiceProvider.class).iterator();

More information at this SO question

    
answered by 05.02.2018 в 16:32
1

I could assure you that the Sun classes "sun.misc.service" do not work with Android.

You do not need to add any dependency, use the class ServiceLoader :

Add the import:

import java.util.ServiceLoader;

Use the class ServiceLoader :

ServiceLoader serviceLoader = ServiceLoader.load(ClaseServicio.class);
    
answered by 06.02.2018 в 21:22