Android Studio Tesseractの設定とエラー compileSdkVersion is not specified.

調査するときのポイント

実際に自分が調べていくときに「使えるな!」と思った方法です。

  1. エラーメッセージをコピって検索する
  2. エラーメッセージを翻訳して日本語にする

色々なサイト、ブログ記事を追いかけるように、みて、試して問題解決に向けて調べていきます。

しかし、途中で「根本的な仕組みを理解したほうが早い」場合もあるので、頭の片隅にそれを置いておくと良いかもしれません。

compileSdkVersion is not specified.

上記のようなエラーメッセージが出て、ビルドができない状態になりました。

調べてみるとこちらのページに下のような記述がありました。

プロジェクト下の build.gradle に以下を記載していますか?

allprojects {
repositories {
mavenLocal()
jcenter()
maven {
url "$rootDir/../node_modules/react-native/android"
}
google()
}
}

これは、プロジェクトのbuild.gradleを修正すると言うことを意味しているので下の図の「AndroidOpenCV」と書かれているものを修正しました。

最終的に下のようになりました。

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

Could not find method maven() for arguments

次に出力されたエラーメッセージは上のものです。

mavenのホームディレクトリ(インストールした場所)を指定しなくてはいけないけど、全く違う場所を指定しているので出力されたエラーです。

Mavenプラグインを使用

参考サイトはこちらのAndroidドキュメントです。

しかし、これは必要ないと判断しました。

maven {
    url 'https://maven.google.com/'
    name 'Google'
}

そして、初めのエラー対応を考えます。

ふと気がつく

「compileSdkVersion is not specified.」このメッセージは日本語にすると「compileSdkVersionが指定されていません。」になります。

ここを中心に調べていくとGradleのページに着きました。
しかし、有益な情報は見つからず。。。

プロジェクトの設定を見てみる

ヘッダーメニューからFile -> Project Structureを選択

Gradleのバージョンを6.5に設定しました。

Could not find com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2.

次に上のエラーが出ました。
こちらのページには次のような追記をしてください。とあったのでそのようにしました。

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5"
  }
}

apply plugin: "com.jfrog.bintray"

Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32.

さらに次のエラーが出ました。

ERROR: Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32.
Searched in the following locations:
  - https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.4.32/kotlin-stdlib-jdk8-1.4.32.pom
If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
Required by:
    project : > com.android.tools.build:gradle:7.0.0-alpha14

下の部分に注目

adjust the 'metadataSources { ... }' of the repository declaration.

次のように、プロジェクトのbuild.gradleを修正

buildscript {
    repositories {
        google()
        jcenter()
    }
    ・
    ・
    ・

下のようなエラーが出たので、「Fix Gradle wrapper and re-import project」をクリック

ダウンロードが始まり、「Could not find 〜」と言うようなエラーが出ます、対象になる場所を削除・コメントアウトしました。
<プロジェクトのbuild.gradle>

//apply plugin: 'com.github.dcendents.android-maven'
//apply plugin: 'com.jfrog.bintray'

そして、次の部分もコメントアウトしました。プル下フォルダのbuild.gradleです。

tess/tess-two/build.gradle

//install {
//    repositories.mavenInstaller {
//        pom.project {
//            name = 'tess-two'
//
//            packaging = 'aar'
//            groupId = 'com.rmtheis'
//            artifactId = 'tess-two'
//
//            developers {
//                developer {
//                    id = 'rmtheis'
//                    name = 'Robert Theis'
//                    email = 'robert.m.theis@gmail.com'
//                }
//            }
//            licenses {
//                license {
//                    name = 'The Apache Software License, Version 2.0'
//                    url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
//                    distribution = 'repo'
//                }
//            }
//            scm {
//                url 'https://github.com/rmtheis/tess-two'
//            }
//        }
//    }
//}
//
//bintray {
//    user = properties.getProperty("bintray.user")
//    key = properties.getProperty("bintray.apikey")
//    configurations = ['archives']
//    pkg {
//        repo = 'maven'
//        name = 'tess-two'
//        userOrg = user
//        publish = true
//    }
//}

Gradle DSL method not found: 'implementation()'

次のエラーです。Android Studioに下のようなエラーがあったので、「Open Gradle wrapper file」をクリックします。

すると、gradle.propertiesファイルが開くので、新しいものを設定します。gradleの新しいバージョンです。

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-rc-1-all.zip

ここまできたらNDKの設定

ヘッダーメニューのTools -> SDK Managerをクリックします。
そして、「SDK Tools」タブを選択します。

そして、[NDK (Side by side)] と [CMake] のチェックボックスをオン
参考ページの内容は以下の通りです。

  1. プロジェクトを開いて、[Tools] > [SDK Manager] をクリックします。
  2. [SDK Tools] タブをクリックします。
  3. [NDK (Side by side)] と [CMake] のチェックボックスをオンにします。

Unable to find method 'org.gradle.api.artifacts.result.ComponentSelectionReason.getDescription()

これは、再起動したら通常通りにビルドが走り、同じエラーが出ました。

そして、Tesseract

こちらのページにある手順を実行します。

export ANDROID_HOME=/path/to/your/android-sdk
git clone git://github.com/rmtheis/tess-two tess
cd tess
./gradlew assemble

まとめると、TesseractをGitからプルしてコンパイルするということです。

このコマンドを実行するのに手間取ったのが、「path/to/your」の部分です。あらかじめNDKのインストール先を理解している必要があります。
NDKのインストール先

android-sdkのインストール先/ndk/ビルド番号?/

実際に自分の場合は下のようにコマンドを叩きました。

~/Android/sdk/ndk/22.1.7171670/ndk-build

結構な時間がかかります。

コンパイル終了後

アプリケーションのbuild.gradleにGradleのデーモン(裏側で動くプロセス)を終了するためにAndroid Studioを再起動します。

あと、Android Studioのアップグレードも行いました。

次のエラー

2つあります。それは、下のような形で出力されたエラーですので、メッセージも複数あります。

This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
Please remove usages of `jcenter()` Maven repository from your build scripts and migrate your build to other Maven repositories.
This repository is deprecated and it will be shut down in the future.
See http://developer.android.com/r/tools/jcenter-end-of-service for more information.
Currently detected usages in: root project 'AndroidOpenCV', project ':app', project ':eyes-two', ...

下の方のエラーを先に対応しました。プロジェクトのbuild.gradleを修正、しかしこれは意味がありませんでした。

    repositories {
        google()
        //jcenter()
    }

エラーが変わりました。

tried to access method sun.security.util.ECUtil.getECParameters(Ljava/security/Provider;)Ljava/security/AlgorithmParameters; from class sun.security.ec.ECKeyPairGenerator

これには、gradle.propertiesに以下のものを追加することで解決できそうです。がダメでした。。。

org.gradle.java.home=JDKへのパス

次の方法を試します。参考にしたサイトはこちらです。

そしたら次のエラーになりました。

Could not get unknown property 'Os' for task

tess-twoのbuild.gradleの以下の行です

def ndkBuildExt = Os.isFamily(Os.FAMILY_WINDOWS) ? ".cmd" : ""

結局のところ

GUIでは原因がわからなくなってきたので、コマンド実行に切り替えることにしいました。

実行するコマンドは「./gradlew clean」です。

しかし。。。

解決には至りませんでした。。。

諦める

考え方を変えることにしました。もともとOpenCVを動かしていたプロジェクトにTesseractを追加する形での設定を行なっていましたが、諦めて、TesseractプロジェクトにOpenCVを追加する形に変更します

つまりは、追加した部分を削除します。そして、新たにTeserractプロジェクトを作成する方向でやり直すことにします。

ビルドを動くようにする

build.gradle(プロジェクト用)を修正して下のようにします。Tesseractを依存関係から外します。
さらに、buildバージョンも修正して4.1.3にします。

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
//        classpath 'com.android.tools.build:gradle:7.0.0-alpha14'
//        classpath 'com.android.tools.build:gradle:1.1.2'

        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
        //classpath 'com.github.dcendents:android-maven-plugin:1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 30
    // You can also create properties to specify versions for dependencies.
    // Having consistent versions between modules can avoid conflicts with behavior.
    supportLibVersion = "30.0.2"
}

ここまできて、依存関係が解決しないエラーが起きました。なのでclasspathを追加して、repositoryも追加しました。

buildscript {
    repositories {
        google()
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath 'com.android.tools.build:aaptcompiler:4.1.3'
        classpath 'com.android.tools.lint:lint-gradle-api:27.1.3'
        classpath 'com.android.tools.build:gradle-api:4.1.3'
        classpath 'com.android.tools:common:27.1.3'
        classpath 'com.android.tools.analytics-library:tracker:27.1.3'
        classpath 'com.android.tools.analytics-library:shared:27.1.3'
        classpath 'com.android.tools:repository:27.1.3'
        classpath 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta09'

        classpath 'com.android.tools.build:builder:4.1.3'
        classpath 'com.android.tools.build:bundletool:0.14.0'
        classpath 'androidx.databinding:databinding-compiler-common:4.1.3'
        classpath 'com.android.tools.analytics-library:crash:27.1.3'
        classpath 'com.android.tools:sdk-common:27.1.3'
        classpath 'com.android.tools.build:apkzlib:4.1.3'
        classpath 'com.android.tools:sdklib:27.1.3'
        classpath 'com.android.tools.layoutlib:layoutlib-api:27.1.3'
        classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta09'
        classpath 'org.ow2.asm:asm-commons:7.0'
        classpath 'org.jdom:jdom2:2.0.6'
        classpath 'com.android.tools:repository:27.1.3'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
        //classpath 'com.github.dcendents:android-maven-plugin:1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

compileSdkVersion is not specified. Please add it to build.gradle

こんなエラーも出ました。
そして、調べるとandroid{}を追加すればOKと言うところにたどり着き以下のようなbuild.gradleになりました。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

apply plugin: 'com.android.application'

buildscript {
    repositories {
        google()
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.3'
        classpath 'com.android.tools.build:aaptcompiler:4.1.3'
        classpath 'com.android.tools.lint:lint-gradle-api:27.1.3'
        classpath 'com.android.tools.build:gradle-api:4.1.3'
        classpath 'com.android.tools:common:27.1.3'
        classpath 'com.android.tools.analytics-library:tracker:27.1.3'
        classpath 'com.android.tools.analytics-library:shared:27.1.3'
        classpath 'com.android.tools:repository:27.1.3'
        classpath 'com.android.tools.build.jetifier:jetifier-core:1.0.0-beta09'

        classpath 'com.android.tools.build:builder:4.1.3'
        classpath 'com.android.tools.build:bundletool:0.14.0'
        classpath 'androidx.databinding:databinding-compiler-common:4.1.3'
        classpath 'com.android.tools.analytics-library:crash:27.1.3'
        classpath 'com.android.tools:sdk-common:27.1.3'
        classpath 'com.android.tools.build:apkzlib:4.1.3'
        classpath 'com.android.tools:sdklib:27.1.3'
        classpath 'com.android.tools.layoutlib:layoutlib-api:27.1.3'
        classpath 'com.android.tools.build.jetifier:jetifier-processor:1.0.0-beta09'
        classpath 'org.ow2.asm:asm-commons:7.0'
        classpath 'org.jdom:jdom2:2.0.6'
        classpath 'com.android.tools:repository:27.1.3'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
        //classpath 'com.github.dcendents:android-maven-plugin:1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
//        jcenter()
//        maven {
//            url 'https://maven.google.com/'
//            name 'Google'
//        }
    }

}

ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 30
    // You can also create properties to specify versions for dependencies.
    // Having consistent versions between modules can avoid conflicts with behavior.
    supportLibVersion = "30.0.2"
}
//task clean(type: Delete) {
//    delete rootProject.buildDir
//}

android {
    compileSdkVersion 30
    buildToolsVersion '30.0.2'

    defaultConfig {
        applicationId ""
        minSdkVersion 15
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

ビルドも通りました。

でわでわ。。。

投稿者:

takunoji

音響、イベント会場設営業界からIT業界へ転身。現在はJava屋としてサラリーマンをやっている。自称ガテン系プログラマー(笑) Javaプログラミングを布教したい、ラスパイとJavaの相性が良いことに気が付く。 Spring framework, Struts, Seaser, Hibernate, Playframework, JavaEE6, JavaEE7などの現場経験あり。 SQL, VBA, PL/SQL, コマンドプロント, Shellなどもやります。

コメントを残す