LoginSignup
17
11

More than 5 years have passed since last update.

Android Studioの新コンフィグレーションimplementationとapiのひとつ注意点

Last updated at Posted at 2018-05-10

Android Studioで古いGradleバージョンで作成されたモジュールのコンパイルで以下のようなエラーが出ました。

Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

早速公式のサイトを確認しましたが、旧「compile」コンフィグレーションが2018を以て廃止するらしいです。

で、代替品は「implementation」と「api」となりますが、どっちに使うべきかなという疑問があると思います。

公式サイトの説明によると、大半のアプリとテスト モジュールに、「implementation」を使用することをお勧めしますと書いてあるので、とりあえず一般的にはこれを使えば問題ないということですね。

そして、私が導入した旧バージョンで開発されたモジュールの「build.gradle」ファイルの「compile」を全部「implementation」で書き換えて、コンパイルしたら特にエラーが出てなく、問題ないようです。

が、アプリのMainActivity.javaでモジュールのlibsの下に存在するjarファイルの中のclassをimportして参照されようとしたら

Cannot reslove symbol 'XXXX'

というエラーが出ました!

この問題は、モジュールの「build.gradle」ファイルの「dependencies」のところに

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'src/main/libs')
    implementation 'com.android.support:support-v4:27.0.0'
    implementation project(':XXXX')
}

↑誤

implementation fileTree(include: ['*.jar'], dir: 'src/main/libs')

になるから。

正しい書き方は

dependencies {
    api fileTree(include: ['*.jar'], dir: 'src/main/libs')
    implementation 'com.android.support:support-v4:27.0.0'
    implementation project(':XXXX')
}

↑正

「fileTree」の前に「api」を使うべきです。

こう書くとアプリのMainActivity.javaにimportでモジュールのlibs下のjarファイルの中身を参照させたらエラーが出なくなります。

IDEとコンパイル環境

・Android Studio 3.1.1
・Android SDK Build-Tools 28-rc2

17
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
11