I want to have an android app in java, but one library module in kotlin. However when I try to run the app in my phone, there's an error saying that it can't find my Kotlin class. This is my kotlin class:
package com.example.mylibrary
import android.util.Log
class A {
fun helloWorld(){
Log.d("Kotlin", "Hello World!")
}
}
and gradle file for my kotlin module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.6.+'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
Here's my main activity:
package com.example.uisample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.mylibrary.A;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A obj = new A();
obj.helloWorld();
}
}
Notice how android studio imports com.example.mylibrary.A instead of com.example.mylibrar.Akt as the reference says. Android studio reports no errors before compiling.
Gradle file for app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.uisample"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile project(':mylibrary')
}
After running the project, gradle throws this error: "cannot find symbol class A". What am I doing wrong?
A few things:
Apply the kotlin-android plugin to your library module build.gradle (see the docs):
apply plugin: 'kotlin-android'
As it stands now, you're not even telling gradle to compile Kotlin files.
Your buildscript block should probably be at the top-level build.gradle rather than the module build.gradle.
Consider updating the fully released version of Kotlin 1.0.1-2
For that, you need to add
buildscript {
ext.kotlin_version = '1.4.30' <-- This line
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" <-- This line
}
}
In the gradle.build (project)
and then add the
apply plugin: 'kotlin-android'
In the gradle.build (App)
Related
Error:(36, 0)
Could not find method api() for arguments [project ':model'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
Please help me.
This is my build.gradle file.
apply plugin: 'com.android.application'
//apply plugin: 'io.fabric'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.manafzar.mytaxi.driver"
minSdkVersion 16
targetSdkVersion 26
versionCode 23
versionName "2.0.3"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
javaCompileOptions {
annotationProcessorOptions {
arguments = [eventBusIndex:
'com.manafzar.mytaxi.driver.DriverEventBusIndex']
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
api project(':model')
api 'com.google.maps.android:android-maps-utils:0.5'
api 'com.diogobernardino:williamchart:2.5.0'
}
apply plugin: 'com.google.gms.google-services'
I ran into the same issue when migrating from Gradle 5.0 to Gralde 6.0.x.
As #ShadowMan mentioned, the issue was missing the java-library plugin which introduces the api method.
In your build.gradle file you should include:
plugins {
id 'java-library'
}
More info can be found here: https://docs.gradle.org/current/userguide/java_library_plugin.html
I added two project dependency to build.gradle of project:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.2'
**api project(':StickyListHeaders')**
**api project(':Farayan.Core')**
}
}
After moving dependencies to build.gradle of module, problem resolved:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
**api project(':StickyListHeaders')**
**api project(':Farayan.Core')**
}
Please add "compile" instead of "api" in your dependencies tag.
And please add below code in your main build.gradle file:
allprojects {
repositories {
jcenter()
google()
maven {
url "https://maven.google.com"
}
}
}
I want to use grpc in Android Studio, but failed in gradle setting.
my gradle plugin version is 2.1.2. I have installed gradle 2.14.
In my app\build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
apply plugin: "com.google.protobuf"
defaultConfig {
applicationId "com.example.test.test_20"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
}
}
buildscript {
repositories {
maven {
mavenCentral()
}
}
dependencies {
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
}
}
protobuf {
protoc {
// The version of protoc must match protobuf-java. If you don't depend on
// protobuf-java directly, you will be transitively depending on the
// protobuf-java version that grpc depends on.
artifact = "com.google.protobuf:protoc:3.0.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'io.grpc:grpc-okhttp:1.0.0'
compile 'io.grpc:grpc-protobuf-lite:1.0.0'
compile 'io.grpc:grpc-stub:1.0.0'
}
Gradle sync message output is
Error:(7, 0) Plugin with id 'com.google.protobuf' not found.
What is wrong in my setting?
I create a new empty project, and app/build.gradle file:
apply plugin: 'com.android.application'
apply plugin: "com.google.protobuf"
buildscript {
repositories {
maven {
//mavenCentral()
url 'https://repo1.maven.org/maven2'
}
}
dependencies {
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.test.testprotobuf"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
}
And gradle-wrapper.properties file:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
I excute "gradlew build" command successfully.
And sync gradle, it prompt that “Error:(2, 0) Cause: org/gradle/api/internal/file/collections/DefaultDirectoryFileTreeFactory
” 。
Something wrong in your build.gradle. You have to move some parts:
apply plugin: 'com.android.application'
apply plugin: "com.google.protobuf"
buildscript {
repositories {
maven {
mavenCentral()
}
}
dependencies {
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
}
}
android {
}
you must add the following code in the build.gradle(the project's build.gradle, not the app's):
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// for gRPC
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.0"
}}
I am using BetterSpinner in my app.
I have simply included it like that:
compile 'com.weiwangcn.betterspinner:library-material:1.1.0'
When I try to use it Android Studio even does not suggest to import the library and marks BetterSpinner red:
The code:
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.registration_spinner_stufe);
textView.setAdapter(adapter);
Where is the mistake that Android Studio does not import the BetterSpinner library into my Fragment class?
The solution
"File" -> "Invalidate Caches..." -> "Invalidate and Restart"
did not work for me..
import com.weiwangcn.betterspinner.library.BetterSpinner;
import com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner;
Was ignored by Android Studio:
My complete grade file:
[app]
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId "com.example.jublikon"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main { res.srcDirs = ['src/main/res', 'src/main/res/drawable/ic_action_search.png'] }
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Google Libraries
compile 'com.android.support:appcompat-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.github.navasmdc:MaterialDesign:1.5#aar'
compile 'com.github.rey5137:material:1.2.1.6-SNAPSHOT'
compile 'com.github.paolorotolo:appintro:3.3.0'
compile 'com.weiwangcn.betterspinner:library-material:1.1.0'
}
[project]
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
mavenCentral()
}
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
I'm on a android project and I need import the ActionBar-PullToRefresh library, so I import the library with gradle like following:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:appcompat-v7:20.0.0') {
force = true
}
compile('com.android.support:support-v4:20.0.0') {
force = true
}
repositories {
mavenCentral()
}
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Then I Sync Project with Gradle Files, so I can use the PullToRefreshLayout in my layout xml file.But when I use the library's class in my Fragment code,I can't get nothing code trigger. When I spell the whole class name PullToRefreshLayout the Android studio will not import the class automatically. So what's the problem.
The next two file is my build.gradle file in the root directory and my app directory:
build.gradle file in the root directory
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build.gradle file in the app directory
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.ifeve.ifeveforandroid"
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:appcompat-v7:20.0.0') {
force = true
}
compile('com.android.support:support-v4:20.0.0') {
force = true
}
repositories {
mavenCentral()
}
compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}
Try importing library this way, add in dependencies section this line:
compile 'com.github.chrisbanes.actionbarpulltorefresh:library:0.9.9'
I'm trying to add both Showcaseview by amlcurran and Android-DirectoryChooser by passy to my android app from MavenCentral, but I keep getting this error for both libraries:
Error:net.rdrei.android.dirchooser:library:2.0
This is my Project Gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
This is the inner one:
apply plugin: 'android'
android {
lintOptions {
checkReleaseBuilds false
}
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 6
versionName "1.2.2"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'org.jaudiotagger:jaudiotagger:2.0.1#jar'
compile 'com.android.support:support-v4:19.1.0'
compile 'net.rdrei.android.dirchooser:library:2.0#aar'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
How can I fix it?
The problem is that you are not specifying the repository URL. Check my answer here:
https://stackoverflow.com/a/23879736/570612